-
CreateDialog
Why is this CreateDialog not working?
IDD_DIALOG1 is the name of the dialog box I created with the visual editor
Code:
#include <windows.h>
#define IDD_DIALOG1 101
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK DialogBoxProc (HWND, UINT, WPARAM, LPARAM) ;
HWND MainDialogBox=NULL ;
HINSTANCE hInstance ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, SW_SHOWMAXIMIZED) ;
UpdateWindow (hwnd) ;
MainDialogBox=CreateDialog (hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
hwnd, DialogBoxProc) ;
if(MainDialogBox != NULL)
{
ShowWindow(MainDialogBox, SW_SHOW);
}
else
{
MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",
MB_OK | MB_ICONINFORMATION);
}
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
if(!IsDialogMessage(MainDialogBox, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
int x;
// int Width, Height;
switch (message)
{
case WM_CREATE:
ShowWindow(MainDialogBox, SW_SHOW);
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK DialogBoxProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG :
return TRUE ;
case WM_COMMAND :
switch (LOWORD (wParam))
{
//case IDOK :
//case IDCANCEL :
// EndDialog (hDlg, 0) ;
// return TRUE ;
}
break ;
}
return FALSE ;
}
Sorry I haven't been coding in win32 for long (it's obvious isn't it?
-
Cast your callback function to a DLGPROC like this:
Code:
MainDialogBox=CreateDialog (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, (DLGPROC)DialogBoxProc) ;
Should be fine.. or you can use the C++ static-cast if you like
-
why do you have to cast it? BTW it is still not working, I'll just start over.....
-
If you obtain an error number with GetLastError() life is much easier.
Code:
#define IDD_DIALOG1 101
Doies this also occur at the top of the rc file? Typically this goes in a header file which you #include into both the c file and the rc file.
If IDD_DIALOG1 is not defined to a number in the rc file it is taken as the name so you can try:
Code:
CreateDialog (hInstance, "IDD_DIALOG1", hwnd, DialogBoxProc) ;
-
Well I kind of assumed you had a resource file and header with the appropriate information in it. I simply created my own with your driver and it did not work. Casting to the DLGPROC then made it work because as it stood it was not of the correct type. Make sure you create your resource correctly.
-
I am getting this error
NuevoClienteWindow = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE (IDD_DIALOG1), hwnd, NuevoCliente);
ShowWindow(NuevoClienteWindow,SW_SHOW);
1400 (Invalid Windows Handle)
-
I think that maybe I am forgetting some settings on the visual editor, because if I create a new one without many changes to it and try load it it works, but if I make any changes whatsoever to it in the visual editor it doesn't work. I am relatively new to this visual editor stuff.
-
What do you mean by doesn't work. Do you mean the changes aren't there or it the function call begins to fail? If it is the latter that is very strange. If the changes aren't taking place then you need to make sure you are saving the resource. You can also try a "rebuild all" and see if that helps. Really, making changes should not be that big of a deal.
-
Sometimes when I create a new dialog box the default one, the small one works, just whenever it feels like it. But after it is created if I make some changes to it ie. Add a new static text or edit box, then the call to CreateDialog fails and I get error code 1400 Invalid windows handle.
-