Valhalla Legends Forums Archive | C/C++ Programming | [Windows API] Dialog Box as Main Window

AuthorMessageTime
Yegg
I've been searching google long enough, maybe someone else could do a better job searching, but I could not figure out how to, using MS Visual C++ 6 set a dialog box to function as my main window. I found 1 site, maybe it was 2, that addressed the issue but ended up not helping at all. The first site had code that would not compile because they used a certain function that had not been defined anywhere and was never mentioned. So, I'm left trying to figure out how to get my dialog box to work as my main window.

I'd greatly appreciate it if someone could help me solve this. I am positive that the solution is trivial, it's just difficult when you have no idea what you're doing.
October 5, 2006, 11:23 PM
UserLoser
In your WinMain, or another function after that, call DialogBox() or CreateDialog() or something along those lines.
October 6, 2006, 12:15 AM
Yegg
[code]int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int show) {
CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAINFORM), 0, DialogProc);
}[/code]

Nothing is displayed. Maybe I did something wrong? Probably!

IIRC, I said, "I am positive that the solution is trivial, it's just difficult when you have no idea what you're doing". Meaning, I am sure the solution is easy to solve, but I cannot solve it because of my lack of experience using the Windows API.

October 6, 2006, 12:43 AM
UserLoser
In your resource editor, is the dialog set to be visible?  If not, when processing WM_INITDIALOG, are you calling ShowWindow?  You also need a message loop
October 6, 2006, 1:48 AM
Yegg
My current code (taken from my main.c) is

[code]BOOL CALLBACK DialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_INITDIALOG:
ShowWindow(hwnd, SW_SHOW);
return 1;
case WM_COMMAND:
return 1;
case WM_CLOSE:
DestroyWindow(hwnd);
return 1;
case WM_DESTROY:
PostQuitMessage(0);
return 1;
default:
return 0;
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int show) {
HWND hDialog;
MSG msg;
int status;

hDialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAINFORM), 0, DialogProc);
if (!hDialog) {
MessageBox(0, "Dialog Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 1;
}
while ((status = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (status == -1) return -1;
if (!IsDialogMessage(hDialog, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}[/code]

Still, the dialog does not display upon executing the application. Thanks for the help so far.
October 6, 2006, 2:14 AM
UserLoser
Here's my code from UserBot:

[code]
BOOL WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Message;
BOOL StopLoop = FALSE;

if(ProfileLauncher::GetInstance()->Initialize(hInstance)) {
OutputDebugString("ProfileLauncher initialize failed!\n");
return 0; // Todo: make error return value
}

while(!StopLoop) {
if(MsgWaitForMultipleObjectsEx(0, 0, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE) == WAIT_OBJECT_0) {
while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) {
if(Message.message == WM_QUIT) {
StopLoop = TRUE;
}
if(!IsDialogMessage(CurrentWindow, &Message)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
}
}

return (BOOL)Message.wParam;
}

...

void UserBot::Start()
{
if(IsRunning == FALSE) {
MainDlg = new MainDialog(hInstance, DIALOG_MAINWINDOW);

MainDlg->SetBotInfo(BotId, BotName);
MainDlg->MakeDialog();
MainDlg->SetWindowTitle("Not logged on");
MainDlg->Show();

IsRunning = TRUE;
}
}

...

HWND Dialog::MakeDialog()
{
return CreateDialogParam(hInstance, MAKEINTRESOURCE(ResourceId), 0, Dialog::MessageRouter, (LPARAM)this);
}

BOOL WINAPI Dialog::MessageRouter(HWND WindowHandle, UINT WindowMessage, WPARAM wParam, LPARAM lParam)
{
Dialog *Self;

if(WindowMessage == WM_INITDIALOG) {
Self = reinterpret_cast<Dialog*>(lParam);
Self->WindowHandle = WindowHandle;

SetWindowLongPtr(WindowHandle, GWLP_USERDATA, reinterpret_cast<LPARAM>(Self));
} else {
Self = reinterpret_cast<Dialog*>(GetWindowLongPtr(WindowHandle, GWLP_USERDATA));
}

CurrentWindow = WindowHandle;

return (Self) ? Self->HandleMessage(WindowMessage, wParam, lParam) : FALSE;
}
[/code]

As you can see, I am using CreateDialogParam so I can pass the class over through WM_INITDIALOG when the dialog is created.  All the dialogs in my program have their own class, i.e., MainDialog, based off of Dialog which is an abstract class its self.

Remember, if your application processes WM_INITDIALOG, among other window messages, you should be returning FALSE.

[code]
BOOL MainDialog::HandleMessage(UINT WindowMessage, WPARAM wParam, LPARAM lParam)
{
switch(WindowMessage) {
case WM_CLOSE:
return OnClose(wParam, lParam);
case WM_COMMAND:
return OnCommand(wParam, lParam);
case WM_INITDIALOG:
return OnInitDialog(wParam, lParam);
case WM_NCDESTROY:
return OnNcDestroy(wParam, lParam);
        ...
default:
return FALSE;
}
}

        ...

BOOL MainDialog::OnInitDialog(WPARAM wParam, LPARAM lParam)
{
        ...

return FALSE;
}
[/code]
October 6, 2006, 2:50 PM

Search