菜单可能是Windows程序提供的一致的用户界面中最重要的部分,而想程序中添加菜单是Windows编程中相对容易的布冯。当用户选择菜单单选时,Windows向你的程序发送了一个含有才菜单单选ID的WM_COMMAND消息

 

LOWORD(wParam)控件ID
HIWORD(wParam)通知码
lParam子窗口句柄

 

 

创建菜单有三种方法;

用WNDCLASS定义

[cpp] view plaincopyprint?

  1. wndclass.lpszMenuName=szAppName;//I don't konw what the fucking is?在VC++6.0和VS2008竟然不显示  

wndclass.lpszMenuName=szAppName;//I don't konw what the fucking is?在VC++6.0和VS2008竟然不显示


2使用LoadMenu获取hMenu,然后在CreateWindow倒数第三个参数写上hMenu

[cpp] view plaincopyprint?

  1. hMenu=LoadMenu(hInstance,TEXT("MyMenu"));  

  2. hwnd=CreateWindow(//倒数第三个参数hMenu)  

hMenu=LoadMenu(hInstance,TEXT("MyMenu"));
hwnd=CreateWindow(//倒数第三个参数hMenu)


3第三种方法和第二种方法一样:

[cpp] view plaincopyprint?

  1. hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUID));  

  2. hwnd=CreateWindow(//倒数第三个参数hMenu);  

hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUID));
hwnd=CreateWindow(//倒数第三个参数hMenu);


 

具体步骤跟创建自己的ICON差不多;
 1新建Windows32项目,编写好自己的.cpp文件
 2在项目名称那里右键添加资源(.rc),然后resource.h头文件也会自己创建
 3在Resource文件夹右键,添加资源(Menu)

 4然后在图形界面里面编辑选项和ID,重命名就行了
 5运行


下面看一段简单的程序:

[cpp] view plaincopyprint?

  1. #include<windows.h>   

  2. #include"resource.h"   

  3.   

  4. LRESULT CALLBACK WindowProc(  

  5.   HWND hwnd,      // handle to window   

  6.   UINT uMsg,      // message identifier   

  7.   WPARAM wParam,  // first message parameter   

  8.   LPARAM lParam   // second message parameter   

  9. );  

  10.   

  11. int WINAPI WinMain(  

  12.   HINSTANCE hInstance,      // handle to current instance   

  13.   HINSTANCE hPrevInstance,  // handle to previous instance   

  14.   LPSTR lpCmdLine,          // command line   

  15.   int nCmdShow              // show state   

  16. )  

  17. {  

  18.     static TCHAR szAppName[]=TEXT("MENUDEMO");  

  19.     HWND hwnd;  

  20.     WNDCLASS wndclass;  

  21.     MSG msg;  

  22.     HMENU hMenu;  

  23.   

  24.     wndclass.cbClsExtra=0;  

  25.     wndclass.cbWndExtra=0;  

  26.     wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);  

  27.     wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);  

  28.     wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);  

  29.     wndclass.hInstance=hInstance;  

  30.     wndclass.lpfnWndProc=WindowProc;  

  31.     wndclass.lpszClassName=szAppName;  

  32.     wndclass.lpszMenuName=NULL;  

  33.     wndclass.style=CS_HREDRAW|CS_VREDRAW;  

  34.   

  35.     if(!RegisterClass(&wndclass))  

  36.     {  

  37.         MessageBox(NULL,TEXT("the program require the window nt"),TEXT("TIPS "),MB_ICONERROR);  

  38.         return 0;  

  39.     }  

  40.       

  41.     hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUDEMO));  

  42.   

  43.     hwnd=CreateWindow(  

  44.       szAppName,  // registered class name   

  45.       TEXT("this is title"), // window name   

  46.       WS_OVERLAPPEDWINDOW,        // window style   

  47.       CW_USEDEFAULT,                // horizontal position of window   

  48.       CW_USEDEFAULT,                // vertical position of window   

  49.       CW_USEDEFAULT,           // window width   

  50.       CW_USEDEFAULT,          // window height   

  51.       NULL,      // handle to parent or owner window   

  52.       hMenu,          // menu handle or child identifier   

  53.       hInstance,  // handle to application instance   

  54.       NULL       // window-creation data   

  55. );  

  56.   

  57.     //SetMenu(hwnd,hMenu);   

  58.     ShowWindow(hwnd,nCmdShow);  

  59.     UpdateWindow(hwnd);  

  60.   

  61.     while(GetMessage(&msg,NULL,0,0))  

  62.     {  

  63.         TranslateMessage(&msg);  

  64.         DispatchMessage(&msg);  

  65.     }  

  66.   

  67.     return msg.wParam;  

  68. }  

  69.   

  70. LRESULT CALLBACK WindowProc(  

  71.   HWND hwnd,      // handle to window   

  72.   UINT uMsg,      // message identifier   

  73.   WPARAM wParam,  // first message parameter   

  74.   LPARAM lParam   // second message parameter   

  75. )  

  76. {  

  77.     HDC hdc;  

  78.     PAINTSTRUCT ps;  

  79.     RECT rect;  

  80.   

  81.     switch(uMsg)  

  82.     {  

  83.     case WM_CREATE:  

  84.         return 0;  

  85.   

  86.     case WM_PAINT:  

  87.         GetClientRect(hwnd,&rect);  

  88.         hdc=BeginPaint(hwnd,&ps);  

  89.           

  90.         EndPaint(hwnd,&ps);  

  91.         return 0;  

  92.   

  93.     case WM_COMMAND:  

  94.         switch(LOWORD(wParam))  

  95.         {  

  96.         case ID_FILE_NEW:  

  97.             MessageBox(NULL,TEXT("你选择了new"),TEXT("提示"),MB_OK);  

  98.             break;  

  99.               

  100.         case ID_FILE_EXIT:  

  101.             MessageBeep(MB_ICONEXCLAMATION);  

  102.             SendMessage(hwnd,WM_CLOSE,0,0);  

  103.             break;  

  104.   

  105.         case ID_FILE_OPEN:  

  106.             MessageBox(NULL,TEXT("你选择了Open"),TEXT("提示"),MB_OK);  

  107.             break;  

  108.   

  109.         case ID_FILE_SAVE:  

  110.             MessageBox(NULL,TEXT("你选择了Save"),TEXT("提示"),MB_OK);  

  111.             break;  

  112.   

  113.         case ID_FILE_SAVEAS:  

  114.             MessageBox(NULL,TEXT("你选择了Save_As"),TEXT("提示"),MB_OK);  

  115.             break;  

  116.         }  

  117.         return 0;  

  118.   

  119.     case WM_DESTROY:  

  120.         PostQuitMessage(0);  

  121.         return 0;  

  122.     }  

  123.   

  124.     return DefWindowProc(hwnd,uMsg,wParam,lParam);  

  125. }  

