How to get Microsofts Win32API-handle of the Main-Window? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Jan 13, 2011 1:52 pm

How to get Microsofts Win32API-handle of the Main-Window?

Post by thebesius » Thu Jan 13, 2011 2:05 pm

Untill now I found the wxWidgets, I was writing small personal applications directly under the Win32API. Using the wxWidgets lets <windows> be accessible and it shall be possible to use classes and call functions of the Win32API.

How to get Microsofts handle of the Main-Window? I don't want to change all my classes.

Till now I used to do it like this:

// With
typedef HWND* ptHWND;

// and
class myClass {
ptHWND lp_hWnd;
myClass(ptHWND a, ... ) {lp_hWnd = a, ... };

// and
HWND g_hWnd;
int APIENTRY WinMain() {
g_hWnd = CreateWindowEx();

// the instanciation
basis_myClass myVar = class basis_myClass (&g_hWnd, ...);

/* allowed me in all cases to pass the handle of the Main-Window to the Win32 API-classes and call their functions inside the memberfunctions of my own classes:
myLocalFunc(...) {...; HDC hdc = GetDC(*lp_hWnd); ...};

The Book about Cross-Platform-GUI-Programming by Smart, Hock und Csomor didn't help me in that question.

Where to edit the IMPLEMENT_APP()-macro? In which header? Or is there a wxGetmsHWND()-function, which I did not find?

Thanks for your answer.

Part Of The Furniture
Part Of The Furniture
Posts: 2662
Joined: Sun Jan 03, 2010 5:45 pm

Post by PB » Fri Jan 14, 2011 6:57 am

Generally speaking you can get application's "main window" from
wxWindow* wxApp::GetTopWindow()

You can get window handle for any window using
WXWidget wxWindow::GetHandle() (on Windows returns HWND)

BUT: when using wxWidgets you should rarely have a need for doing this. You should program into wxWidgets and not around it, for example use wxDC and not HDC related Windows' calls etc. I recommend to have a look at the source code of samples included in wxWidgets distribution and to read the up-to-date documentation at
There's also wxWidgets Wiki: and countless other resources.
The book you mentioned might be somewhat obsolete now, at least in some areas, e.g. nowadays you're supposed to use Bind() instead of message maps for event handling. Also plenty of new classes and functions have been added to wxWidgets after the book was published.

In need of some credit
In need of some credit
Posts: 5
Joined: Thu Jan 13, 2011 1:52 pm

Post by thebesius » Fri Jan 14, 2011 12:45 pm

Thanks a lot, that was very helpfull.

I don't want to program around the wxWidgets, just don't want to change all my old classes.

As a believer I confess: ... the old is gone, the new has come. (NT, 2nd Corinthians, 5,17b) :D

Post Reply