View Full Version: My program's been buggin me for an hour

C++ Learning Community > win32 api C++ programming > My program's been buggin me for an hour


Title: My program's been buggin me for an hour
Description: *solved, nvm*


biggoron - December 21, 2006 10:36 PM (GMT)
[edit]
Never mind, turns out unchecking "Support Windows XP Themes" fixes it...
[/edit]

I can compile my program fine, but it doesn't work at all the way it's supposed to. When I press "File -> Dialog -> Get integer" it should run the dialog I've set up but it doesn't. It acts as if it runs it and then exits straight away (this also sometimes happens with MessageBox). I think it appeared because I can hear the Windows ding sounds when I press the menu button.

I think the three possible areas I could've gone wrong are in the dialog's code in the resource file, in the dialog's callback procedure or in my window's callback procedure where the dialog is created.

This is the part of my resource file concerning the dialog:
CODE
IDD_GETINTEGER DIALOG DISCARDABLE 0, 0, 300, 100
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Enter a number"
FONT 8, "MS Sans Serif"
{
   DEFPUSHBUTTON "&Ok", IDOK, 10, 70, 100, 16
   PUSHBUTTON "&Cancel", IDCANCEL, 180, 70, 100, 16
   EDITTEXT IDC_EDITTEXT, 10, 10, 280, 16
}
This is the dialog's callback
CODE
BOOL CALLBACK GetIntegerProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
   switch(Message) {
       case WM_INITDIALOG:
           return TRUE;
   
       case WM_COMMAND:
           switch(LOWORD(wParam)) {
               case IDOK:
                   EndDialog(hwnd, IDOK);
                   break;
                   
               case IDCANCEL:
                   EndDialog(hwnd, IDCANCEL);
                   break;
           }
           break;
           
       default:
           return FALSE;
   }
   return TRUE;
}
And this is my window's callback
CODE
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
   switch(msg) {        
       case WM_COMMAND:
           switch(LOWORD(wParam)) {              
               case ID_FILE_EXIT:
                   SendMessage(hwnd, WM_CLOSE, 0, 0);
                   break;
                   
               case ID_FILE_GETINT: {
                   int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_GETINTEGER), hwnd, GetIntegerProc);
                   
                   if(ret == IDOK) { MessageBox(hwnd, "IDOK", "notice", MB_OK | MB_ICONINFORMATION); }
                   else if(ret == IDCANCEL) { MessageBox(hwnd, "IDCANCEL", "notice", MB_OK | MB_ICONINFORMATION); }
                   else if(ret == -1) { MessageBox(hwnd, "ERROR", "notice", MB_OK | MB_ICONINFORMATION); }
               }
           }
           break;
           
       case WM_CLOSE:
           DestroyWindow(hwnd);
           break;
           
       case WM_DESTROY:
           PostQuitMessage(0);
           break;
           
       default:
           return DefWindowProc(hwnd, msg, wParam, lParam);
   }
   
   return 0;
}
And here's the WinMain 'cause that's probably important too :)
CODE
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
   WNDCLASSEX wc;
   HWND hwnd;
   MSG Msg;
   
   // Register the window class
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = 0;
   wc.lpfnWndProc = WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInstance;
   wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
   wc.hCursor = LoadCursor(GetModuleHandle(NULL), IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
   wc.lpszClassName = g_szClassName;
   wc.hIconSm = LoadIcon(GetModuleHandle(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,
       "The title of my window",
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
       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;
}


Sorry for the long message. Anyway, I've been looking through this for about an hour, checking it with tutorials, my own knowledge and MSDN but I can't find anything wrong. What's more is that it worked when I first compiled it but not anymore, just like when MessageBox sometimes works but other times it doesn't.

Thanks.

adeyblue - December 22, 2006 12:48 AM (GMT)
One thing I see is that your dialog is created but never shown. You need to add WS_VISIBLE to the STYLE section of your dialog resource or call ShowWindow in the WM_INITDIALOG part of GetIntegerProc. You might also need to give your EDITTEXT control the WS_VISIBLE style too to get that showing. Another thing is the return type of GetIntegerProc should be INT_PTR not BOOL.

Oh I see you fixed it, but unchecking that option just happens to work, it doesn't fix the code.

biggoron - December 22, 2006 02:26 PM (GMT)
So that was what was causing it? The procedure returned the wrong value and the dialog wasn't made visible? Either way, doing what you said worked. Thanks :)

[edit]
"-_- Turns out I was including the private .rc of the project in the compilation. That was causing a ton of problems too...




Hosted for free by InvisionFree