Windows程序设计-菜单及其它资源

2017-09-13 12:25:01来源:CSDN作者:lilongsy人点击

分享

资源

图标、鼠标指针、字符串、自定义资源、菜单、快捷键、对话框、位图。

图标、鼠标指针、字符串、自定义资源

加载图标

icondemo
icondemo.rc部分代码:

IDI_ICONDEMO       ICON         "ICONDEMO.ico"IDI_SMALL               ICON         "small.ico"
/*------------------------------------------   ICONDEMO.C -- Icon Demonstration Program                 (c) Charles Petzold, 1998  ------------------------------------------*/#include <windows.h>#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     TCHAR    szAppName[] = TEXT ("IconDemo") ;     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 (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                      szAppName, MB_ICONERROR) ;          return 0 ;     }     hwnd = CreateWindow (szAppName, TEXT ("Icon Demo"),                          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 HICON hIcon ;     static int   cxIcon, cyIcon, cxClient, cyClient ;     HDC          hdc ;     HINSTANCE    hInstance ;     PAINTSTRUCT  ps ;     int          x, y ;     switch (message)     {     case WM_CREATE :          hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;          // 加载图标          // MAKEINTRESOURCE 把整数转换成资源字符串          hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;          // 获取图标宽高          cxIcon = GetSystemMetrics (SM_CXICON) ;          cyIcon = GetSystemMetrics (SM_CYICON) ;          return 0 ;     case WM_SIZE :          cxClient = LOWORD (lParam) ;          cyClient = HIWORD (lParam) ;          return 0 ;     case WM_PAINT :          hdc = BeginPaint (hwnd, &ps) ;          for (y = 0 ; y < cyClient ; y += cyIcon)               for (x = 0 ; x < cxClient ; x += cxIcon)                    DrawIcon (hdc, x, y, hIcon) ;               EndPaint (hwnd, &ps) ;               return 0 ;     case WM_DESTROY :          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}
// 在资源描述文档里,定义一个ico// DISCARDABLE 内存不足时可以丢弃icoIDI_ICON ICON DISCARDABLE "icondemo.ico"// 加载预定义图标LoadIcon (NULL, IDI_APPLICATION) ;// 加载字符串图标static TCHAR szAppName [] = TEXT ("MyProg") ;hIcon = LoadIcon (hInstance, szAppName) ;// 动态改变程序图标SetClassLong (hwnd, GCL_HICON,    LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ALTICON))) ;// 改变窗口类光标SetClassLong (hwndChild, GCL_HCURSOR,    LoadCursor (hInstance, TEXT ("childcursor")) ;// 设置显示区域鼠标SetCursor (hCursor) ;

自定义资源

poepoem
POEPOEM.rc部分:

///////////////////////////////////////////////////////////////////////////////// TEXT//ANNABELLEE              TEXT    DISCARDABLE     "poepoem.txt"///////////////////////////////////////////////////////////////////////////////// Icon//// Icon with lowest ID value placed first to ensure application icon// remains consistent on all systems.POEPOEM                 ICON    DISCARDABLE     "poepoem.ico"///////////////////////////////////////////////////////////////////////////////// String Table//STRINGTABLE DISCARDABLE BEGIN    IDS_APPNAME             "PoePoem"    IDS_CAPTION             """Annabel Lee"" by Edgar Allan Poe"    IDS_ERRMSG              "This program requires Windows NT!"END

RESOURCE.H

#define IDS_APPNAME                     1#define IDS_CAPTION                     2#define IDS_ERRMSG                      3

POEPOEM.c

