Archived

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

Win32 API Help!!!

This topic is 4948 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

I can't get this working... idea is when you execute the program, a message box asks if you want to load or cancel the loading. So I'm new to the IDOK / IDCANCEL stuff. Here's part of the code:
	switch(Msg)
	{
		case WM_CREATE:  MessageBox(NULL, "Do you want to load the window?", "Message", MB_YESNO);
						 
						 switch( LOWORD( wParam ) )		
						 {    
							case IDOK: MessageBox(NULL, "Yes.", "Yes", MB_OK);
									   break;
						    case IDCANCEL: MessageBox(NULL, "No", "No", MB_OK);
										   PostQuitMessage(WM_QUIT);
										   break;
							default: MessageBox(NULL, "Didn't work", "Didn't work", MB_OK);
									 PostQuitMessage(WM_QUIT);
						 }
						 break;   
[edited by - philvaira on May 31, 2004 5:42:58 PM]

Share this post


Link to post
Share on other sites
What the heck is that code doing?

Ignoring the code for the moment, if you want to process the result of the MessageBox(), check it''s return value. If the user presses the OK button, MessageBox() returns IDOK. If the user presses yes, MessageBox() returns IDYES, etc. Look up the MessageBox() documentation for more details.

Share this post


Link to post
Share on other sites
SiCrane, please don't reply if you can't help with the code. You've never been any help to me in your previous posts.

Thank you.

Now the problem is no matter what I click, the default comes on. Why is this, and how can it be solved?

[edited by - philvaira on May 31, 2004 5:49:53 PM]

Share this post


Link to post
Share on other sites
I didn't actually try your code, but I'm assuming the problem you have is that the window isn't created in either case.

This is because when you handle the WM_CREATE message, you are expected to take whatever action is necessary to create the window. In most cases, you pass the request on to DefWindowProc to actually create the window for you and if you don't pass the message on to DefWindowProc, the window isn't created. I think you want something like this:


switch (Msg)
{
case WM_CREATE:
if (MessageBox(hWnd, "Do you want to load the window?", "Message", MB_YESNO) == IDYES)
DefWindowProc(hWnd, Msg, wParam, lParam);
break;


Hope this helps...

[edited by - EvilSteve on May 31, 2004 5:55:39 PM]

Share this post


Link to post
Share on other sites
It''s working, so that''s good. However, when I hit No, the window loads and quits on its own, all taking place in a half of a second. Am I stuck with that?


switch(Msg)
{
case WM_CREATE: if (MessageBox(hWnd, "Do you want to load the window?", "Message", MB_YESNO) == IDYES)
{
DefWindowProc(hWnd, Msg, wParam, lParam);
}
else
{
PostQuitMessage(WM_QUIT);
}

Share this post


Link to post
Share on other sites
quote:
Original post by philvaira
SiCrane, please don''t reply if you can''t help with the code. You''ve never been any help to me in your previous posts.

Thank you.

lol, did you even read what he said?

Share this post


Link to post
Share on other sites
And by the way, what''s the differences with using the two for this code:

DefWindowProc(hWnd, Msg, wParam, lParam);

- and -

return TRUE;

Share this post


Link to post
Share on other sites
quote:
Original post by philvaira
And by the way, what''s the differences with using the two for this code:

DefWindowProc(hWnd, Msg, wParam, lParam);

- and -

return TRUE;


The first is often used in window-proc functions to pass on messages you won''t handle. What I mean is that when you do a switch-case method to test the message you receive against a bunch of ones you do want to handle, sometimes out of the plethora of possible messages you''d get, it might be one you aren''t going to handle in your switch-case code, therefore if the message gets through that part without returning, you can simply do ''return DefWindowProc()'' and windows will use whatever default mechanism to handle that message.

Personally, when I use dialogs, if a message is sent that I don''t want to handle, I''ll just return false, else true if a match is made in switch-case...but that just works for me.

Share this post


Link to post
Share on other sites
Cool, thanks. My last question is if I hit no, it quits fine, but flashes the window really fast and quits. Is that because it''s creating the window no matter what before it quits?

Share this post


Link to post
Share on other sites
quote:
Original post by philvaira
SiCrane, please don''t reply if you can''t help with the code. You''ve never been any help to me in your previous posts.


You''ve got a remarkably short and/or bad memory. It seems you found me helpful in several threads before: 1, 2, 3.

If you''re going to be this ungrateful to people to try to help you, why should anyone ever want to answer any of your questions?

Not only that, I did answer your question. If you don''t understand why my answer is relevant, you should say that instead of attempting to dismiss it, and any other help I''ve given you in the past, out of hand.

However, to answer you last question, by the time you''ve hit the wndproc, the window has already been created. If you want to prompt the user to not create the window, you should ask before the call to CreateWindow().

Share this post


Link to post
Share on other sites

Nervo is correct, I should have had return DefWindowProc rather than just DefWindowProc. Also, it says in the docs that WM_CREATE is supposed to return -1 if no window is to be created. If you do this right, I don''t think you should need the PostQuitMessage, as the window shouldn''t get created in the first place. Give this a try instead:


switch (Msg)
{
case WM_CREATE:
if (MessageBox(hWnd, "Do you want to load the window?", "Message", MB_YESNO) == IDNO)
return -1;
return DefWindowProc(hWnd, Msg, wParam, lParam);

Share this post


Link to post
Share on other sites

Heh, not quite sure what to tell you about that one...

I know that the MessageBox makes different sounds depending on which MB_ICON flag you Bitwise OR with the 4th parameter...

Examples:

MessageBox(hWnd, "This is a Question", "Message", MB_YESNO | MB_ICONQUESTION);

MessageBox(hWnd, "This is a Warning", "Message", MB_YESNO | MB_ICONWARNING);

Share this post


Link to post
Share on other sites