NO

Author Topic: Why doesn't my dialog show up?  (Read 7272 times)

Freddy

  • Guest
Why doesn't my dialog show up?
« on: January 02, 2008, 10:49:33 PM »
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include "main.h"


//
//  Process WM_COMMAND message for window/dialog: hwndDlg
//
void hwndDlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
// TODO: Add your message processing code here...
static int count = 0;
char thetext[20];
count++;
wsprintf(thetext,"Clicked %d times",count);
if(id==IDOK)SetDlgItemText(hwnd,IDOK,thetext);
}

//
//  Process WM_CLOSE message for window/dialog: hwndDlg
//
void hwndDlg_OnClose(HWND hwnd)
{
// TODO: Add your message processing code here...
ExitProcess(0);
}


static LRESULT CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
HANDLE_MSG (hwndDlg, WM_CLOSE, hwndDlg_OnClose);
HANDLE_MSG (hwndDlg, WM_COMMAND, hwndDlg_OnCommand);
    }

    return 0;
}


int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
/* The user interface is a modal dialog box */
    DialogBoxParam(0, MAKEINTRESOURCE(DLG_MAIN), 0, (DLGPROC)MainDlgProc,0);
return 0;
}

Why?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2123
Re: Why doesn't my dialog show up?
« Reply #1 on: January 03, 2008, 09:24:41 AM »
First parameter 0, need to be hInstance for resources ?.
Code: [Select]
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
/* The user interface is a modal dialog box */
    DialogBoxParam(hInstance, MAKEINTRESOURCE(DLG_MAIN), 0, (DLGPROC)MainDlgProc,0);
return 0;
}
May the source be with you

Freddy

  • Guest
Re: Why doesn't my dialog show up?
« Reply #2 on: January 03, 2008, 06:53:08 PM »
No. Either with 0, hInstance or GetModuleHandle(0) the dialog doesn't show up.
It must be something else. Although if I register the window class it does show up.
But I've coded dialog-based programs before and it doesn't need to register the class.
Now, I'm confused.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2123
Re: Why doesn't my dialog show up?
« Reply #3 on: January 03, 2008, 07:01:37 PM »
In MainDlgProc WM_INITDIALOG message should return TRUE ?
If in rc file is CLASS keyword, remove it.
« Last Edit: January 03, 2008, 07:53:07 PM by timovjl »
May the source be with you

Freddy

  • Guest
Re: Why doesn't my dialog show up?
« Reply #4 on: January 03, 2008, 08:02:01 PM »
This code works:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include "main.h"

#define NELEMS(a)  (sizeof(a) / sizeof((a)[0]))


static HANDLE ghInstance;


void hwndDlg_OnClose(HWND hwnd)
{
 ExitProcess(0);
}

void hwndDlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
if(id==IDOK) {
HWND textedit = GetDlgItem(hwnd,4001);
HWND listbox = GetDlgItem(hwnd,4002);
char buf[25];
GetDlgItemText(hwnd,4001,buf,20);
SendMessage(listbox,LB_ADDSTRING,0,(LPARAM)buf);
}
}


BOOL CALLBACK MainDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
HANDLE_MSG (hwndDlg, WM_CLOSE, hwndDlg_OnClose);
HANDLE_MSG (hwndDlg, WM_COMMAND, hwndDlg_OnCommand);

default: return FALSE;
}
}


int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{

    WNDCLASSEX wcx;

    ghInstance = hInstance;


    /* Get system dialog information */
    wcx.cbSize = sizeof(wcx);
    if (!GetClassInfoEx(NULL, MAKEINTRESOURCE(32770), &wcx))
        return 0;

    /* Add our own stuff */
    wcx.hInstance = hInstance;
    wcx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_ICO_MAIN));
    wcx.lpszClassName = _T("testClass");
    if (!RegisterClassEx(&wcx))
        return 0;

    /* The user interface is a modal dialog box */
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)MainDlgProc);
}

I guess I must have that class registration there. Very strange...

Freddy

  • Guest
Re: Why doesn't my dialog show up?
« Reply #5 on: January 04, 2008, 12:04:22 AM »
If in rc file is CLASS keyword, remove it.

Thanks for the tip. It worked! ;)