/*-------------------------------------------   POEPOEM.C -- Demonstrates Custom Resource                (c) Charles Petzold, 1998  -------------------------------------------*/#include <windows.h>#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;HINSTANCE hInst ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     TCHAR    szAppName [16], szCaption [64], szErrMsg [64] ;     HWND     hwnd ;     MSG      msg ;     WNDCLASS wndclass ;     LoadString (hInstance, IDS_APPNAME, szAppName,                             sizeof (szAppName) / sizeof (TCHAR)) ;     LoadString (hInstance, IDS_CAPTION, szCaption,                             sizeof (szCaption) / sizeof (TCHAR)) ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;     wndclass.lpfnWndProc   = WndProc ;     wndclass.cbClsExtra    = 0 ;     wndclass.cbWndExtra    = 0 ;     wndclass.hInstance     = hInstance ;     wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          LoadStringA (hInstance, IDS_APPNAME, (char *) szAppName,                                   sizeof (szAppName)) ;          LoadStringA (hInstance, IDS_ERRMSG, (char *) szErrMsg,                                    sizeof (szErrMsg)) ;          MessageBoxA (NULL, (char *) szErrMsg,                              (char *) szAppName, MB_ICONERROR) ;          return 0 ;     }     hwnd = CreateWindow (szAppName, szCaption,                          WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,                          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 char  * pText ;     static HGLOBAL hResource ;     static HWND    hScroll ;     static int     iPosition, cxChar, cyChar, cyClient, iNumLines, xScroll ;     HDC            hdc ;     PAINTSTRUCT    ps ;     RECT           rect ;     TEXTMETRIC     tm ;     switch (message)     {     case WM_CREATE :          hdc = GetDC (hwnd) ;          GetTextMetrics (hdc, &tm) ;          cxChar = tm.tmAveCharWidth ;          cyChar = tm.tmHeight + tm.tmExternalLeading ;          ReleaseDC (hwnd, hdc) ;          xScroll = GetSystemMetrics (SM_CXVSCROLL) ;          hScroll = CreateWindow (TEXT ("scrollbar"), NULL,                                  WS_CHILD | WS_VISIBLE | SBS_VERT,                                  0, 0, 0, 0,                                  hwnd, (HMENU) 1, hInst, NULL) ;          // 加载自定义资源          hResource = LoadResource (hInst,                       FindResource (hInst, TEXT ("AnnabelLee"),                                           TEXT ("TEXT"))) ;          pText = (char *) LockResource (hResource) ;          iNumLines = 0 ;          while (*pText != '//' && *pText != '/0')          {               if (*pText == '/n')                    iNumLines ++ ;               pText = AnsiNext (pText) ;          }          // *pText = '/0' ;          SetScrollRange (hScroll, SB_CTL, 0, iNumLines, FALSE) ;          SetScrollPos   (hScroll, SB_CTL, 0, FALSE) ;          return 0 ;     case WM_SIZE :          MoveWindow (hScroll, LOWORD (lParam) - xScroll, 0,                      xScroll, cyClient = HIWORD (lParam), TRUE) ;          SetFocus (hwnd) ;          return 0 ;     case WM_SETFOCUS :          SetFocus (hScroll) ;          return 0 ;     case WM_VSCROLL :          switch (wParam)          {          case SB_TOP :               iPosition = 0 ;               break ;          case SB_BOTTOM :               iPosition = iNumLines ;               break ;          case SB_LINEUP :               iPosition -= 1 ;               break ;          case SB_LINEDOWN :               iPosition += 1 ;               break ;          case SB_PAGEUP :               iPosition -= cyClient / cyChar ;               break ;          case SB_PAGEDOWN :               iPosition += cyClient / cyChar ;               break ;          case SB_THUMBPOSITION :               iPosition = LOWORD (lParam) ;               break ;          }          iPosition = max (0, min (iPosition, iNumLines)) ;          if (iPosition != GetScrollPos (hScroll, SB_CTL))          {               SetScrollPos (hScroll, SB_CTL, iPosition, TRUE) ;               InvalidateRect (hwnd, NULL, TRUE) ;          }          return 0 ;     case WM_PAINT :          hdc = BeginPaint (hwnd, &ps) ;          // 锁定自定义资源          pText = (char *) LockResource (hResource) ;          GetClientRect (hwnd, &rect) ;          rect.left += cxChar ;          rect.top  += cyChar * (1 - iPosition) ;          DrawTextA (hdc, pText, -1, &rect, DT_EXTERNALLEADING) ;          EndPaint (hwnd, &ps) ;          return 0 ;     case WM_DESTROY :         // 释放自定义资源          FreeResource (hResource) ;          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

删掉了resource.h里AnnabelLee里的define才加载资源成功,不明具体原因。

菜单

菜单结构

菜单都有菜单句柄;
菜单项包括三个特征:显示的字符串或位图、ID或句柄、属性。

有&会加底线
/t第一列右边新一列上
/a向右对齐

在程序中引用菜单

第一、窗口类中指定

wndclass.lpszMenuName = szAppName ;

第二、LoadMenu加载菜单

