Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Basic question about HWND handle

red888red888 Member Posts: 25
I apologize for what is probably a very stupid question but I am still extremely confused about when/how the instance handle is implemented.
I'm going through a version of the forgers tutorial and in the following code HWND is used with createwindow (apparently a standard implementation cause I see it like this in ever other tutorial) but I still don't know why.
If I comment out: "HWND hwnd;" and "hwnd = ..." the program still works in exactly the same way.
So my question is how do I use this handle, whats the advantage. I know this is an extremely basic question that I should probably know the answer to by know, but hey I'm slow.


Thanks a lot for any responses.


#include

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);

void CreateDialogBox(HWND);
void RegisterDialogClass(HWND);

HINSTANCE ghInstance;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
HWND hwnd;

WNDCLASS wc = {0};

wc.lpszClassName = TEXT( "Window" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;

RegisterClass(&wc);
hwnd = CreateWindow( wc.lpszClassName, TEXT("Window"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 250, 150, NULL, NULL, hInstance, NULL);

ghInstance = hInstance;

while( GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch(msg)
{
case WM_CREATE:
RegisterDialogClass(hwnd);
CreateWindow(TEXT("button"), TEXT("Show dialog"),
WS_VISIBLE | WS_CHILD ,
20, 50, 95, 25,
hwnd, (HMENU) 1, NULL, NULL);
break;

case WM_COMMAND:
CreateDialogBox(hwnd);
break;

case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK DialogProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

switch(msg)
{
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("Ok"),
WS_VISIBLE | WS_CHILD ,
50, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
break;

case WM_COMMAND:
DestroyWindow(hwnd);
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;

}
return (DefWindowProc(hwnd, msg, wParam, lParam));

}


void RegisterDialogClass(HWND hwnd)
{
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = (WNDPROC) DialogProc;
wc.hInstance = ghInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = TEXT("DialogClass");
RegisterClassEx(&wc);

}

void CreateDialogBox(HWND hwnd)
{
CreateWindowEx(WS_EX_DLGMODALFRAME | WS_EX_TOPMOST, TEXT("DialogClass"), TEXT("Dialog Box"),
WS_VISIBLE | WS_SYSMENU | WS_CAPTION , 100, 100, 200, 150,
NULL, NULL, ghInstance, NULL);
}






Comments

  • anthrax11anthrax11 Member Posts: 511
    Don't worry, it's a totally valid question:)

    Keep in mind that the window handle(hwnd) and the instance handle(hInstance) are two different things. Windows uses handles to uniquely identify objects. Thanks to the instance handle, Windows knows which application instance a window belongs to. The handles are managed internally by Windows and stored in memory even if you don't keep a reference to them (meaning even if you don't use "HWND hwnd;" and "hwnd = ..."). The handle of a window is used to perform actions related to that particular window, such as creating its child windows or destroying the window.

    In this case you don't need to save hwnd, because the hwnd provided in the DialogProc callback is exactly the same handle that CreateWindow returns. The only place where you need the window handle is in the DialogProc.

    There may be cases where you need the handle outside of the DialogProc. For example, it's possible for the application that starts your application to tell you how to display your main window. This is defined by nCmdShow, which is a parameter to WinMain. For example, if nCmdShow is SW_MINIMIZE, then the window is created minimized.
    [code]
    hwnd = CreateWindow( wc.lpszClassName, TEXT("Window"),
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    100, 100, 250, 150, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    [/code]
  • red888red888 Member Posts: 25
    Thanks for the response it helped make things a little bit clearer.
Sign In or Register to comment.