Archived

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

WiseElben

Windows Dialog problem

Recommended Posts

#include <windows.h>
#include <cstdlib>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //CALLBACK Prototype

BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam); //Diaolog


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR CmdLine, int wndState)
{
	HWND hWnd;
	MSG msg;
	WNDCLASSEX wndClass;

	wndClass.cbSize = sizeof (wndClass); //Size of class

	wndClass.style = CS_HREDRAW | CS_VREDRAW; //Style of Window

	wndClass.lpfnWndProc = WndProc; //The CALLBACK

	wndClass.cbClsExtra = 0; //Extra bytes for class

	wndClass.cbWndExtra = 0; //			""

	wndClass.hInstance = hInstance; //This instance

	wndClass.hIcon = LoadIcon (NULL, MAKEINTRESOURCE(IDI_ICON1)); //Load the icon

	wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); //Load the cursor

	wndClass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH); //The background

	wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); //The Menu

	wndClass.lpszClassName = "WindowX"; //The class name

	wndClass.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, 0); //Load the small icon


	RegisterClassEx(&wndClass); //Register the class

	if(!&wndClass)
	{
		MessageBox(NULL, "Error: Cannot register WindowX", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hWnd = CreateWindow("WindowX", //Creates the window

						"WindowX Alpha",
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
		400, 400, NULL, NULL, hInstance, NULL);

	if(hWnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	ShowWindow (hWnd, wndState);
	UpdateWindow (hWnd);

	while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

	UnregisterClass("WindowX", hInstance);
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch (iMsg)											// This checks what the message is.  Below is some of the message that windows might return.  There of course is HUNDREDS.

    {														// The only message that you NEED to deal with, is the WM_DESTROY.  The rest you don''t have to check for.  I am just showing some examples on how to check for a message.

		case WM_LBUTTONDOWN:
		case WM_RBUTTONDOWN:
		case WM_MBUTTONDOWN:
			{
				char szFileName[MAX_PATH];
				HINSTANCE hInstance = GetModuleHandle(NULL);

				GetModuleFileName(hInstance, szFileName, MAX_PATH);
				MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);			
			}
			break;

		case WM_CREATE:										// This message is sent when the window is created.


			break;											// We break from the switch statement.


		case WM_SIZE:										// This Message happens when we resize the window.

			// We would put code to resize text or a picture here...

			break;											// Break from the switch statement


		case WM_PAINT:										// This message is sent to the WndProc when the window needs to be repainted.  This might be if we moved the window, resized it, or maximized it, or another window was over it.

			// Put our code here to paint what should be in the window.	

		    break;											// Break from the switch statement


		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
                case ID_FILE_EXIT:
					MessageBox(NULL, "Exiting WindowX", "Exit", MB_OK);
					PostQuitMessage(0);

                break;

				case ID_HELP_ABOUT:
				{
					int dialog = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc);
					if(dialog == IDOK)
					{
						//***********************>>>LOOK HERE<<<********************

						MessageBox(NULL, "You pressed OK", "WindowX", MB_OK | IDI_ICON1);
						//**********************************************************

					}
				}
				break;
			}

		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;

		case WM_QUIT:
		case WM_DESTROY:									// This message is sent when the user closes the window.

			// Here you would handle Deinitialization, freeing memory, etc..

			PostQuitMessage(0);								// You must call this function or else you will need to do control-alt-delete and manually close the program from the programs queue.  0 = WM_QUIT.

															// This function actually puts a message on the message queue.  SendMessage() sends a direct message, which is acted upon immediately.  just for your information.

			break;											// Break from the switch statement

	}													

	return DefWindowProc (hwnd, iMsg, wParam, lParam);		// The DefWindowProc function calls the default window procedure to provide default processing for any window messages that an application does not process. 

															// This function ensures that every message is processed. DefWindowProc is called with the same parameters received by the WndProc. 

}															// End of the WndProc


BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:

        return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    EndDialog(hwnd, IDOK);
                break;
                case IDCANCEL:
                    EndDialog(hwnd, IDCANCEL);
                break;
            }
        break;

        default:
            return FALSE;
    }
    return TRUE;
}
1. If you go to the line marked, LOOK HERE please. Ok, I tried to load an icon that is in the resources, but somethign weird happens. When I compile and run it, then I go to Help -> About, then I press OK, this is how the message box looks like: ---------------- You pressed OK [Retry] [Cancel] ---------------- 2. Since I''m new to Win32, I don''t know where is the spot to fix this next problem. After I go to Help -> About and press Cancel or OK, the program terminates. How do I stop it from doing that? Thanks.
WiseElben.com - Game programming tutorials, articles, and community. E-mail:wiseelben@wiseelben.com AIM: WiseElben ICQ: 299127026 [edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042]

Share this post


Link to post
Share on other sites
1) Ok, I use something like this when loading an icon:
quote:

wndClass.hIconSm = LoadIcon( NULL, MAKEINTRESOURCE(IDI_ICON2) );


Hopefully this will load the icon correctly, ill look into it somemore

2) Also try the following for your About Box:
quote:

DialogBox( hInstance, MAKEINTRESOURCE( IDD_DIALOG1 ), GetDesktopWindow(), ( DLGPROC )AboutDlgProc );



3) For your message box:
quote:

MessageBox(NULL, "You pressed OK", "WindowX", MB_OK);


You cannot put a Icon in the last parameter since that handles the buttons.

- BlueDev



   BlueDev Net

[edited by - BlueDev on August 6, 2003 1:13:45 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by BlueDev
Ok, I use something like this when loading an icon:
quote:

wndClass.hIconSm = LoadIcon( NULL, MAKEINTRESOURCE(IDI_ICON2) );


Hopefully this will load the icon correctly, ill look into it somemore


Actually, it's
wndClass.hIconSm = LoadIcon( NULL, MAKEINTO      RESOURCE(IDI_ICON2) ); 
// Also, wndClass.hIcon = LoadIcon (NULL, MAKEINTO RESOURCE(IDI_ICON1)); //Load the icon






[edited by - raven_soul on August 6, 2003 1:12:32 PM]

[edited by - raven_soul on August 6, 2003 1:18:48 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by glassJAw
You''re missing the "break" for "case WM_COMMAND:"

It falls through to the WM_CLOSE message and ends your program


Ahh. Something simple like that.

quote:

You cannot put a Icon in the last parameter since that handles the buttons.



How about: MessageBox(NULL, "Hello", "Hello", MB_OK | MB_ICONINFORMATION)? That loads an icon.




WiseElben.com - Game programming tutorials, articles, and community.

E-mail:wiseelben@wiseelben.com
AIM: WiseElben
ICQ: 299127026

[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042]

Share this post


Link to post
Share on other sites
I think you can only use predefined icons with messageboxes.


"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"
"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oogly booh. super califragilistic expialodocius.
serious sam. half life. daikanta. deu Sex.
oo woops wrong post.

Share this post


Link to post
Share on other sites