1

I am trying to find a reliable way to control windows Wordpad, which I successfully embedded in application as a child window.

One command I would like to execute just after document was opened is "print preview". PostMessage seems to be a good tool to do it, yet I am struggling to find a reference of commands accepted by Wordpad without success, not to mention commands which will work on WindowsXP machines as well as Windows Vista, 7 and 8.

The list of commands I am particularly interested in is:

  • Print
  • Print Preview
  • Close without saving (in a more elegant manner than killing the process)

Can somebody share some hints about controlling Wordpad using messages, or possibly in a different way?

too
  • 3,009
  • 4
  • 37
  • 51
  • 8
    The is going to be a world of pain. Can't you just use a rich edit control? – David Heffernan Dec 01 '11 at 15:28
  • 2
    If you want to replicate WordPad, the source code's here: http://msdn.microsoft.com/en-us/library/51y8h3tk(v=VS.90).aspx – Roger Lipscombe Dec 01 '11 at 15:32
  • Anything you do in regards to this is probably not going to be compatible with windows 7, as it uses ribbon controls. Events like 'print', 'print preview' and 'close without saving' could be effected by recording and replaying the relevant windows messages to the wordpad application, but again, you would most likely need to reimplement them for Windows 7. – Anya Shenanigans Dec 01 '11 at 15:38
  • I would happily use edit control, but using original Microsoft's implementation of rendering and printing even though it's probably using EM_FORMATRANGE seems to be a good plan. I already tested Wordpad embedded on Windows 7 - works ok. How about AFX_ID_PREVIEW_PRINT? – too Dec 01 '11 at 16:08
  • 3
    Embedding another process in your UI is **never** a good plan. – David Heffernan Dec 01 '11 at 16:23
  • Fair enough, yet there are standard ways of controlling Windows applications through messages other than sending WM_KEYDOWN and WM_KEYUP. The question is how to do it with a good old wordpad ? If it is not possible to force it to show print preview on start, then I will accept this answer. – too Dec 01 '11 at 16:30
  • The *standard* way to control an app is through [UI Automation](http://msdn.microsoft.com/en-us/library/ms747327.aspx). – David Heffernan Dec 01 '11 at 17:05

1 Answers1

5

Post a WM_COMMAND message for the respective command of WordPad's menu (send it if you have to wait for the command to finish its job). For print preview:

PostMessage(WPad, WM_COMMAND, 57609, 0);

To find out the identifiers, open the application in a resource explorer and search for your items in menu resources. The above works in 2K and W7, so I guess command ID's are not changing frequently.


Since there's no command for 'close without saving' your best bet may be to find a handle of the rich edit control of the application and send an EM_SETMODIFY before attempting to close.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169