#include<windows.h>
#include"resource.h"

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	static TCHAR szAppName[]=TEXT("MENUDEMO");
	HWND hwnd;
	WNDCLASS wndclass;
	MSG msg;
	HMENU hMenu;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WindowProc;
	wndclass.lpszClassName=szAppName;
	wndclass.lpszMenuName=NULL;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("the program require the window nt"),TEXT("TIPS "),MB_ICONERROR);
		return 0;
	}
	
	hMenu=LoadMenu(hInstance,MAKEINTRESOURCE(MENUDEMO));

	hwnd=CreateWindow(
	  szAppName,  // registered class name
	  TEXT("this is title"), // window name
	  WS_OVERLAPPEDWINDOW,        // window style
	  CW_USEDEFAULT,                // horizontal position of window
	  CW_USEDEFAULT,                // vertical position of window
	  CW_USEDEFAULT,           // window width
	  CW_USEDEFAULT,          // window height
	  NULL,      // handle to parent or owner window
	  hMenu,          // menu handle or child identifier
	  hInstance,  // handle to application instance
	  NULL       // window-creation data
);

	//SetMenu(hwnd,hMenu);
	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch(uMsg)
	{
	case WM_CREATE:
		return 0;

	case WM_PAINT:
		GetClientRect(hwnd,&rect);
		hdc=BeginPaint(hwnd,&ps);
		
		EndPaint(hwnd,&ps);
		return 0;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_FILE_NEW:
			MessageBox(NULL,TEXT("你选择了new"),TEXT("提示"),MB_OK);
			break;
			
		case ID_FILE_EXIT:
			MessageBeep(MB_ICONEXCLAMATION);
			SendMessage(hwnd,WM_CLOSE,0,0);
			break;

		case ID_FILE_OPEN:
			MessageBox(NULL,TEXT("你选择了Open"),TEXT("提示"),MB_OK);
			break;

		case ID_FILE_SAVE:
			MessageBox(NULL,TEXT("你选择了Save"),TEXT("提示"),MB_OK);
			break;

		case ID_FILE_SAVEAS:
			MessageBox(NULL,TEXT("你选择了Save_As"),TEXT("提示"),MB_OK);
			break;
		}
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}