hMenu = LoadMenu (hInstance, TEXT ("MyMenu")) ;hMenu = LoadMenu (hInstance, MAKEINTRESOURCE (ID_MENU)) ;hwnd = CreateWindow (TEXT ("MyClass"), TEXT ("Window Caption"),    WS_OVERLAPPEDWINDOW,    CW_USEDEFAULT, CW_USEDEFAULT,    CW_USEDEFAULT, CW_USEDEFAULT,    NULL, hMenu, hInstance, NULL);  // 第九个参数指定hMenu

第三、SetMenu动态调用

SetMenu (hwnd, hMenu) ;

当窗口被清除时,与窗口相关的所有菜单都将被清除。与窗口不相关的菜单在程序结束前通过呼叫DestroyMenu主动清除。

菜单和消息

WM_INITMENU:wParam: 主菜单句柄; lParam: 0

WM_MENUSELECT
LOWORD (wParam):被选中项目:菜单ID或者弹出式菜单句柄
HIWORD (wParam):选择旗标(MF_GRAYED、MF_DISABLED、MF_CHECKED、MF_BITMAP、MF_POPUP、
MF_HELP、MF_SYSMENU和MF_MOUSESELECT)
lParam: 包含被选中项目的菜单句柄

WM_INITMENUPOPUP
wParam: 弹出式菜单句柄
LOWORD (lParam):弹出式菜单索引
HIWORD (lParam): 系统菜单为1,其它为0

最重要的菜单消息是WM_COMMAND,它表示使用者已经从菜单中选中了一个被启用的菜单项。

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

WM_SYSCOMMAND是WM_SYSCOMMAND表示使用者从系统菜单中选择一个启用的菜单项。
wParam: 菜单ID
lParam: 0

如果WM_SYSCOMMAND消息是由按鼠标按键产生的,LOWORD(lParam)和HIWORD(lParam)将包含鼠标光标位置的x和y屏幕坐标。

对于WM_SYSCOMMAND,菜单ID指示系统菜单中的哪一项被选中。对于预先定义的系统菜单项,较低的那四个位应该和0xFFF0进行AND运算来屏蔽掉,结果值应该为下列之一:SC_SIZE 、 SC_MOVE 、 SC_MINIMIZE 、 SC_MAXIMIZE 、 SC_NEXTWINDOW 、SC_PREVWINDOW、SC_CLOSE、SC_VSCROLL、SC_HSCROLL、SC_ARRANGE、SC_RESTORE 和 SC_TASKLIST 。 此 外 , wParam 可 以 是 SC_MOUSEMENU 或SC_KEYMENU。

如果您在系统菜单中添加菜单项,那么wParam的低字组将是您定义的菜单ID。为了避免与预先定义的菜单ID相冲突,应用程序应该使用小于0xF000的值,这对于将一般的WM_SYSCOMMAND消息发送给DefWindowProc是很重要的。如果您不这样做,那么您实际上就是禁用了正常的系统菜单命令。

WM_MENUCHAR

Windows会把这个消息发送到窗口消息处理程序:如果使用者按下Alt和一个与菜单项不匹配的字符时,或者在显示弹出式菜单而使用者按下一个与弹出式菜单里的项目不匹配的字符键时。随WM_MENUCHAR消息一起发送的参数如下所示:

LOWORD (wParam): 字符代码(ASCII或Unicode)
HIWORD (wParam): 选择码
lParam: 菜单句柄
选择码是:
0 不显示弹出式菜单
MF_POPUP 显示弹出式菜单
MF_SYSMENU 显示系统弹出式菜单

Windows程序通常把该消息传递给DefWindowProc,它一般给Windows传回0,这会使Windows发出哔声。

简单的菜单

MenuDemo
MenuDemo.rc

