无法在MDI窗口中获取菜单. [英] Not getting Menu in MDI window.

查看:58
本文介绍了无法在MDI窗口中获取菜单.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在应用程序中使用菜单和MDI,请提供帮助.


I am not getting menu and MDI working in my application Please help.


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) ;
}

推荐答案

您已将菜单名称指定为NULL
you have given menu name as NULL
wndclass.lpszMenuName  = NULL;



如下正确设置



give it correct as below

wndclass.lpszMenuName  = MAKEINTRESOURCE(IDC_MENU1/*resource name of your menu item*/);


我已经警告过您有关MDI的信息,但您可能没有注意.
谁需要MDI?为什么要折磨自己并吓users用户?
帮自己一个忙:完全不要使用MDI.没有设计,您可以更轻松地实现设计,并且质量更高.即使Microsoft也不鼓励使用MDI,实际上,Microsoft已将其从WPF中删除,并且几乎不支持它.更重要的是,如果您使用MDI,则会吓跑所有用户.只是不要.您可以更好地使用选项卡式UI,停靠UI,基于列表视图或菜单的导航,以及更多,更轻松,并且为您的用户所接受的质量.

请参阅:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [如何在WPF中创建MDI父窗口? [ ^ ],
和我过去的答案:
在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何最大程度地设置子表单,最小化最后一个子表单 [ ^ ].

祝你好运,
—SA
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


这篇关于无法在MDI窗口中获取菜单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