效果如下

windows WIN32菜单_windows

 

还有一段:

[cpp] view plaincopyprint?

  1. /*----------------------------------------- 

  2.    MENUDEMO.C -- Menu Demonstration 

  3.                  (c) Charles Petzold, 1998 

  4.   -----------------------------------------*/  

  5.    

  6. #include <windows.h>   

  7. #include "resource.h"   

  8.   

  9. #define ID_TIMER 1   

  10.   

  11. LRESULT CALLBACK WndProc (HWNDUINTWPARAMLPARAM) ;  

  12.   

  13. TCHAR szAppName[] = TEXT ("MenuDemo") ;  

  14.   

  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,  

  16.                     PSTR szCmdLine, int iCmdShow)  

  17. {  

  18.      HWND     hwnd ;  

  19.      MSG      msg ;  

  20.      WNDCLASS wndclass ;  

  21.        

  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;  

  23.      wndclass.lpfnWndProc   = WndProc ;  

  24.      wndclass.cbClsExtra    = 0 ;  

  25.      wndclass.cbWndExtra    = 0 ;  

  26.      wndclass.hInstance     = hInstance ;  

  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;  

  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;  

  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;  

  30.      wndclass.lpszMenuName  = szAppName ;  

  31.      wndclass.lpszClassName = szAppName ;  

  32.        

  33.      if (!RegisterClass (&wndclass))  

  34.      {  

  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),  

  36.                       szAppName, MB_ICONERROR) ;  

  37.           return 0 ;  

  38.      }  

  39.        

  40.      hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),  

  41.                           WS_OVERLAPPEDWINDOW,  

  42.                           CW_USEDEFAULT, CW_USEDEFAULT,  

  43.                           CW_USEDEFAULT, CW_USEDEFAULT,  

  44.                           NULL, NULL, hInstance, NULL) ;  

  45.        

  46.      ShowWindow (hwnd, iCmdShow) ;  

  47.      UpdateWindow (hwnd) ;  

  48.        

  49.      while (GetMessage (&msg, NULL, 0, 0))  

  50.      {  

  51.           TranslateMessage (&msg) ;  

  52.           DispatchMessage (&msg) ;  

  53.      }  

  54.      return msg.wParam ;  

  55. }  

  56.   

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

  58. {  

  59.      static int idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,  

  60.                                 DKGRAY_BRUSH, BLACK_BRUSH } ;  

  61.      static int iSelection = IDM_BKGND_WHITE ;  

  62.      HMENU      hMenu ;  

  63.        

  64.      switch (message)  

  65.      {  

  66.      case WM_COMMAND:  

  67.           hMenu = GetMenu (hwnd) ;  

  68.             

  69.           switch (LOWORD (wParam))  

  70.           {  

  71.           case IDM_FILE_NEW:  

  72.           case IDM_FILE_OPEN:  

  73.           case IDM_FILE_SAVE:  

  74.           case IDM_FILE_SAVE_AS:  

  75.                MessageBeep (0) ;  

  76.                return 0 ;  

  77.                  

  78.           case IDM_APP_EXIT:  

  79.                SendMessage (hwnd, WM_CLOSE, 0, 0) ;  

  80.                return 0 ;  

  81.                  

  82.           case IDM_EDIT_UNDO:  

  83.           case IDM_EDIT_CUT:  

  84.           case IDM_EDIT_COPY:  

  85.           case IDM_EDIT_PASTE:  

  86.           case IDM_EDIT_CLEAR:  

  87.                MessageBeep (0) ;  

  88.                return 0 ;  

  89.                  

  90.           case IDM_BKGND_WHITE:         // Note: Logic below   

  91.           case IDM_BKGND_LTGRAY:        //   assumes that IDM_WHITE   

  92.           case IDM_BKGND_GRAY:          //   through IDM_BLACK are   

  93.           case IDM_BKGND_DKGRAY:        //   consecutive numbers in   

  94.           case IDM_BKGND_BLACK:         //   the order shown here.   

  95.                  

  96.                CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;  

  97.                iSelection = LOWORD (wParam) ;  

  98.                CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;  

  99.                  

  100.                SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)   

  101.                     GetStockObject   

  102.                              (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;  

  103.                  

  104.                InvalidateRect (hwnd, NULL, TRUE) ;  

  105.                return 0 ;  

  106.                  

  107.           case IDM_TIMER_START:  

  108.                if (SetTimer (hwnd, ID_TIMER, 1000, NULL))  

  109.                {  

  110.                     EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;  

  111.                     EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;  

  112.                }  

  113.                return 0 ;  

  114.                  

  115.           case IDM_TIMER_STOP:  

  116.                KillTimer (hwnd, ID_TIMER) ;  

  117.                EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;  

  118.                EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;  

  119.                return 0 ;  

  120.                  

  121.           case IDM_APP_HELP:  

  122.                MessageBox (hwnd, TEXT ("Help not yet implemented!"),  

  123.                            szAppName, MB_ICONEXCLAMATION | MB_OK) ;  

  124.                return 0 ;  

  125.                  

  126.           case IDM_APP_ABOUT:  

  127.                MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")  

  128.                                  TEXT ("(c) Charles Petzold, 1998"),  

  129.                            szAppName, MB_ICONINFORMATION | MB_OK) ;  

  130.                return 0 ;  

  131.           }  

  132.           break ;  

  133.             

  134.      case WM_TIMER:  

  135.           MessageBeep (0) ;  

  136.           return 0 ;  

  137.                  

  138.      case WM_DESTROY:  

  139.           PostQuitMessage (0) ;  

  140.           return 0 ;  

  141.      }  

  142.      return DefWindowProc (hwnd, message, wParam, lParam) ;  

  143. }  

