944,017 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12454
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 24th, 2006
0

WIN32 GUI application - Problem popping up dialog box

Expand Post »
Hi all

I have been following a tutorial on creating win32 GUI applications. I'm having a problem getting a dialog box to pop up on top of my main window ...the CreateDialog() method returns a NULL for some reason.

Would really appreciate some help with this. Here is a zip file containing the main CPP and as well as the resource file.

http://www.apcx.3rror.com/test.zip

(the program runs, but the test condition for CreateDialog()'s return value shows a NULL value is returned)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Feb 24th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

http://www.apcx.3rror.com/main.cpp


The resource file:
Quote ...
#include "windows.h"
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "&Press This Button",IDC_PRESS,7,7,84,14
PUSHBUTTON "&Or This One",IDC_OTHER,7,31,84,14
END


Window Procedure of the main window...
Quote ...
C++ Syntax (Toggle Plain Text)
  1. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  2. {
  3. switch (message)
  4. {
  5.  
  6. case WM_CREATE:
  7. {
  8. hDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
  9. if(hDialog != NULL)
  10. {
  11. ShowWindow(hDialog, SW_SHOW);
  12. }
  13. else
  14. {
  15. MessageBox(hwnd, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
  16. }
  17. }
  18. break;
  19. //handle other messages...
  20. }
...where the (HWND) hDialog is declared as global variable.


Message Handling function of the dialog box...
Quote ...
C++ Syntax (Toggle Plain Text)
  1. BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  2. {
  3. switch(Message)
  4. {
  5. case WM_INITDIALOG:
  6. return TRUE;
  7. case WM_COMMAND:
  8. switch(LOWORD(wParam))
  9. {
  10. case IDC_PRESS:
  11. MessageBox(hwnd, "Hi!", "This is a message", MB_OK | MB_ICONEXCLAMATION);
  12. break;
  13. case IDC_OTHER:
  14. MessageBox(hwnd, "Bye!", "This is also a message", MB_OK | MB_ICONEXCLAMATION);
  15. break;
  16. }
  17. break;
  18. default:
  19. return FALSE;
  20. }
  21. return TRUE;
  22. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Feb 24th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

Try putting all the definitions,
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2.  
  3. #define ID_FILE_EXIT 9001
  4. #define ID_STUFF_GO 9002
  5. #define ID_STUFF_ABOUT 9003
  6. #define IDD_TOOLBAR 9004
  7. #define IDC_PRESS 9005
  8. #define IDC_OTHER 9006
  9. #define ID_DIALOG_SHOW 9007
  10. #define ID_DIALOG_HIDE 9008
  11. #define ID_HELP_ABOUT 9009
  12. #define IDD_ABOUT 9010
to a resource.h file and including the resource.h file to both your .rc file and main.cpp file. I dont know if the compiler allows the present rc file name. It hasn't got a file name. Only an extention. Correct that also.

What is the compiler you are using. Is it a IDE or command line compiler?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 25th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

Hi Wolfpack

I'm using the Dev-Cpp IDE (I think it's the MinGW compiler that comes along with dev-cpp).

I tried the above, CreateDialogBox() still returns a NULL...
Is there an error in the contents of my .rc file?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Feb 25th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

There was nothing wrong with your rc file. I am using the Visual Studio IDE, and here is what I did to make the project compile.

1. Rename .rc to main.rc
2. Create a new file resource.h
3. Put the
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2.  
  3. #define ID_FILE_EXIT 9001
  4. #define ID_STUFF_GO 9002
  5. #define ID_STUFF_ABOUT 9003
  6. #define IDD_TOOLBAR 9004
  7. #define IDC_PRESS 9005
  8. #define IDC_OTHER 9006
  9. #define ID_DIALOG_SHOW 9007
  10. #define ID_DIALOG_HIDE 9008
  11. #define ID_HELP_ABOUT 9009
  12. #define IDD_ABOUT 9010
of your main.cpp file to the resource.h file.
4. Added
C++ Syntax (Toggle Plain Text)
  1. #include "resource.h"
to your main.cpp file and main.rc file.
5. Added the above 3 files to a Visual Studio Solution and built it..

After that the program ran okay. I dont know the procedure for dev-Cpp. But I think you will have to do the above 4 steps for any compiler. Try it and tell the result. Only the way you compile; step 5, will change for Dev-Cpp. Here are the modified files.
Attached Files
File Type: zip GUI.zip (4.6 KB, 115 views)
Last edited by WolfPack; Feb 25th, 2006 at 4:20 am. Reason: Added new files
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 25th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

ah ... it runs fine in Dev-cpp. Thank you SO much, Wolfpack.

Just one more question. I'm creating this program to do some bitmap displaying/manipulation ... so I'd like to have another window (a blank one with no windows style/border and NO menu) pop up over this window.


So right after this bit of code ...

Quote ...
C++ Syntax (Toggle Plain Text)
  1. WNDCLASSEX wincl; /* Data structure for the windowclass */
  2.  
  3. /* The Window structure */
  4.  
  5.  
  6.  
  7. wincl.hInstance = hThisInstance;
  8. wincl.lpszClassName = szClassName;
  9. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  10. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  11. wincl.cbSize = sizeof (WNDCLASSEX);
  12.  
  13. /* Use default icon and mouse-pointer */
  14. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  15. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  16. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  17. wincl.lpszMenuName = NULL; /* No menu */
  18. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  19. wincl.cbWndExtra = 0; /* structure or the window instance */
  20. /* Use Windows's default color as the background of the window */
  21. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  22.  
  23.  
  24. /* Register the window class, and if it fails quit the program */
  25. if (!RegisterClassEx (&wincl))
  26. return 0;
  27.  
  28. /* The class is registered, let's create the program*/
  29. hwnd = CreateWindowEx (
  30. 0, /* Extended possibilites for variation */
  31. szClassName, /* Classname */
  32. "Windows App", /* Title Text */
  33. WS_OVERLAPPEDWINDOW, /* default window */
  34. CW_USEDEFAULT, /* Windows decides the position */
  35. CW_USEDEFAULT, /* where the window ends up on the screen */
  36. 544, /* The programs width */
  37. 375, /* and height in pixels */
  38. HWND_DESKTOP, /* The window is a child-window to desktop */
  39. NULL, /* No menu */
  40. hThisInstance, /* Program Instance handler */
  41. NULL /* No Window Creation data */
  42. );
... I tried the following:
1.
wincl.lpfnWndProc = ImageWindowProc();
registered wincl
created a new HWND imageHwnd using CreateWindowEx()
then used the ShowWindow method to display this new window as well.


2.
created a new WNDCLASSEX -- wincl2.
After the creation of the main window:
initialized wincl2
registered wincl2
created a new HWND imageHwnd using CreateWindowEx()
used ShowWindow method to display this new window as well.

The problem is that in both of the above cases, the new window (whether it is the child is created as the child window of the main window or not) ... appears with the same menu that was added to the main window. The new window appears to share the main window's WindowProcedure method, even though I have tried to set it otherwise.


(My ImageWindowProc does NOT yet respond to any messages, e.g. display menu, dialogbox etc.)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Feb 25th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

I dont know what you are trying to do. But try replacing your main file with the one I have attached. It is a skeleton window procedure. It worked for me.No problems with menus or whatever.
Attached Files
File Type: cpp main.cpp (7.6 KB, 72 views)
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Feb 25th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

Quote ...
Return Value

If the function succeeds, the return value is the handle to the dialog box.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Try using GetLastError() function.
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005
Feb 26th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

Hey guys... thanks for the replies

Yesterday, I used the main.cpp that Wolfpack included in GUI.zip ... everything worked fine, and the dialog box popped up.

Today I had to reinstall dev-cpp ... I tried compiling and running the same main.cpp but the dialogbox won't show up (a NULL value is returned again).
Everything is the same, except that I had to create a new resource file, with the same information, but this resource file did compile successfully.

I used GetLastError() ... it printed the value 1813.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Feb 26th, 2006
0

Re: WIN32 GUI application - Problem popping up dialog box

Code :1813
Error :The specified resource type cannot be found in the image file.
Const :ERROR_RESOURCE_TYPE_NOT_FOUND
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
AhmedHan is offline Offline
71 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: get a random word
Next Thread in C++ Forum Timeline: First time posting...syntax error...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC