[c+Win32] Windows/Dialogs Questions
i am learning about win32 programming.
i came around the code below and it got me having unanswered questions.
Code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
void CreateDialogBox(HWND);
void RegisterDialogClass(HWND);
HINSTANCE ghInstance;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow){
MSG msg;
HWND hwnd;
WNDCLASSW wc = {0};
wc.lpszClassName = L"Window";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
RegisterClassW(&wc);
hwnd = CreateWindowW(wc.lpszClassName, L"Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 150, NULL, NULL, hInstance, NULL);
ghInstance = hInstance;
while(GetMessage(&msg, NULL, 0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:
RegisterDialogClass(hwnd);
CreateWindowW(L"Button", L"Show Dialog", WS_VISIBLE | WS_CHILD, 20, 20, 95, 25, hwnd, (HMENU)1, NULL, NULL);
break;
case WM_COMMAND:
CreateDialogBox(hwnd);
break;
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return (DefWindowProcW(hwnd, msg, wParam, lParam));
}
LRESULT CALLBACK DialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:
CreateWindowW(L"Button", L"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 (DefWindowProcW(hwnd, msg, wParam, lParam));
}
void RegisterDialogClass(HWND hwnd){
WNDCLASSEXW wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = (WNDPROC)DialogProc;
wc.hInstance = ghInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = L"DialogClass";
RegisterClassExW(&wc);
}
void CreateDialogBox(HWND hwnd){
CreateWindowExW(WS_EX_DLGMODALFRAME | WS_EX_TOPMOST, L"DialogClass", L"Dialog Box", WS_VISIBLE | WS_SYSMENU | WS_CAPTION, 100, 100, 200, 150, NULL, NULL, ghInstance, NULL);
}
the code works properly but i have questions;
1. the code line
Code:
void CreateDialogBox(HWND);
void RegisterDialogClass(HWND);
declares two functiions which both return nothing but take as argument handle to a window. however in both functions definition, there is no place where the handle argument which they receive(hwnd) is used. what i'm i missing?
2. in the code line
Code:
wc.lpfnWndProc = (WNDPROC)DialogProc;
the dialog box's procedure function address is type casted to type 'WNDPROC' - note that i'm using my knowledge of data-type casting here but i never knew same thing applied to functions too. if they do, is 'WNDPROC' a type of function? what i'm i missing?
3. according to msdn, for the WNDCLASSEX structure;
Quote:
hInstance
Type: HINSTANCE
A handle to the instance that contains the window procedure for the class.
but in this code we have
Code:
wc.hInstance = ghInstance;
but ghInstance has been assigned the value of the main program's hInstance variable. And the windows procedure contained in main program is WndProc according to
Code:
wc.lpfnWndProc = WndProc;
could it mean that that was why they
"function-type-casted" 'DialogProc;' to '(WNDPROC)DialogProc;' ???
i'm confused. Please pardon this beginner questions.