本文介绍了无法在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) ;
}

推荐答案

wndclass.lpszMenuName  = NULL;



如下正确设置



give it correct as below

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



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

08-20 08:36
查看更多