Click to See Complete Forum and Search --> : Using Dialog Boxes / forms VS 6 C++


Theone2k
October 25th, 2004, 07:28 PM
hello i have one more little prob i cant sort i wont to make a program that uses forms i have created in the resource part of VS6 i have created a few forms and i can get the main to work by using

BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
//My Code
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(Frm_Main), NULL, DlgProc);
}

but what i wont to do is have a button that opens more over the top of that for options and stuf is this possible using the way a create my main on or am i doing it complelty the wrong way

finding it hard to find desent tutorials :) thx for any help.

kirants
October 25th, 2004, 07:32 PM
The common way to do it is in first dialog's callback, i.e. DlgProc, you add a handler for WM_COMMAND with ID = button ID, and do a DialogBox for the other form.

Theone2k
October 25th, 2004, 07:42 PM
yeah i have a WM_COMMAND and have all the buttons on that forum working. what i wont to do is click a connect button then a dialog box appears asking for address and port what i dont understand is how on the button press i get it to show the other dialog box

case WM_COMMAND:
{
switch(LOWORD(wParam))
{

case BTN_CONNECT:
{

//wont to call connect form here
}
}
i have tried calling it the same as the main one butt that doesnt work and is there an easy way to do it or could you point me somewere that coulde explain it?

kirants
October 25th, 2004, 07:45 PM
You are in the right track:

case BTN_CONNECT:
{
int nRet = DialogBox(hInstance, MAKEINTRESOURCE(Connect_Dialog_template_ID), NULL, ConnectDlgProc);
}

Theone2k
October 25th, 2004, 07:52 PM
yeah i tried something simmerla to that but when i try it i get

error C2065: 'hInstance' : undeclared identifier

do i have to pass it from the main some how?

im very new to win programming :)

kirants
October 25th, 2004, 07:59 PM
What is generally done is:
1. have a global variable HINSTANCE g_hInstance;
2. Set it to NULL
3. In WinMain, one of the first things to do is to
g_hInstance = [hInstance passed in WinMain];
4. In future, wherever you need to pass the instance handle, use g_hInstance.

Theone2k
October 25th, 2004, 08:21 PM
well this now seems to work

it compiles and runs, I click the connect button button but nothing happens do i need some code to draw it to the screen?

this is what i have so far



BOOL CALLBACK ConnectDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case BTN_CONNECT:
{
break;
}
default:
{
return TRUE;
}
}
break;
}
default:
{
return FALSE;
}
}

}

<Separate Process>

//code befor

case BTN_SEND:
{
int nRet = DialogBox(g_hInstance, MAKEINTRESOURCE(Frm_Connect), NULL, ConnectDlgProc);
}
//code after

mrRee
October 25th, 2004, 09:29 PM
You said you want to open connect form by clicking button connect(BTN_CONNECT i guess) but you don't have any code to create or call the dialog box.

I think you need something like:

case WM_COMMAND:
switch(wParam)
{ case BTN_CONNECT:
DialogBox(ghInst,MAKEINTRESOURCE(Frm_connect),hWnd,(DLGPROC)<your dialog rocedure>);
break;
}

Theone2k
October 25th, 2004, 09:57 PM
Thx for all the Help i have now got it working by.

creating a window in the main



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
g_hInstance = hInstance;

WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc; //Runs this Procedure//
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 = NULL;
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;
}

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Chat Client V2.0",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 240,
NULL, NULL, hInstance, NULL);

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

ShowWindow(hwnd, nCmdShow);

UpdateWindow(hwnd);

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


LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{

return DialogBox(g_hInstance, MAKEINTRESOURCE(Frm_Main), NULL, DlgProc);
//Calls the Main Befor Drawinf the Window

}


then all i need is to call

DialogBox(g_hInstance, MAKEINTRESOURCE(Frm_UserInfo), NULL, UserInfo);

and write a procedure for it and i now hoave multi forms working

Thx for all the help hope this mite help anyone else who is stuck on this

mrRee
October 25th, 2004, 10:15 PM
Actually you don't have to create a main window in WinMain(). You can just call the dialog box that you had design by using:

HWND hWnd=CreateDialog(g_hInstance,MAKEINTRESOURCE((Frm_Main), NULL, (DLGPROC)DlgProc);
in you WInMain.

And you had had to change the message loop to:

while (GetMessage(&msg, hWnd, 0, 0))
{
if (!IsDialogMessage(hWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

then you can call any form from your main dialog box procedure function