API菜单与加速键

#define IDM_NEW 10
#define IDM_OPEN 11
#define IDM_CLOSE 12
#define IDM_SAVE 13
#define  IDM_SAVES 14
#define  IDM_ADDPRINTMENU 15
#define   IDM_DELPRINTMENU 16
#define   IDM_EXIT 17
#define   IDM_CUT 18
#define    IDM_COPY 19
#define    IDM_PASTE 20
#define    IDM_DELETE 21
#define    IDM_HELP 22
#define    IDM_LINE 23
#define    IDM_RECT 24
#define   IDM_RRECT 25
#define    IDM_ELLIP  26


#include<windows.h>
#include"7-1.h"
HMENU hmenu,hprintmenu;//定义菜单句柄
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain
(
 HINSTANCE hInstance,
 HINSTANCE hPrevInst,
 LPSTR lpszCmdLine,
 int nCmdShow
 )
{
 HWND hwnd;
 MSG Msg;
 HACCEL hAccel;
 WNDCLASS wndclass;
 char lpszMenuName[]="Menu";
 char lpszClassName[]="菜单";
 char lpszTitle[]="My_Menu_Example";
 wndclass.style=0;
 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=GetStockObject(WEITE_BRUSH);
 wndclass.lpszMenuName=lpszMenuName;//加载窗口类菜单
 wndclass.lpszClassName=lpszClassName;

 if(!RegisterClass(&wndclass))
 {
  MessageBeep(0);
  return FALSE;
 }
 hwnd=CreateWindow
  (
  lpszClassName,
  lpszTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL
  );
 ShowWindow(hwnd,nCmdShow);
 UpdateWindow(hwnd);

 //加载加速键资源
 hAccel=LoadAccelerators(hInstance,lpszMenuName);

 while(GetMessage(&Msg,NULL,0,0))
 {
  //在消息循环中截获加速键消息
  if(!(TranslateAccelerator(hwnd,hAccel,&Msg))
  {
   TranslateMessage(&Msg);
   DispatchMessage(&Msg);
  }
 }
 return Msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 switch(message)
 {
  //处理菜单消息
 case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case IDM_NEW:
   break;
  case IDM_OPEN:
   break;
  case IDM_SAVE:
   brak;
  case IDM_SAVES:
   break;
  case IDM_CLOSE:
   break;
   //在主菜单栏中添加“统计计算”菜单
  case IDM_ADDPRINTMENU:
   //获取主菜单句柄
   hmenu=GetMenu(hwnd);
   //动态创建菜单
   hprintmenu=CreateMenu();
   //在创建的菜单中增加菜单项
   AppendMenu(hprintmenu,MF_ENABLED,IDM_LINE,"求和");
   AppendMenu(hprintmenu,MF_ENABLED,IDM_RECT,"求差");
   AppendMenu(hprintmenu,MF_ENABLED,IDM_RECT,"方差");
   AppendMenu(hprintmenu,MF_ENABLED,IDM_ELLIP,"均方根");
   //将创建的弹出式菜单插入主菜单中
   InsertMenu(hmenu,2,MF_POPUP|MF_BYPOSITION,(UINT)hprintmenu,"统计计算(&c)");
   //相应改变菜单中有关统计计算单项的属性
   EnableMenuItem(hmenu,IDM_ADDPAINTMENU,MF_GRAYED);
   EnableMenuItem(hmenu,IDM_DELPRINTMENU,MF_ENABLED);
   DrawMenuBar(hwnd);//重新显示窗口菜单

   //从菜单中删除弹出式统计计算菜单
  case IDM_DELPRINTMENU:
   DeleteMenu(hmenu,2,MF_BYPOSITION);//删除统计计算菜单项
   //相应改变“文件”菜单中有关统计计算菜单项的属性
   EnableMenuItem(hmenu,IDM_ADDPRINTMENU,MF_ENABLED);
   EnableMenuItem(hmenu,IDM_DELPRINTMENU,MF_GRAYED);
   DrawMenuBar(hwnd);//重新显示窗口菜单
   break;
  case IDM_EXIT:
   //选择“退出”菜单项时,向应用程序发出WM_DESTROY消息
   SendMessage(hwnd,WM_DESTROY,0,0);
   break;
  case IDM_COPY:
   break;
  case IDM_PASTE:
   break;
  case IDM_DELETE:
   break;
  }
  break;
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hwnd,message,wParam,lParam);
 }
 return 0;
}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值