///////////////////////////////////////////////////////////////////////////////// Menu//MENUDEMO MENU DISCARDABLE BEGIN    POPUP "&File"    BEGIN        MENUITEM "&New",                        ID_MENUITEM40020        MENUITEM "&Open",                       IDM_FILE_OPEN        MENUITEM "&Save",                       IDM_FILE_SAVE        MENUITEM "Save &As...",                 IDM_FILE_SAVE_AS        MENUITEM SEPARATOR        MENUITEM "E&xit",                       IDM_APP_EXIT    END    POPUP "&Edit"    BEGIN        MENUITEM "&Undo",                       IDM_EDIT_UNDO        MENUITEM SEPARATOR        MENUITEM "C&ut",                        IDM_EDIT_CUT        MENUITEM "&Copy",                       IDM_EDIT_COPY        MENUITEM "&Paste",                      IDM_EDIT_PASTE        MENUITEM "De&lete",                     IDM_EDIT_CLEAR    END    POPUP "&Background"    BEGIN        MENUITEM "&White",                      IDM_BKGND_WHITE, CHECKED        MENUITEM "&Light Gray",                 IDM_BKGND_LTGRAY        MENUITEM "&Gray",                       IDM_BKGND_GRAY        MENUITEM "&Dark Gray",                  IDM_BKGND_DKGRAY        MENUITEM "&Black",                      IDM_BKGND_BLACK    END    POPUP "&Timer"    BEGIN        MENUITEM "&Start",                      IDM_TIMER_START        MENUITEM "S&top",                       IDM_TIMER_STOP, GRAYED    END    POPUP "&Help"    BEGIN        MENUITEM "&Help...",                    IDM_APP_HELP        MENUITEM "&About MenuDemo...",          IDM_APP_ABOUT    ENDEND

RESOURCE.H

#define IDM_FILE_NEW                    40001#define IDM_FILE_OPEN                   40002#define IDM_FILE_SAVE                   40003#define IDM_FILE_SAVE_AS                40004#define IDM_APP_EXIT                    40005#define IDM_EDIT_UNDO                   40006#define IDM_EDIT_CUT                    40007#define IDM_EDIT_COPY                   40008#define IDM_EDIT_PASTE                  40009#define IDM_EDIT_CLEAR                  40010#define IDM_BKGND_WHITE                 40011#define IDM_BKGND_LTGRAY                40012#define IDM_BKGND_GRAY                  40013#define IDM_BKGND_DKGRAY                40014#define IDM_BKGND_BLACK                 40015#define IDM_TIMER_START                 40016#define IDM_TIMER_STOP                  40017#define IDM_APP_HELP                    40018#define IDM_APP_ABOUT                   40019#define ID_MENUITEM40020                40020
/*-----------------------------------------   MENUDEMO.C -- Menu Demonstration                 (c) Charles Petzold, 1998  -----------------------------------------*/#include <windows.h>#include "resource.h"#define ID_TIMER 1LRESULT 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))               {                   // 设置菜单项Start无效,Stop启用                    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) ;}

浮动弹出式菜单

popmenu
PopMenu.rc

POPMENU MENU DISCARDABLE BEGIN    POPUP "MyMenu"    BEGIN        POPUP "&File"        BEGIN            MENUITEM "&New",                        IDM_FILE_NEW            MENUITEM "&Open",                       IDM_FILE_OPEN            MENUITEM "&Save",                       IDM_FILE_SAVE            MENUITEM "Save &As",                    IDM_FILE_SAVE_AS            MENUITEM SEPARATOR            MENUITEM "E&xit",                       IDM_APP_EXIT        END        POPUP "&Edit"        BEGIN            MENUITEM "&Undo",                       IDM_EDIT_UNDO            MENUITEM SEPARATOR            MENUITEM "Cu&t",                        IDM_EDIT_CUT            MENUITEM "&Copy",                       IDM_EDIT_COPY            MENUITEM "&Paste",                      IDM_EDIT_PASTE            MENUITEM "De&lete",                     IDM_EDIT_CLEAR        END        POPUP "&Background"        BEGIN            MENUITEM "&White",                      IDM_BKGND_WHITE, CHECKED            MENUITEM "&Light Gray",                 IDM_BKGND_LTGRAY            MENUITEM "&Gray",                       IDM_BKGND_GRAY            MENUITEM "&Dark Gray",                  IDM_BKGND_DKGRAY            MENUITEM "&Black",                      IDM_BKGND_BLACK        END        POPUP "&Help"        BEGIN            MENUITEM "&Help...",                    IDM_APP_HELP            MENUITEM "&About PopMenu...",           IDM_APP_ABOUT        END    ENDEND

RESOURCE.H

