Win32 Program Won't Close

message handling, icons

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2888 Views - Last Post: 15 February 2009 - 05:19 PM Rate Topic: -----

#16 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Win32 Program Won't Close

Posted 15 February 2009 - 02:12 PM

All right, I was able to unzip the file. I was using WinZip, but I downloaded 7-Zip and it worked fine.

Your first problem, as you said, was that the program continued to run after the window was closed. I think I found the problem in the application's message loop. In main.cpp, on line 89, I changed

while((bRet = GetMessage(&Msg, Wnd, 0, 0 )) != 0)

to

while((bRet = GetMessage(&Msg, NULL, 0, 0 )) != 0).




As for the icon problem: The name of the icon resource is "IDI_ICON", a string.

On line 12 of application.cpp, MAKEINTRESOURCE attempts to convert a resource identifier to something LoadImage can work with:

= (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON), IMAGE_ICON, 32, 32, 0);.




The thing is, there is no icon with an identifier of IDI_MAINICON; just an icon with a name "IDI_MAINICON".

The problem can be resolved by removing the double quotes from around the resource name in the resource editor, so that "IDI_MAINICON" becomes IDI_MAINICION.

Alternatively, it can be fixed by removing MAKEINTRESOURCE(IDI_MAINICON) on line 12 of application.cpp, and specifying just "IDI_MAINICON" with quotes:


WndClsEx.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE("IDI_MAINICON"), IMAGE_ICON, 32, 32, 0);
...
...
WndClsEx.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), "IDI_MAINICON", IMAGE_ICON, 16, 16, 0);

Was This Post Helpful? 0
  • +
  • -

#17 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Win32 Program Won't Close

Posted 15 February 2009 - 03:16 PM

OMG that's it! Muchas gracias, man.

So if you change the second parameter in getmessage() to NULL, it'll pick up messages for any dialog boxes I create, right? This would cause the program to close if the user clicked the [X] in a dialog box, wouldn't it? I've tried this:

while((bRet = GetMessage(&Msg, Wnd.hwnd, 0, 0 )) != 0)

(and making the HWND variable in the Window class public) but the same thing happens.

EDIT: But not if I select "Exit" from under the "File" menu. (The window closes and the program terminates.) They both send PostQuitMessage(WM_QUIT), so I'm not quite sure what the difference is.

btw, thanks for all the help. this has really gotten me a long way!

This post has been edited by lanec42: 15 February 2009 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#18 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Win32 Program Won't Close

Posted 15 February 2009 - 03:23 PM

I'm glad I can help.

I don't understand the question about the dialog boxes, though.
Was This Post Helpful? 1
  • +
  • -

#19 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Win32 Program Won't Close

Posted 15 February 2009 - 04:21 PM

Sorry, I'm really new to this.

Doesn't the message loop handle messages for the entire application, i.e. the main windows and all of the dialogs? If so, sending WM_QUIT will kill all of the program's dialogs, right?
Was This Post Helpful? 0
  • +
  • -

#20 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Win32 Program Won't Close

Posted 15 February 2009 - 04:26 PM

Yep. If the dialog box procedure posts a WM_QUIT message to the thread's message queue, the entire application will terminate. However, if the dialog box closes without posting a WM_QUIT message (without calling PostQuitMessage), the message loop will continue; the program will continue to run.
Was This Post Helpful? 0
  • +
  • -

#21 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Win32 Program Won't Close

Posted 15 February 2009 - 04:42 PM

So is there any way to discern between the dialog and main window's quit messages? Or do dialogs only send wm_close? I guess I could check which window is active when the quit message is sent...

edit: Or just never let the main window be active when there's a dialog up. That'd probably be fine, as any dialogs will be open file, change preferences, etc.

This post has been edited by lanec42: 15 February 2009 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#22 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Win32 Program Won't Close

Posted 15 February 2009 - 05:08 PM

When you want to close the dialog box, in the message procedure of the dialog, call PostMessage(hwndDlg, WM_CLOSE, 0, 0), where hwndDlg is the handle to the dialog box window.

When the message queue retrieves the WM_CLOSE message, it will know that the message belongs to the dialog box. That's because when the message queue recieves a message, it also recieves information about what window the message was intended for. The GetMessage function retrieves a message from the message queue and stores it in an MSG structure. One of the members of the MSG structure, hwnd, is a handle to the window the message must be passed to. So when DispatchMessage is called and given a pointer to the MSG structure as an argument, it knows what window to send it to.

It has no effect on other windows the thread owns. So if a dialog box closes itself, it won't affect the parent window.
Was This Post Helpful? 0
  • +
  • -

#23 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Win32 Program Won't Close

Posted 15 February 2009 - 05:19 PM

Oookay. That makes sense.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2