Archived

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

Window then Dialog

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

Below is my source code for creating a window, adding a menu to that window and then if the Menu option to create the game is selected it should bring up a dialog box asking how many players wish to participate. I've been through many versions of this including getting the dialog to appear, but I cannot trap the windows WM_COMMAND message for the dialog. SO I rewrote it....
      
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include <stdio.h>

#include <stdlib.h>

#include "resource.h"

const char g_szClassName[] = "myWindowClass";
HINSTANCE hInstance1;

//Function prototype for callback function

BOOL CALLBACK DialogFunc(HWND hdwnd, UINT Msg, WPARAM wParam, LPARAM lParam);


int APIENTRY WinMain(HINSTANCE hInstance,
					 HINSTANCE hPrevInstance,
					 LPSTR lpCmdLine,
					 int nCmdShow)
{

	WNDCLASSEX wc;
    MSG Msg;
	HWND hwnd;
	hInstance1 = hInstance;

	//Step 1: Registering the Window Class

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = DialogFunc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	 if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	  // Step 2: Creating the Window

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Let it Ride",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 480, 240,
        NULL, NULL, hInstance, NULL);
	
	ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;

}
BOOL CALLBACK DialogFunc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
		case WM_INITDIALOG:
		{
		//set dialog icon

			SetClassLong(hwnd, GCL_HICON, (LONG)LoadIcon(NULL, IDI_APPLICATION));
			return TRUE;
		}
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			EndDialog(hwnd, 0);
			return TRUE;
		case ID_GAME_NEW:
			CreateDialog(hInstance1, MAKEINTRESOURCE(IDD_MAIN), 0, DialogFunc);
                //this code added to html screen..

                case WM_COMMAND
                switch(Msg)//repaired

                {
                   case 31 //number 1 was selected

                    Messagebox(0,"Number One was selected",NULL,"WM_OK")
                   //etc... 

                 } 
			return 0;
	}
return FALSE;
}
      
I have two problems, the current code returns a compling error: This statement crashes during compile: wc.lpfnWndProc = DialogFunc; compiling error return: cpp(30) : error C2440: '=' : cannot convert from 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' This conversion requires a reinterpret_cast, a C-style cast or function-style cast The other once it does display, will my message handler work. I've look at many books and tutorials, but I seem to have a link in my brain that is missing. Some would even say I'm the missing link...but well that is a whole other topic for another board. Any suggestions, comments and pointers (wince)... Triston edited cuz I screwed up... edit #2 cuz I missed a switch variable All the world's a stage...and I seem to fall off quite a bit. [edited by - Triston on August 26, 2002 4:46:33 PM] [edited by - Triston on August 26, 2002 4:48:09 PM]

Share this post


Link to post
Share on other sites
quote:

DialogProc''s have a return type of long, not BOOL (typecast for int). Changing that should make it work.



Thanks. This made it compile. I thikn I did that once, but changed something else as well and FUBAR''d something else up. When I ran the program my main window didn''t show.

I guess I should make a message handler for both the window and the dialog?

-Triston

All the world''s a stage...and I seem to fall off quite a bit.

Share this post


Link to post
Share on other sites
main.cpp

        
#define WIN32_LEAN_AND_MEAN

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

const char g_szClassName[] = "myWindowClass";
HINSTANCE hInstance1;

//Function prototype for callback function

//Dialog Function Callback

LONG CALLBACK DialogFunc(HWND hdwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//Window Function Callback

LONG CALLBACK MainProc(HWND hdwnd, UINT Msg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

WNDCLASSEX wc;
MSG Msg;
HWND hwnd;
hInstance1 = hInstance;


//Step 1: Registering the Window Class

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = MainProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU2);
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Let it Ride",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
480,
240,
HWND_DESKTOP,
NULL,
hInstance,
NULL);

ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;

}
LONG CALLBACK DialogFunc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
//case WM_INITDIALOG:

//{

//set dialog icon

// SetClassLong(hwnd, GCL_HICON, (LONG)LoadIcon(NULL, IDI_APPLICATION));

// return 0;

//}

case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
EndDialog(hwnd, 0);
return 0;
case ID_GAME_NEW:
CreateDialog(hInstance1, MAKEINTRESOURCE(IDD_MAIN), 0, (DLGPROC)DialogFunc);
return 0;
}
return 1;
}

LONG CALLBACK MainProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
// case WM_INITDIALOG:

// {

//set dialog icon

// SetClassLong(hwnd, GCL_HICON, (LONG)LoadIcon(NULL, IDI_APPLICATION));

// return 0;

// }

case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
default:
return DefWindowProc(hwnd, Msg, wParam, lParam);
}
return 1;
}

resource.h

        
#define IDR_MENU2 102//menu


#define IDD_MAIN 103//dialog


#define IDC_ID 1002

#define IDC_SPLAYERS 1003

#define ID_FILE_QUIT 40003//quit button on window



#define ID_GAME_START 40007//start game option on window


#define IDC_RIDE -1


Okay I have my main window appearing (not in vegas but on my desktop), but my menu is not being created with it. I seem to have everything in place. Does anyone have any suggestions?
I think I'm jsut missing something small...
(Menu should be on the window not on the dialog box)
TIA
-Triston
-edited for subpar content

All the world's a stage...and I seem to fall off quite a bit.

[edited by - Triston on August 27, 2002 9:58:42 AM]

[edited by - Triston on August 27, 2002 9:59:41 AM]

[edited by - Triston on August 27, 2002 10:00:19 AM]

Share this post


Link to post
Share on other sites
Nevermind...I the code works, I think my rc file was all messed up....or the setting were whacked someplace. I created a new workspace and used new files and it all works.

-Triston

Share this post


Link to post
Share on other sites