Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am not getting menu and MDI working in my application Please help.


C++
include "windows.h"
#include "resource.h"

#define IDR_MENU1	2
#define IDR_MENU2	3
#define IDR_MENU3	3

#define IDM_FIRSTCHILD   50000

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL    CALLBACK CloseEnumProc (HWND, LPARAM) ;
LRESULT CALLBACK SystemInfoWndProc  (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK DriverInfoWndProc   (HWND, UINT, WPARAM, LPARAM) ;

HWND		hAddRuleDlg,hwnd,hWndStatus;

TCHAR		szAppName[] = TEXT ("Utility Application");
TCHAR		szFrameClass[] = TEXT ("Utility Application");
TCHAR		szSysClass[] = TEXT ("System Information");
TCHAR		szDriverClass[] = TEXT ("Driver Information") ;

HINSTANCE	hInst;

HMENU		hMenuInit, hMenuSys, hMenuDriver ;
HMENU       hMenuInitWindow, hMenuSysWindow, hMenuDriverWindow ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
	 HWND     hwnd, hwndClient ;
     MSG      msg ;
     WNDCLASS wndclass ;
     
	 hInst = hInstance;

	 // Frame Window Class
	 wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1) ;
     wndclass.lpszMenuName  = NULL; 
     wndclass.lpszClassName = szFrameClass ;
	      
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
	 //Register SystemInformation Window
	 wndclass.style         = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc   = SystemInfoWndProc;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof(HANDLE) ;
     wndclass.hInstance     = hInstance;
     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON2)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL; 
     wndclass.lpszClassName = szSysClass ;
	      
     RegisterClass (&wndclass);

	//Registring DriverInformation Window
	 wndclass.style         = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc   = DriverInfoWndProc;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof(HANDLE) ;
     wndclass.hInstance     = hInstance;
     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON3)) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL; 
     wndclass.lpszClassName = szDriverClass ;
	      
     RegisterClass (&wndclass);
	
     // Obtain handles to three possible menus & submenus
     
     hMenuInit  = LoadMenu (hInstance, TEXT ("MdiMenuInit")) ;
     hMenuSys = LoadMenu (hInstance, TEXT ("MdiMenuSys")) ;
     hMenuDriver  = LoadMenu (hInstance, TEXT ("MdiMenuDriver")) ;
     
     hMenuInitWindow  = GetSubMenu (hMenuInit,   IDR_MENU1) ;
     hMenuSysWindow = GetSubMenu (hMenuSys, IDR_MENU2) ;
     hMenuDriverWindow  = GetSubMenu (hMenuDriver,   IDR_MENU3) ;

	 //Creating Frame Window

	 hwnd = CreateWindow (szFrameClass, TEXT ("Utility Application"),
                          WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, hMenuInit, hInstance, NULL) ;

	 hwndClient = GetWindow (hwnd, GW_CHILD) ;

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
  
     while (GetMessage (&msg, NULL, 0, 0))
     {
		  if (hAddRuleDlg == 0 ||!IsDialogMessage (hAddRuleDlg, &msg) && !TranslateMDISysAccel (hwndClient, &msg))
          {
               TranslateMessage (&msg) ;
               DispatchMessage (&msg) ;
          }
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

	 static   HINSTANCE hInstance;    
     HMENU    hMenu ;
	 
     static HWND        hwndClient ;
     CLIENTCREATESTRUCT clientcreate ;
     HWND               hwndChild ;
     MDICREATESTRUCT    mdicreate ;

	 SYSTEMTIME st, lt;
	 GetLocalTime(<);
	 TCHAR szBuffer[40];

	 int iStatusWidths[] = {200,580,-1};
 	 switch (message)
     {

	 case WM_CREATE :

          hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
		   // Create the client window
          clientcreate.hWindowMenu  = hMenuInitWindow ;
          clientcreate.idFirstChild = IDM_FIRSTCHILD ;
          
          hwndClient = CreateWindow (TEXT ("MDICLIENT"), NULL,
                                     WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,   
                                     0, 0, 0, 0, hwnd, (HMENU) 1, hInst,
                                     (PSTR) &clientcreate) ;
		  
		 /* hWndStatus = CreateWindowEx(0L,								     // no extended styles
									  STATUSCLASSNAME,                       // status bar
									  NULL,                                  // no text 
									  WS_CHILD | WS_VISIBLE |SBARS_SIZEGRIP, // styles
									  0, 0, 0, 0,                            // x, y, cx, cy
									  hwnd,                                  // parent window
									  (HMENU)1000,                           // window ID
									  hInstance,                             // instance
									  NULL);                                 // window data

		  SendMessage(hWndStatus, SB_SETPARTS, 3, (LPARAM)iStatusWidths);
		  SendMessage(hWndStatus, SB_SETTEXT, 0,(LPARAM)(TEXT("Application started, no error detected.")));
		  wsprintf(szBuffer, TEXT("Date: %02d-%02d-%02d Time :%02d:%02d"), lt.wDay,lt.wMonth,lt.wYear,lt.wHour,lt.wMinute);
		  SendMessage(hWndStatus, SB_SETTEXT, 2,(LPARAM)szBuffer);
		  ShowWindow(hWndStatus, SW_SHOW);

		  if (hWndStatus == NULL)
				MessageBox (NULL, TEXT ("Error occured!"), szAppName, MB_OK );
		  break;*/
		  return 0;

      case WM_SIZE:
            SendMessage(hWndStatus, WM_SIZE, 0, 0);
		    return 0;

	  case WM_COMMAND:
          hMenu = GetMenu (hwnd) ;
          
          switch (LOWORD (wParam))
          {

		  case ID_FILE_CLOSE:          
               hwndChild = (HWND) SendMessage (hwndClient,
                                               WM_MDIGETACTIVE, 0, 0) ;
               
               if (SendMessage (hwndChild, WM_QUERYENDSESSION, 0, 0))
                    SendMessage (hwndClient, WM_MDIDESTROY,
                                 (WPARAM) hwndChild, 0) ;
               return 0 ;   

          case ID_FILE_EXIT:		//Exit the Application
               SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               return 0 ;
               
          case ID_TOOL_GETIPINFO:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, IpDlgProc) ;            
			   break;

		  case ID_TOOL_TCPCONNECTIONS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG2), hwnd, TcpDlgProc);
			   break;

		  case ID_TOOL_UDPCONNTECTIONS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG3), hwnd, UdpDlgProc);
			   break;

		  case ID_TOOL_TCPSTATISTICS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG4), hwnd, TcpStatDlgProc);
			   break;

		  case ID_TOOL_UDPSTATISTICS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG5), hwnd, UdpStatDlgProc);
			   break;

		  case ID_TOOL_IPSTATISTICS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG6), hwnd, IpStatDlgProc);
			   break;

		  case ID_TOOL_ICMPSTATISTICS:
			   DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG7), hwnd, IcmpStatDlgProc);
			   break;

	      case ID_INFORMATION_SYSTEMINFORMATION:		//System information child window
			   mdicreate.szClass = szSysClass ;
               mdicreate.szTitle = TEXT ("System Information") ;
               mdicreate.hOwner  = hInst ;
               mdicreate.x       = CW_USEDEFAULT ;
               mdicreate.y       = CW_USEDEFAULT ;
               mdicreate.cx      = CW_USEDEFAULT ;
               mdicreate.cy      = CW_USEDEFAULT ;
               mdicreate.style   = 0 ;
               mdicreate.lParam  = 0 ;
               
               hwndChild = (HWND) SendMessage (hwndClient,
                                   WM_MDICREATE, 0,
                                   (LPARAM) (LPMDICREATESTRUCT) &mdicreate) ;
			   return 0;

		  case ID_INFORMATION_DRIVERINFROMATION:		// Driver information child window
			   mdicreate.szClass = szDriverClass ;
               mdicreate.szTitle = TEXT ("Driver Information") ;
               mdicreate.hOwner  = hInst ;
               mdicreate.x       = CW_USEDEFAULT ;
               mdicreate.y       = CW_USEDEFAULT ;
               mdicreate.cx      = CW_USEDEFAULT ;
               mdicreate.cy      = CW_USEDEFAULT ;
               mdicreate.style   = 0 ;
               mdicreate.lParam  = 0 ;
               
               hwndChild = (HWND) SendMessage (hwndClient,
                                   WM_MDICREATE, 0,
                                   (LPARAM) (LPMDICREATESTRUCT) &mdicreate) ;
			   return 0;

		  case ID_WINDOW_TILE:
               SendMessage (hwndClient, WM_MDITILE, 0, 0) ;
               return 0 ;
               
          case ID_WINDOW_CASCADE:
               SendMessage (hwndClient, WM_MDICASCADE, 0, 0) ;
               return 0 ;
               
          case ID_WINDOW_ARRANGE:
               SendMessage (hwndClient, WM_MDIICONARRANGE, 0, 0) ;
               return 0 ;
               
          case ID_WINDOW_CLOSEALL:     // Attempt to close all children
               EnumChildWindows (hwndClient, CloseEnumProc, 0) ;
               return 0 ;

          case ID_HELP_HELP:
               MessageBox (hwnd, TEXT ("Help not yet implemented!"),
                           szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               return 0 ;
               
          case ID_HELP_ABOUT:
               MessageBox (hwnd, TEXT ("Project By\n\n")
                                 TEXT ("02] Amit Kumar Yadav\n03] Rahul Bandgar\n51] Taufiq Ahmed"),
                           szAppName, MB_ICONINFORMATION | MB_OK) ;
               return 0 ;

		 default:             // Pass to active child...
               
               hwndChild = (HWND) SendMessage (hwndClient,
                                               WM_MDIGETACTIVE, 0, 0) ;
               
               if (IsWindow (hwndChild))
                    SendMessage (hwndChild, WM_COMMAND, wParam, lParam) ;
               
               break ; 
          }
          return 0 ;

	 case WM_QUERYENDSESSION:
     case WM_CLOSE:                      // Attempt to close all children
               
          SendMessage (hwnd, WM_COMMAND, ID_WINDOW_CLOSEALL, 0) ;
               
          if (NULL != GetWindow (hwndClient, GW_CHILD))
               return 0 ;
               
          break ;   // i.e., call DefFrameProc
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
   // Pass unprocessed messages to DefFrameProc (not DefWindowProc)
     return DefFrameProc (hwnd, hwndClient, message, wParam, lParam) ;
}