#define IDM_FILE_NEW                    40001#define IDM_FILE_OPEN                   40002#define IDM_FILE_SAVE                   40003#define IDM_FILE_SAVE_AS                40004#define IDM_APP_EXIT                    40005#define IDM_EDIT_UNDO                   40006#define IDM_EDIT_CUT                    40007#define IDM_EDIT_COPY                   40008#define IDM_EDIT_PASTE                  40009#define IDM_EDIT_CLEAR                  40010#define IDM_BKGND_WHITE                 40011#define IDM_BKGND_LTGRAY                40012#define IDM_BKGND_GRAY                  40013#define IDM_BKGND_DKGRAY                40014#define IDM_BKGND_BLACK                 40015#define IDM_HELP_HELP                   40016#define IDM_APP_HELP                    40016#define IDM_APP_ABOUT                   40017
/*----------------------------------------   POPMENU.C -- Popup Menu Demonstration                (c) Charles Petzold, 1998  ----------------------------------------*/#include <windows.h>#include "resource.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;HINSTANCE hInst ;TCHAR     szAppName[] = TEXT ("PopMenu") ;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, szAppName) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                      szAppName, MB_ICONERROR) ;          return 0 ;     }     hInst = hInstance ;     hwnd = CreateWindow (szAppName, TEXT ("Popup 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 HMENU hMenu ;     static int   idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,                                  DKGRAY_BRUSH, BLACK_BRUSH } ;     static int   iSelection = IDM_BKGND_WHITE ;     POINT        point ;     switch (message)     {     case WM_CREATE:          hMenu = LoadMenu (hInst, szAppName) ;          hMenu = GetSubMenu (hMenu, 0) ;          return 0 ;     case WM_RBUTTONUP:         // 获取鼠标位置          point.x = LOWORD (lParam) ;          point.y = HIWORD (lParam) ;          ClientToScreen (hwnd, &point) ;          // 鼠标右键弹出菜单          TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, point.x, point.y,                           0, hwnd, NULL) ;          return 0 ;     case WM_COMMAND:          switch (LOWORD (wParam))          {          case IDM_FILE_NEW:          case IDM_FILE_OPEN:          case IDM_FILE_SAVE:          case IDM_FILE_SAVE_AS:          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_APP_ABOUT:               MessageBox (hwnd, TEXT ("Popup Menu Demonstration Program/n")                                 TEXT ("(c) Charles Petzold, 1998"),                           szAppName, MB_ICONINFORMATION | MB_OK) ;               return 0 ;          case IDM_APP_EXIT:               SendMessage (hwnd, WM_CLOSE, 0, 0) ;               return 0 ;          case IDM_APP_HELP:               MessageBox (hwnd, TEXT ("Help not yet implemented!"),                           szAppName, MB_ICONEXCLAMATION | MB_OK) ;               return 0 ;          }          break ;     case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

使用系统菜单

系统菜单

/*-----------------------------------------   POORMENU.C -- The Poor Person's Menu                 (c) Charles Petzold, 1998  -----------------------------------------*/#include <windows.h>#define IDM_SYS_ABOUT   1#define IDM_SYS_HELP    2#define IDM_SYS_REMOVE  3LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;static TCHAR szAppName[] = TEXT ("PoorMenu") ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     HMENU    hMenu ;     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  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                      szAppName, MB_ICONERROR) ;          return 0 ;     }     hwnd = CreateWindow (szAppName, TEXT ("The Poor-Person's Menu"),                          WS_OVERLAPPEDWINDOW,                          CW_USEDEFAULT, CW_USEDEFAULT,                          CW_USEDEFAULT, CW_USEDEFAULT,                          NULL, NULL, hInstance, NULL) ;     hMenu = GetSystemMenu (hwnd, FALSE) ;     AppendMenu (hMenu, MF_SEPARATOR, 0,           NULL) ;     AppendMenu (hMenu, MF_STRING, IDM_SYS_ABOUT,  TEXT ("About...")) ;     AppendMenu (hMenu, MF_STRING, IDM_SYS_HELP,   TEXT ("Help...")) ;     AppendMenu (hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT ("Remove Additions")) ;     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){     switch (message)     {     case WM_SYSCOMMAND:          switch (LOWORD (wParam))          {          case IDM_SYS_ABOUT:               MessageBox (hwnd, TEXT ("A Poor-Person's Menu Program/n")                                 TEXT ("(c) Charles Petzold, 1998"),                           szAppName, MB_OK | MB_ICONINFORMATION) ;               return 0 ;          case IDM_SYS_HELP:               MessageBox (hwnd, TEXT ("Help not yet implemented!"),                           szAppName, MB_OK | MB_ICONEXCLAMATION) ;               return 0 ;          case IDM_SYS_REMOVE:               GetSystemMenu (hwnd, TRUE) ;               return 0 ;          }          break ;     case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

快捷键

poppad2
PopPad2.rc

