Cannot close my windows

Started by
8 comments, last by al_le 20 years, 10 months ago
Hello. I have recently started with windows programming. I have now done a simple program that makes a window from a resource. The problem is, i cannot close it. Heres the code: /* Trim windows header */ #define WIN32_LEAN_AND_MEAN /* Includes */ #include <windows.h> #include "resource.h" /* Global variables */ HWND hWnd; // Window handle char className[] = "Liquid Silver"; // Class name /* Function prototypes */ int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int); long FAR PASCAL WindowProc(HWND, UINT, WPARAM, LPARAM); /* Start of application (WinMain) */ int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow) { WNDCLASS wc; MSG Msg; /* Register the window class */ wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = DLGWINDOWEXTRA; wc.hInstance = hInst; wc.hIcon = LoadIcon(hInst, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); wc.lpszMenuName = NULL; wc.lpszClassName = className; RegisterClass(&wc); /* Create window from file */ hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_WINDOW), 0, NULL); UpdateWindow(hWnd); ShowWindow(hWnd, nCmdShow); /* Message loop */ while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } /* Clean */ UnregisterClass(className, hInst); return 0; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_COMMAND: switch(LOWORD(wParam)) { } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } return 0; }
Advertisement
ehh... shouldnt that last parameter in CreateDialog have the pointer to the window procedure?
I am a signature virus. Please add me to your signature so that I may multiply.
Sorry pag, the first thing didn''t compile and i don''t understand what you mean with the other statement.

But thanks for trying to help.
so when is the window procedure called? i cant see you passing it anywhere... but anyhow somehow it is passed as you can create the window... hmm.. tried WM_CLOSE? maybe a dialog window doesnt receive a WM_DESTROY message, unless you explicitly says so... heh just some random guesses...

[edited by - pag on May 26, 2003 3:00:43 AM]
I am a signature virus. Please add me to your signature so that I may multiply.
Ohh, i forgot to call it. Ooops. I will try again.
I cannot figure out how the call to WindowProc() will look like.
I tried this:

hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_WINDOW), 0, WindowProc(hWnd, msg, msg.wParam, msg.lParam));

And I got the following error:
error C2664: ''WindowProc'' : cannot convert parameter 2 from ''struct tagMSG *'' to ''unsigned int''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
it should look like this:>
hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_WINDOW), 0, WindowProc);
I am a signature virus. Please add me to your signature so that I may multiply.
Still having trouble. I tried your pice of code and got a new error:

error C2664: ''CreateDialogParamA'' : cannot convert parameter 4 from ''long (struct HWND__ *,unsigned int,unsigned int,long)'' to ''int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)''
None of the functions with this name in scope match the target type
oh well.. but if you can see the window there shouldnt be any problem with the code you had first...
But you could also try changing the LRESULT of WindowProc to BOOL as the error message you get "suggest", actually it "suggest" an int but i looked in the window reference that it should be a BOOL(not bool)...

Anyways, im not sure dialogs get the WM_DESTROY message which you use to destroy the window(closing the application). Try putting:

case WM_CLOSE:
SendMessage(hWnd, WM_DESTROY, 0, 0);
break;

in the switch statement in your window procedure.


hehe havent slept on the whole night


[edited by - pag on May 26, 2003 3:25:09 AM]

[edited by - pag on May 26, 2003 3:25:38 AM]
I am a signature virus. Please add me to your signature so that I may multiply.
If you''re using CreateDialog, then your window proc should be declared like this:

BOOL CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);

and should be passed as a function pointer into CreateDialog. Also, if you''re using dialogs, you don''t need to register a WNDCLASS.

Also, a dialog procedure shouldn''t call DefWindowProc. It should return TRUE if it processes the message, and FALSE if not.

This topic is closed to new replies.

Advertisement