/*-----------------------------------------
   MENUDEMO.C -- Menu Demonstration
                 (c) Charles Petzold, 1998
  -----------------------------------------*/
 
#include <windows.h>
#include "resource.h"

#define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     HWND     hwnd ;
     MSG      msg ;
     WNDCLASS wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = szAppName ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static int idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
                                DKGRAY_BRUSH, BLACK_BRUSH } ;
     static int iSelection = IDM_BKGND_WHITE ;
     HMENU      hMenu ;
     
     switch (message)
     {
     case WM_COMMAND:
          hMenu = GetMenu (hwnd) ;
          
          switch (LOWORD (wParam))
          {
          case IDM_FILE_NEW:
          case IDM_FILE_OPEN:
          case IDM_FILE_SAVE:
          case IDM_FILE_SAVE_AS:
               MessageBeep (0) ;
               return 0 ;
               
          case IDM_APP_EXIT:
               SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               return 0 ;
               
          case IDM_EDIT_UNDO:
          case IDM_EDIT_CUT:
          case IDM_EDIT_COPY:
          case IDM_EDIT_PASTE:
          case IDM_EDIT_CLEAR:
               MessageBeep (0) ;
               return 0 ;
               
          case IDM_BKGND_WHITE:         // Note: Logic below
          case IDM_BKGND_LTGRAY:        //   assumes that IDM_WHITE
          case IDM_BKGND_GRAY:          //   through IDM_BLACK are
          case IDM_BKGND_DKGRAY:        //   consecutive numbers in
          case IDM_BKGND_BLACK:         //   the order shown here.
               
               CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
               iSelection = LOWORD (wParam) ;
               CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
               
               SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
                    GetStockObject 
                             (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
               
               InvalidateRect (hwnd, NULL, TRUE) ;
               return 0 ;
               
          case IDM_TIMER_START:
               if (SetTimer (hwnd, ID_TIMER, 1000, NULL))
               {
                    EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
                    EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;
               }
               return 0 ;
               
          case IDM_TIMER_STOP:
               KillTimer (hwnd, ID_TIMER) ;
               EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
               EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;
               return 0 ;
               
          case IDM_APP_HELP:
               MessageBox (hwnd, TEXT ("Help not yet implemented!"),
                           szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               return 0 ;
               
          case IDM_APP_ABOUT:
               MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")
                                 TEXT ("(c) Charles Petzold, 1998"),
                           szAppName, MB_ICONINFORMATION | MB_OK) ;
               return 0 ;
          }
          break ;
          
     case WM_TIMER:
          MessageBeep (0) ;
          return 0 ;
               
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}


效果:

windows WIN32菜单_windows_02