///////////////////////////////////////////////////////////////////////////////// Icon//// Icon with lowest ID value placed first to ensure application icon// remains consistent on all systems.POPPAD2                 ICON    DISCARDABLE     "poppad2.ico"///////////////////////////////////////////////////////////////////////////////// Menu//POPPAD2 MENU DISCARDABLE BEGIN    POPUP "&File"    BEGIN        MENUITEM "&New",                        IDM_FILE_NEW        MENUITEM "&Open...",                    IDM_FILE_OPEN        MENUITEM "&Save",                       IDM_FILE_SAVE        MENUITEM "Save &As...",                 IDM_FILE_SAVE_AS        MENUITEM SEPARATOR        MENUITEM "&Print",                      IDM_FILE_PRINT        MENUITEM SEPARATOR        MENUITEM "E&xit",                       IDM_APP_EXIT    END    POPUP "&Edit"    BEGIN        MENUITEM "&Undo/tCtrl+Z",               IDM_EDIT_UNDO        MENUITEM SEPARATOR        MENUITEM "Cu&t/tCtrl+X",                IDM_EDIT_CUT        MENUITEM "&Copy/tCtrl+C",               IDM_EDIT_COPY        MENUITEM "&Paste/tCtrl+V",              IDM_EDIT_PASTE        MENUITEM "De&lete/tDel",                IDM_EDIT_CLEAR        MENUITEM SEPARATOR        MENUITEM "&Select All",                 IDM_EDIT_SELECT_ALL    END    POPUP "&Help"    BEGIN        MENUITEM "&Help...",                    IDM_HELP_HELP        MENUITEM "&About PopPad2...",           IDM_APP_ABOUT    ENDEND///////////////////////////////////////////////////////////////////////////////// Accelerator//POPPAD2 ACCELERATORS DISCARDABLE BEGIN    VK_BACK,        IDM_EDIT_UNDO,          VIRTKEY, ALT, NOINVERT    VK_DELETE,      IDM_EDIT_CLEAR,         VIRTKEY, NOINVERT    VK_DELETE,      IDM_EDIT_CUT,           VIRTKEY, SHIFT, NOINVERT    VK_F1,          IDM_HELP_HELP,          VIRTKEY, NOINVERT    VK_INSERT,      IDM_EDIT_COPY,          VIRTKEY, CONTROL, NOINVERT    VK_INSERT,      IDM_EDIT_PASTE,         VIRTKEY, SHIFT, NOINVERT    "^C",           IDM_EDIT_COPY,          ASCII,  NOINVERT    "^V",           IDM_EDIT_PASTE,         ASCII,  NOINVERT    "^X",           IDM_EDIT_CUT,           ASCII,  NOINVERT    "^Z",           IDM_EDIT_UNDO,          ASCII,  NOINVERTEND

RESOURCE.H

#define IDM_FILE_NEW                    40001#define IDM_FILE_OPEN                   40002#define IDM_FILE_SAVE                   40003#define IDM_FILE_SAVE_AS                40004#define IDM_FILE_PRINT                  40005#define IDM_APP_EXIT                    40006#define IDM_EDIT_UNDO                   40007#define IDM_EDIT_CUT                    40008#define IDM_EDIT_COPY                   40009#define IDM_EDIT_PASTE                  40010#define IDM_EDIT_CLEAR                  40011#define IDM_EDIT_SELECT_ALL             40012#define IDM_HELP_HELP                   40013#define IDM_APP_ABOUT                   40014

PopPad2.c

