943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6965
  • C++ RSS
Apr 9th, 2006
-1

Windows GUI - problem with dialog box

Expand Post »
Hi all

I'm experimenting with win API gui stuff, and I've run into a problem with dialog boxes. Currently, when the "Show DialogBox" option in the menu is pressed, the dialog box pops up.

I then need for this dialog box to be draggable/moveable, and to "hide" when its close button is pressed. (and then pop up again when requested etc.)

The problem is that when the dialog box pops up, I lose control over the initial window, as well as the new dialogbox window. The only thing that remains "active" is the button within the dialog box.

Any help would be much appreciated.


Quote ...
C++ Syntax (Toggle Plain Text)
  1. #include "windows.h"
  2. #define IDC_DIALOG1 9090
  3. #define IDC_HELLOBUTTON 9091
  4.  
  5. IDC_DIALOG1 DIALOGEX 0, 0, 100, 100
  6. STYLE WS_POPUP | WS_CAPTION
  7. EXSTYLE WS_EX_TOOLWINDOW
  8. CAPTION "Options Dialog"
  9. FONT 7, "MS Sans Serif"
  10. BEGIN
  11. PUSHBUTTON "&Hello World",IDC_HELLOBUTTON, 10,10,80,30
  12. END
Quote ...
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <iostream>
  3. #define IDC_DIALOG1 9090
  4. #define IDC_HELLOBUTTON 9091
  5. #define ID_OPTIONS_SHOW 9092
  6.  
  7. using namespace std;
  8.  
  9. /* Declare Windows procedure */
  10.  
  11.  
  12. HWND testDialog = NULL;
  13.  
  14. LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
  15. BOOL CALLBACK ToolDlgProc(HWND, UINT, WPARAM, LPARAM);
  16. /* Make the class name into a global variable */
  17. char szClassName[ ] = "WindowsApp";
  18. int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
  19.  
  20. {
  21. HWND hwnd; /* This is the handle for our window */
  22. MSG messages; /* Here messages to the application are saved */
  23. WNDCLASSEX wincl; /* Data structure for the windowclass */
  24.  
  25. /* The Window structure */
  26. wincl.hInstance = hThisInstance;
  27. wincl.lpszClassName = szClassName;
  28. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  29. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  30. wincl.cbSize = sizeof(WNDCLASSEX);
  31.  
  32. /* Use default icon and mouse-pointer */
  33. wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  34. wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  35. wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
  36. wincl.lpszMenuName = NULL; /* No menu */
  37. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  38. wincl.cbWndExtra = 0; /* structure or the window instance */
  39. /* Use light-gray as the background of the window */
  40. wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
  41.  
  42. /* Register the window class, if fail quit the program */
  43. if(!RegisterClassEx(&wincl)) return 0;
  44.  
  45. /* The class is registered, let's create the program*/
  46. hwnd = CreateWindowEx(
  47. 0, /* Extended possibilites for variation */
  48. szClassName, /* Classname */
  49. "Windows App", /* Title Text */
  50. WS_OVERLAPPEDWINDOW, /* default window */
  51. CW_USEDEFAULT, /* Windows decides the position */
  52. CW_USEDEFAULT, /* where the window ends up on the screen */
  53. 544, /* The programs width */
  54. 375, /* and height in pixels */
  55. HWND_DESKTOP, /* The window is a child-window to desktop */
  56. NULL, /* No menu */
  57. hThisInstance, /* Program Instance handler */
  58. NULL /* No Window Creation data */
  59. );
  60.  
  61. /* Make the window visible on the screen */
  62. ShowWindow(hwnd, nFunsterStil);
  63. /* Run the message loop. It will run until GetMessage( ) returns 0 */
  64. while(GetMessage(&messages, NULL, 0, 0))
  65. {
  66. /* Translate virtual-key messages into character messages */
  67. TranslateMessage(&messages);
  68. /* Send message to WindowProcedure */
  69. DispatchMessage(&messages);
  70. }
  71.  
  72. /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
  73. return messages.wParam;
  74. }
  75.  
  76. /* This function is called by the Windows function DispatchMessage( ) */
  77. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  78. {
  79. switch (message) /* handle the messages */
  80. {
  81. case WM_DESTROY:
  82. PostQuitMessage(0); /* send a WM_QUIT to the message queue */
  83. break;
  84.  
  85. case WM_COMMAND:
  86. {
  87. switch(LOWORD(wParam)) //contains the ID of the control that sent message
  88. {
  89. case ID_OPTIONS_SHOW:
  90. cout << "clicked options-show";
  91. ShowWindow(testDialog, SW_SHOW);
  92. }
  93. }
  94.  
  95.  
  96. case WM_CREATE:
  97. {
  98. HMENU hMenu, hSubMenu;
  99. hMenu = CreateMenu();
  100. hSubMenu = CreatePopupMenu();
  101. AppendMenu(hSubMenu, MF_STRING, ID_OPTIONS_SHOW, "&Show DialogBox");
  102. AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Options");
  103. SetMenu(hwnd, hMenu);
  104. testDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDC_DIALOG1),hwnd, ToolDlgProc);
  105. if(testDialog != NULL)
  106. ShowWindow(testDialog, SW_HIDE);
  107.  
  108. else
  109. cout << endl << "testDialog is null!" << endl;
  110.  
  111. }
  112. default: /* for messages that we don't deal with */
  113. return DefWindowProc(hwnd, message, wParam, lParam);
  114. }
  115. return 0;
  116. }
  117.  
  118.  
  119. BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  120. {
  121. switch (message) /* handle the messages */
  122. {
  123.  
  124. case WM_DESTROY:
  125. ShowWindow(testDialog, SW_HIDE); /* send a WM_QUIT to the message queue */
  126. break;
  127. case WM_COMMAND:
  128. switch(LOWORD(wParam))
  129. {
  130. case IDC_HELLOBUTTON:
  131. cout << endl << "You clicked the button!" << endl;
  132. }
  133. break;
  134.  
  135. default: /* for messages that we don't deal with */
  136. return DefWindowProc(hwnd, message, wParam, lParam);
  137. }
  138. return 0;
  139. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Apr 9th, 2006
0

Re: Windows GUI - problem with dialog box

... nevermind, got it working. :cheesy:
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Apr 9th, 2006
0

Re: Windows GUI - problem with dialog box

Well what was the problem?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Apr 9th, 2006
0

Re: Windows GUI - problem with dialog box

well ... mainly that I was calling DefWindowProc() from within the dialogbox's callback function, which messed things up. :-|
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
Jan 10th, 2008
0

Re: Windows GUI - problem with dialog box

I read somewhere, the real programmers are the C language programmers. I belive that given the structure of C but you also are a real programmer if you are actually using win API. Why not try MFC? Its alot easier.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
wamuti is offline Offline
23 posts
since Jan 2008

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: Please help me "I WANT IT FOR TOMORROW"
Next Thread in C++ Forum Timeline: Drawing on a Dialog Box.





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


Follow us on Twitter


© 2011 DaniWeb® LLC