BOOL CALLBACK CloseEnumProc (HWND hwnd, LPARAM lParam)
{
     if (GetWindow (hwnd, GW_OWNER))         // Check for icon title
          return TRUE ;
     
     SendMessage (GetParent (hwnd), WM_MDIRESTORE, (WPARAM) hwnd, 0) ;
     
     if (!SendMessage (hwnd, WM_QUERYENDSESSION, 0, 0))
          return TRUE ;
     
     SendMessage (GetParent (hwnd), WM_MDIDESTROY, (WPARAM) hwnd, 0) ;
     return TRUE ;
}

LRESULT CALLBACK SystemInfoWndProc (HWND hwnd, UINT message, 
                               WPARAM wParam, LPARAM lParam)
{
     static COLORREF clrTextArray[] = { RGB (0,   0, 0), RGB (255, 0,   0),
                                        RGB (0, 255, 0), RGB (  0, 0, 255),
                                        RGB (255, 255, 255) } ;
     static HWND     hwndClient, hwndFrame ;
     HDC             hdc ;
     HMENU           hMenu ;
     PAINTSTRUCT     ps ;
     RECT            rect ;
     
     switch (message)
     {
     case WM_CREATE:
          return 0 ;
          
     case WM_PAINT:
               // Paint the window
               
          hdc = BeginPaint (hwnd, &ps) ;
               
          GetClientRect (hwnd, &rect) ;
               
          DrawText (hdc, TEXT ("Hello, World!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
               
          EndPaint (hwnd, &ps) ;
          return 0 ;
               
     case WM_MDIACTIVATE:
               // Set the Hello menu if gaining focus
               
          if (lParam == (LPARAM) hwnd)
               SendMessage (hwndClient, WM_MDISETMENU,
                            (WPARAM) hMenuSys, (LPARAM) hMenuSysWindow) ;
               
                       
               // Set the Init menu if losing focus
               
          if (lParam != (LPARAM) hwnd)
               SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuInit,
                            (LPARAM) hMenuInitWindow) ;
               
          DrawMenuBar (hwndFrame) ;
          return 0 ;
               
     case WM_QUERYENDSESSION:
     case WM_CLOSE:
          if (IDOK != MessageBox (hwnd, TEXT ("OK to close window?"),
                                  TEXT ("Hello"), 
                                  MB_ICONQUESTION | MB_OKCANCEL))
               return 0 ;
               
          break ;   // i.e., call DefMDIChildProc
               
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
          // Pass unprocessed message to DefMDIChildProc
     
     return DefMDIChildProc (hwnd, message, wParam, lParam) ;
}

LRESULT CALLBACK DriverInfoWndProc (HWND hwnd, UINT message, 
                              WPARAM wParam, LPARAM lParam)
{
     static HWND hwndClient, hwndFrame ;
     HBRUSH      hBrush ;
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT            rect ;
     int         xLeft, xRight, yTop, yBottom ;
     short       nRed, nGreen, nBlue ;
     
     switch (message)
     {
     case WM_CREATE:
          return 0 ;
          
           
     case WM_PAINT:            // Clear the window
               
          hdc = BeginPaint (hwnd, &ps) ;
               
          GetClientRect (hwnd, &rect) ;
               
          DrawText (hdc, TEXT ("Hello, World!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
               
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_MDIACTIVATE:      // Set the appropriate menu
          if (lParam == (LPARAM) hwnd)
               SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuDriver,
                            (LPARAM) hMenuDriverWindow) ;
          else
               SendMessage (hwndClient, WM_MDISETMENU, (WPARAM) hMenuInit,
                            (LPARAM) hMenuInitWindow) ;
          
          DrawMenuBar (hwndFrame) ;
          return 0 ;
          
     case WM_DESTROY:
	       PostQuitMessage (0) ;
          return 0 ;
     }
          // Pass unprocessed message to DefMDIChildProc
     
     return DefMDIChildProc (hwnd, message, wParam, lParam) ;
}
Posted

you have given menu name as NULL
C++
wndclass.lpszMenuName  = NULL;


give it correct as below

C++
wndclass.lpszMenuName  = MAKEINTRESOURCE(IDC_MENU1/*resource name of your menu item*/);
 
Share this answer
 
Comments
amityadav4a 10-Apr-12 9:22am    
No it is not working
I already warned you about MDI, but you probably did not pay attention.

Who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. You can better use tabbed UI, docking UI, navigation based on list view or menu and a lot more, much easier for you and with acceptable quality for your users.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^].

Please also see this answer:
How to Create MDI Parent Window in WPF?[^],
and my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

Good luck,
—SA
 
Share this answer
 
Comments
amityadav4a 9-Apr-12 17:05pm    
Can you help me in Doing it in MDI. I have assignment on it as a college work. Have no other option than to use MDI.

Please check once and let me know what is wrong. I guess there is something wrong with Menu.
Sergey Alexandrovich Kryukov 9-Apr-12 17:14pm    
Sorry, no. I don't do bad things, so since some experience a long time ago I never tried it again. And yes, you do have other options. If someone in your college tries to force you to do that, you have a wrong teacher.

However, you problem might be unrelated to it, but you did not report what exactly is happening.

The menu is not shown or what? And let me ask you, how do you develop? Do you put everything in your target application, or you do a set of miniature test projects? If you don't do the later, you are doing it wrong. You need to try menu technique separately, on a smallest possible application. Do one step at a time. Do you run it under debugger? You can see which part of code is executed and which is not...

--SA
amityadav4a 9-Apr-12 17:32pm    
Yes menu is not showing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900