Reputation: 1945
So i was wondering if there is any difference between the handle returned when creating the window using CreateWindow/CreateWindowEx
, and the one that is passed as a parameter to the WNDPROC
window callback procedure for the concerned window. The context for this question is that I need to use certain API functions that require a handle to the window, so I thought that instead of passing the handle received by the window callback procedures, I could just save a copy of the handle returned by CreateWindow once, and then use it for successive operations, in case both handles are the same.
Regards,
Devjeet
Upvotes: 0
Views: 534
Reputation: 941485
Be careful here, the window procedure is used by multiple instances of a window. The callback is determined by the value of WNDCLASSEX.lpfnWndProc so any CreateWindow/Ex() call that uses the same window class name uses the same callback. If you are doing this to map the callback to a class instance method then you first have to go through a thunk that maps the callback's hwnd argument to a class instance. Making a special case for WM_CREATE of course.
Upvotes: 2
Reputation: 98368
Yes they are the same, that's the whole point of having an HWND. Every window has only one HWND
that identifies it within the system and remains valid until DestroyWindow
is called with it. It is returned by CreateWindow(EX)
and sent in with every message so you can use that as a unique identifier for your window, even if you create several windows of the same class.
But note that some messages are sent to the window procedure even before the CreateFunction
returns, so if you use a global variable to hold your HWND
:
HWND globalHwnd = NULL;
int main()
{
//...
globalHwnd = CreateWindow(...);
}
LRESULT WndProc(HWND hWnd, ...)
{
assert(hWnd == globalHwnd); //fail!
}
The assertion will fail because several messages such as WM_CREATE
are sent before CreateWindow
returns, so the global variable is not yet assigned.
Upvotes: 2