/*-----------------------------------------------------   POPPAD2.C -- Popup Editor Version 2 (includes menu)                (c) Charles Petzold, 1998  -----------------------------------------------------*/#include <windows.h>#include "resource.h"#define ID_EDIT     1LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);TCHAR szAppName[] = TEXT ("PopPad2") ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     HACCEL   hAccel ;     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 (hInstance, szAppName) ;     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, szAppName,                          WS_OVERLAPPEDWINDOW,                          GetSystemMetrics (SM_CXSCREEN) / 4,                          GetSystemMetrics (SM_CYSCREEN) / 4,                          GetSystemMetrics (SM_CXSCREEN) / 2,                          GetSystemMetrics (SM_CYSCREEN) / 2,                          NULL, NULL, hInstance, NULL) ;     ShowWindow (hwnd, iCmdShow) ;     UpdateWindow (hwnd) ;      // 加载快捷键表     hAccel = LoadAccelerators (hInstance, szAppName) ;     while (GetMessage (&msg, NULL, 0, 0))     {          // 快捷键代码转换          if (!TranslateAccelerator (hwnd, hAccel, &msg))          {               TranslateMessage (&msg) ;               DispatchMessage (&msg) ;          }     }     return msg.wParam ;}AskConfirmation (HWND hwnd){     return MessageBox (hwnd, TEXT ("Really want to close PopPad2?"),                        szAppName, MB_YESNO | MB_ICONQUESTION) ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){     static HWND hwndEdit ;     int         iSelect, iEnable ;     switch (message)     {     case WM_CREATE:          hwndEdit = CreateWindow (TEXT ("edit"), NULL,                              WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |                              WS_BORDER | ES_LEFT | ES_MULTILINE |                              ES_AUTOHSCROLL | ES_AUTOVSCROLL,                              0, 0, 0, 0, hwnd, (HMENU) ID_EDIT,                              ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;          return 0 ;     case WM_SETFOCUS:          SetFocus (hwndEdit) ;          return 0 ;     case WM_SIZE:           MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ;          return 0 ;     case WM_INITMENUPOPUP:          if (lParam == 1)          {               EnableMenuItem ((HMENU) wParam, IDM_EDIT_UNDO,                    SendMessage (hwndEdit, EM_CANUNDO, 0, 0) ?                                   MF_ENABLED : MF_GRAYED) ;               EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE,                    IsClipboardFormatAvailable (CF_TEXT) ?                                   MF_ENABLED : MF_GRAYED) ;               iSelect = SendMessage (hwndEdit, EM_GETSEL, 0, 0) ;               if (HIWORD (iSelect) == LOWORD (iSelect))                    iEnable = MF_GRAYED ;               else                    iEnable = MF_ENABLED ;               EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,   iEnable) ;               EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,  iEnable) ;               EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, iEnable) ;               return 0 ;          }          break ;     case WM_COMMAND:          if (lParam)          {               if (LOWORD (lParam) == ID_EDIT &&                         (HIWORD (wParam) == EN_ERRSPACE ||                          HIWORD (wParam) == EN_MAXTEXT))                    MessageBox (hwnd, TEXT ("Edit control out of space."),                                szAppName, MB_OK | MB_ICONSTOP) ;               return 0 ;          }          else switch (LOWORD (wParam))          {          case IDM_FILE_NEW:          case IDM_FILE_OPEN:          case IDM_FILE_SAVE:          case IDM_FILE_SAVE_AS:          case IDM_FILE_PRINT:               MessageBeep (0) ;               return 0 ;          case IDM_APP_EXIT:               SendMessage (hwnd, WM_CLOSE, 0, 0) ;               return 0 ;          case IDM_EDIT_UNDO:               SendMessage (hwndEdit, WM_UNDO, 0, 0) ;               return 0 ;          case IDM_EDIT_CUT:               SendMessage (hwndEdit, WM_CUT, 0, 0) ;               return 0 ;          case IDM_EDIT_COPY:               SendMessage (hwndEdit, WM_COPY, 0, 0) ;               return 0 ;          case IDM_EDIT_PASTE:               SendMessage (hwndEdit, WM_PASTE, 0, 0) ;               return 0 ;          case IDM_EDIT_CLEAR:               SendMessage (hwndEdit, WM_CLEAR, 0, 0) ;               return 0 ;          case IDM_EDIT_SELECT_ALL:               SendMessage (hwndEdit, EM_SETSEL, 0, -1) ;               return 0 ;          case IDM_HELP_HELP:               MessageBox (hwnd, TEXT ("Help not yet implemented!"),                           szAppName, MB_OK | MB_ICONEXCLAMATION) ;               return 0 ;          case IDM_APP_ABOUT:               MessageBox (hwnd, TEXT ("POPPAD2 (c) Charles Petzold, 1998"),                           szAppName, MB_OK | MB_ICONINFORMATION) ;               return 0 ;          }          break ;     case WM_CLOSE:          if (IDYES == AskConfirmation (hwnd))               DestroyWindow (hwnd) ;          return 0 ;     case WM_QUERYENDSESSION:          if (IDYES == AskConfirmation (hwnd))               return 1 ;          else               return 0 ;     case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

接收到的WM_COMMAND消息

  快捷键 菜单 控件
LOWORD(wParam) 快捷键ID 菜单ID 控件ID
HIWORD(wParam) 1 0 通知码
lParam 0 0 子窗口句柄

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台