(just needling the "reverse-engineering"-averse ones here)
So I want to add a print page setup dialog to my editor (EdAsm). I've got everything else figured out, but I thought it would be nice to have a little preview display to see, roughly, what the settings look like on a sample printed page. Like what Notepad (and lots of other apps) show:
(https://i.postimg.cc/D4f18NYj/Notepad-preview.gif) (https://postimg.cc/D4f18NYj)
But I'm mystified by how they get that display. The image that they show for the page has to be one of two things: either teeny-tiny text rendered with TextOut() or DrawText(), or a bitmap image of some kind. So I first went spelunking into Notepad.exe itself, using my own string-finder program and a resource editor (PE Explorer), found nothing: no image, nothing that looked like sample text.
Then it dawned on me, it wouldn't be there, it'd be in a DLL, most likely Comdlg32.dll, since that contains the function (PageSetupDlgW()) that puts up the Page Setup dialog. So I looked in there: nothing. No image, no sample text. I looked through the other DLLs that Notepad uses (there are 14 altogether), could find nothing in any of them.
So how do they generate that display? I suppose it could be done programmatically, but what would be the source of those lines? They obviously repeat whatever they're doing 4 times.
I guess rather than actually reverse-engineering this it would be better to just figure out on my own whether I should use a bitmap or text. I'm leaning towards a bitmap, which would probably be easier to deal with.
Anyone have any idea how they do this?
Quote from: NoCforMe on April 29, 2024, 07:43:33 AMAnyone have any idea how they do this?
https://masm32.com/board/index.php?topic=11765.0 (https://masm32.com/board/index.php?topic=11765.0)
look into the xp source files for "notepad.c" . Yes, its C code. But you will have the actual code used. (at least in that ancient version)
Quote from: notepad.c*
* Notepad application
* Copyright (C) 1984-2001 Microsoft Inc.
*
I am not saying to use that code, (surely don't publish it) but it should give you some ideas.
Isn't there an api that does the print preview thingy? I never print anything (have a digital copy already), so have never coded for it.
Later...
A quick perusal through xp notepads c sources, I only see page setup, but not page preview - besides normal printing related code.
Would the page preview stuff be in the page setup code?
Well, that turned out to be the answer. In the file nt5src/Source/XPSP1/NT/shell/comdlg32
/prnsetup.c I found this:
HGDIOBJ hPen, hBr, hFont, hFontGreek;
HRGN hRgn;
TCHAR szGreekText[] = TEXT("Dheevaeilnorpoefdi lfaocr, \nMoiccsriocsnoafrtf \tbnya\nSFlr acnn IF iynnnaepgmaonc\n F&i nyneelglaanm 'Ox' Mnaalgleenyn i&f QCnoamgpeannnyi FI nxca.r\nFSoaynb Ftrfaonscoirscciom, \rCoafl idfeopronlieav\ne\n");
LPTSTR psGreekText;
So they use "greeked" text with a very small font.
Yes, page preview is part of the PageSetupDlg() dialog. (Oddly, the word "preview" was of no help here, as it only occurs once in the source. The key word to search for was "greek".)
Quote from: NoCforMe on April 29, 2024, 08:34:13 AMYes, page preview is part of the PageSetupDlg() dialog.
:smiley:
Quote(Oddly, the word "preview" was of no help here, as it only occurs once in the source. The key word to search for was "greek".)
lol
So now I have to write yet another tool: a string finder that can find Unicode strings.
Project #11,982.
"Greek Text" in Page Setup Dialog (https://cplusplus.com/forum/windows/195646/) :cool:
Says Microsoft (https://learn.microsoft.com/en-us/windows/win32/dlgbox/page-setup-dialog-box)
Quote from: sudoku on April 29, 2024, 08:05:07 AMQuote from: NoCforMe on April 29, 2024, 07:43:33 AMAnyone have any idea how they do this?
https://masm32.com/board/index.php?topic=11765.0 (https://masm32.com/board/index.php?topic=11765.0)
look into the xp source files for "notepad.c" . Yes, its C code. But you will have the actual code used. (at least in that ancient version)
Quote from: notepad.c*
* Notepad application
* Copyright (C) 1984-2001 Microsoft Inc.
*
I am not saying to use that code, (surely don't publish it) but it should give you some ideas.
What??? It's already published! How do you think you were able to read it?
Quote from: NoCforMe on April 29, 2024, 09:30:37 AMWhat??? It's already published! How do you think you were able to read it?
Just because someone posted it in Github, doesn't mean it is fair game to freely post the sources all over - thats what I am saying. Some of the code from the xp versions may still be under copyright... (used in later windows versions) :icon_idea:
I think it is still of questionable origin. I really don't think that Microsoft put it on github.
Do as you want with it though.
Rich Edit control can render page to small window too.
Other option is EMF "picture".
With C++ language
Printing and Previewing Rich Edit Controls (http://yacs.lebeausoftware.org/Papers/REPnP2.html)
Quote from: TimoVJL on April 29, 2024, 04:20:40 PMRich Edit control can render page to small window too
That's actually a cute idea - I might add it to PrintRTF() (https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1446) :cool:
include \masm32\MasmBasic\Res\MbGui.asm ; wCL$(): load file in commandline
GuiControl MyEdit, "RichEdit", wCL$(), bcol LiteYellow, fcol Black, font -14
Event Key
.if VKey==-VK_P
MsgBox 0, Str$("%i pages printed", PrintRtf(hMyEdit, 2)), "PrintRtf:", MB_OK
.endif
GuiEnd
Interesting article.
I think in my case the way to go is to use some kind of image rather than actual text. If you wanted to create a small preview replica of an actual page of text then it would probably be better (though more difficult) to render text at a very small size. As the article explains, there are lots of problems with scaling text down like that. It should be pretty easy to come up with an image that looks like a page of text shrunk down.
Quote from: NoCforMe on April 29, 2024, 06:09:10 PMIt should be pretty easy to come up with an image that looks like a page of text shrunk down.
Don't forget StretchBlt.
Sure, that's a good tool to have. But I'm thinking that wouldn't even be needed: just make a full-size (= small) image that looks like a reduced page of text. Could do an actual screen capture with Paint Shop Pro and reduce it, or just create it by hand, "lines of text", until it looks right.
In any case, no messing around with rendering actual text like PageSetupDlg() does.
In reality, if you print from a RichEdit control, the preview is pretty useless: it comes out exactly as you see it in the control itself.
I should be more precise here about what I want.
I'm not after an actual preview of the text I'm about to print: the type of preview that Notepad, et al, shows (greeked text) is fine for my purposes. The idea is to show the print margins in relation to the page so one can set the margins accordingly.
Heh; surprised that Micro$oft didn't use the industry-standard "lorem ipsum" text for their greeking:
Quote"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Quote from: NoCforMe on April 30, 2024, 06:05:51 AMThe idea is to show the print margins in relation to the page so one can set the margins accordingly.
optional: margins in mm, e.g. void PrintRtf(hMyEdit, 0, 20, 20, 15, 20) ; left, top, right, bottom (https://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1446)
The default values are ok for 99% of all cases. If not, test the margins with Notepad, and use them with your own program. The preview is not that precise anyway.
Printing images with PaintShop is a different use case: there you might need to center the image, or to make it much smaller than the default.
Quote from: jj2007 on April 30, 2024, 06:10:29 AMThe default values are ok for 99% of all cases.
Except that since I'm "rolling my own here" (i.e., not using
PrintSetupDlg()), the defaults are (0,0,0,0), so I need to be able to set reasonable margins. (When I print my code, for instance, if I use a 3-hole punch so I can put the pages in a notebook it punches through some of the text.)
Besides, I can't use that dialog because I'm using my own (non-standard) headers and footers.
Quote from: NoCforMe on April 30, 2024, 06:14:49 AMnot using PrintSetupDlg()), the defaults are (0,0,0,0)
You are not obliged to use a dialog, and you are not obliged to use these defaults. This code does
not use a dialog, it just tells you how many pages were printed:
GuiParas equ "Hello World", w500, h400, b LiteBlueGreen, icon Printer ; width+height, background colour
include \masm32\MasmBasic\Res\MbGui.asm
GuiControl MyEdit, "RichEdit", wCL$(), bcol LiteYellow, fcol Black, font -14 ; load file in commandline
Event Key
.if CtrlVKey()==-VK_P
MsgBox 0, Str$("%i pages printed", PrintRtf(hMyEdit, 0, 20, 20, 150, 20)), "PrintRtf:", MB_OK
.endif
GuiEnd
PrintRtf(hMyEdit, 0, 20, 20, 150, 20) ; handle, mode0=no dialog, left20, top20,
right150, bottom20
(https://i.postimg.cc/gXWmsrSD/PrintRtf.png) (https://postimg.cc/gXWmsrSD)