Archived

This topic is now archived and is closed to further replies.

Cannot close my windows

This topic is 5314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites