News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

"Reverse engineering": Notepad's print preview

Started by NoCforMe, April 29, 2024, 07:43:33 AM

Previous topic - Next topic

NoCforMe

(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:



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?
Assembly language programming should be fun. That's why I do it.

sudoku

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
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?
:cool:

NoCforMe

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".)
Assembly language programming should be fun. That's why I do it.

sudoku

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
:cool:

NoCforMe

So now I have to write yet another tool: a string finder that can find Unicode strings.
Project #11,982.
Assembly language programming should be fun. That's why I do it.


NoCforMe

Quote from: sudoku on April 29, 2024, 08:05:07 AM
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
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?
Assembly language programming should be fun. That's why I do it.

sudoku

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.
:cool:

TimoVJL

Rich Edit control can render page to small window too.
Other option is EMF "picture".

With C++ language
Printing and Previewing Rich Edit Controls
May the source be with you

jj2007

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() :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

NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

jj2007

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.

NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

jj2007

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.

NoCforMe

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."
Assembly language programming should be fun. That's why I do it.