User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,077 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,066 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Nov 4th, 2004
Views: 13,641
The snippet shows how to create a menu on a windows form and test it. Original code via BCX, modified to compile with Dev C++ as a Windows Application. This approach does speed up writing of GUI programs.
cplusplus Syntax | 4 stars
  1. // create a basic windows form with a menu
  2. // modified from BCX generated C code for Dev-C++
  3. // a Dev-C++ tested Windows Application by vegaseat 04nov2004
  4.  
  5. #include <windows.h>
  6.  
  7. #define ID_Exit 101
  8. #define ID_Edit 201
  9. #define ID_Load 301
  10. #define ID_Save 302
  11.  
  12. static HINSTANCE BCX_hInstance;
  13. static int BCX_ScaleX;
  14. static int BCX_ScaleY;
  15. static char BCX_ClassName[2048]; // default size
  16. static HMENU MainMenu; // has to be HMENU type
  17. static HMENU FileMenu; // dito
  18. static HWND Form1;
  19.  
  20. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  21.  
  22. HWND BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
  23.  
  24. void FormLoad (void);
  25. BOOL AddMenu (HWND);
  26. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  27.  
  28.  
  29. // this is the main() function under Windows GUI
  30. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
  31. {
  32. WNDCLASS Wc;
  33. MSG Msg;
  34. // *****************************
  35. strcpy(BCX_ClassName,"FORM3");
  36. // ************************************
  37. // Scale Dialog Units To Screen Units
  38. // (pixels have not been specified here)
  39. // ************************************
  40. RECT rc = {0,0,4,8};
  41. MapDialogRect (NULL,&rc);
  42. BCX_ScaleX = rc.right/2;
  43. BCX_ScaleY = rc.bottom/4;
  44. BCX_hInstance = hInst;
  45. // ******************************************************
  46. Wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  47. Wc.lpfnWndProc = WndProc;
  48. Wc.cbClsExtra = 0;
  49. Wc.cbWndExtra = 0;
  50. Wc.hInstance = hInst;
  51. Wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
  52. Wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  53. Wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  54. Wc.lpszMenuName = NULL;
  55. Wc.lpszClassName = BCX_ClassName;
  56. RegisterClass(&Wc);
  57.  
  58. FormLoad();
  59. while(GetMessage(&Msg,NULL,0,0)) {
  60. HWND hActiveWindow = GetActiveWindow();
  61. if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg)) {
  62. TranslateMessage(&Msg);
  63. DispatchMessage(&Msg);
  64. }
  65. }
  66. return Msg.wParam;
  67. }
  68.  
  69.  
  70. // create the windows form in style selected
  71. HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
  72. {
  73. HWND A;
  74. if (!Style) {
  75. Style= WS_MINIMIZEBOX |
  76. WS_SIZEBOX |
  77. WS_CAPTION |
  78. WS_MAXIMIZEBOX |
  79. WS_POPUP |
  80. WS_SYSMENU;
  81. }
  82. A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
  83. Style,
  84. X*BCX_ScaleX,
  85. Y*BCX_ScaleY,
  86. (4+W)*BCX_ScaleX,
  87. (12+H)*BCX_ScaleY,
  88. NULL,(HMENU)NULL,BCX_hInstance,NULL);
  89. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  90. (LPARAM)MAKELPARAM(FALSE,0));
  91. return A;
  92. }
  93.  
  94.  
  95. // the details like upper left corner coordinates, width, height ...
  96. // are in Dialog Units
  97. void FormLoad (void)
  98. {
  99. Form1=BCX_Form("A Form with a menu ...",20,10,200,100);
  100. AddMenu(Form1);
  101. Show(Form1);
  102. }
  103.  
  104.  
  105. BOOL AddMenu (HWND hwndOwner)
  106. {
  107. MainMenu=CreateMenu();
  108. FileMenu=CreateMenu();
  109. InsertMenu(MainMenu,ID_Edit,MF_POPUP,(UINT)FileMenu,"File");
  110. AppendMenu(FileMenu,MF_STRING,ID_Load,"&Load");
  111. AppendMenu(FileMenu,MF_STRING,ID_Save,"&Save");
  112. AppendMenu(FileMenu,MF_SEPARATOR,0,"");
  113. AppendMenu(FileMenu,MF_STRING,ID_Exit,"E&xit");
  114. // activate menu
  115. if (!SetMenu(hwndOwner,MainMenu)) {
  116. return FALSE;
  117. }
  118. return TRUE;
  119. }
  120.  
  121.  
  122. // standard windows message handler
  123. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  124. {
  125. while(1) {
  126. if (Msg==WM_COMMAND) {
  127. if (LOWORD(wParam)==ID_Exit) {
  128. PostQuitMessage(0);
  129. }
  130. // do something with other menu items here,
  131. // normally one calls up a file load/save dialog box
  132. if (LOWORD(wParam)==ID_Load) {
  133. SetWindowText(Form1,"Load ...");
  134. }
  135. if (LOWORD(wParam)==ID_Save) {
  136. SetWindowText(Form1,"Save ...");
  137. }
  138. return 0;
  139. break;
  140. }
  141. break;
  142. }
  143. // tidy up and exit the program via the form's upper left corner x
  144. if (Msg==WM_DESTROY) {
  145. UnregisterClass(BCX_ClassName,BCX_hInstance);
  146. PostQuitMessage(0);
  147. }
  148. return DefWindowProc(hWnd,Msg,wParam,lParam);
  149. }
  150.  
  151. // *************************************************************
  152. // Created with BCX -- The BASIC To C Translator (ver 5.02)
  153. // BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
  154. // *************************************************************
  155.  
Comments (Newest First)
vegaseat | Kickbutt Moderator | Dec 6th, 2004
For those who need some hand holding with the Dev C++ IDE:
In the IDE go to FILE, then NEW, then Project, select Windows Application, give it a name (eg. Menu1) click OK A filesave dialog box comes up, create a new folder and save Menu1.dev there. The DevCpp IDE comes up with a template, select and delete that and cut and paste this code into the empty editor page. Now compile and run.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 12:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC