Drawing on a wxFrame via wxWindow::GetHandle()?

Armel Asselin armelasselin at hotmail.com
Tue Feb 6 06:19:36 PST 2007


> I want to port a rather big application, which is written in ANSI-C
> and uses the GDI-functions for graphical output, to wxWidgets 2.8
> (with Visual C++ 6.0).
> In order to not have to port everything at once, I decided to create
> the main windows of the application as wxFrames and then use the
> method wxWindow::GetHandle() so that the old GDI-functions which need
> to have a HWND-Handle will still work.
> My code looks something like this:
>
>
> wxFrame *myframe = new wxFrame((wxFrame *)NULL, wxT("Test"),
> wxPoint(0,60), wxSize(1280, 800));
>
> HWND WindowHandle = (HWND)myframe->GetHandle();
>
> HDC WindowDC = GetDC(WindowHandle);
>
> Polygon(WindowDC, ...)
>
>
> Now this code doesn't throw any errors, it just simply doesn't draw
> anything on 'myframe'.
> A suggestion I got was that all the painting for a wxFrame should be
> in the 'OnPaint'-method for this particular frame. Is that really the
> only way to draw on a frame? At the moment I have not implemented a
> Paint-Event for 'myframe'.
> I would really like to keep the current version of the source code
> (that means the graphical functions) and just replace them at the
> exact place with wxWidgets-functions one by one.
it is not technically a question of "can I", it is more a question of "what 
will be the effect".
If you create your frame and then draw , then let it live alone, it works... 
until something asks for a refresh. then, the OnEraseBackground/OnPaint 
couple will do its work. Unless you put your drawing code in OnPaint, the 
OnEraseBackground stuff will simply blank out all your drawings.
Note that blocking the Erase part will not help if another 
window/application comes on top of your window (or if it is 
minimized/maximized)...
so, put your drawing code in OnPaint if possible or use a memory DC to draw 
and make your OnPaint be just a simple put bitmap of that memory DC.

HIH
Armel








More information about the wx-users mailing list