I am attempting to learn about creating windows in c++, I have looked at an article about creating a wrapper class but I don't really understand it. So far I know that you can't have a class method WndProc (I dont know why) but honestly, that is all. Can somebody give an explanation, also explaining the reinterpret_cast? Here is the article.
LRESULT CALLBACK Window::MsgRouter(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
Window *wnd = 0;
if(message == WM_NCCREATE)
{
// retrieve Window instance from window creation data and associate
wnd = reinterpret_cast<Window *>((LPCREATESTRUCT)lparam)->lpCreateParams;
::SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast<long>(wnd));
// save window handle
wnd->SetHWND(hwnd);
}
else
// retrieve associated Window instance
wnd = reinterpret_cast<Window *>(::GetWindowLong(hwnd, GWL_USERDATA));
// call the windows message handler
wnd->WndProc(message, wparam, lparam);
}
Thanks in advance, ell.
The MsgRouter()
procedure acts as a proxy between the Windows message handling system to the Window
instance associated with a HWND
. It routes Windows messages to C++ objects.
A pointer to the Window
instance is passed to the MsgRouter()
procedure via the last parameter of the CreateWindow()
function. When you first create a HWND
via CreateWindow()
(or CreateWindowEx()
), some messages are sent - one of them being WM_NCCREATE
. When the procedure receives a WM_NCCREATE
message, the LPARAM
parameter contains a pointer to a CREATESTRUCT
which contains the arguments that was passed into the CreateWindow()
function. The procedure retrieves the Window
instance pointer from the CREATESTRUCT
and saves it in the HWND
by setting it as an attribute of the HWND
( GWL_USERDATA
via SetWindowLong()
).
Now that the pointer has been saved, the window procedure can from now on retrieve a pointer to the original Window
instance from a HWND
via GetWindowLong()
when it receives a message. Finally, the window procedure calls WndProc()
on the retrieved Window
pointer, passing in the exact message and parameters, so the Window
instance can handle the message.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.