使用Win API创建顶级菜单(不使用资源文件)

作者:朱金灿

来源:http://blog.csdn.net/clever101

 

         最近使用Code::Blocks进行业余学习(VS200x对我的机子来说太庞大了)。我就编编些Win API程序。Code::Blocks貌似需要额外的资源脚本编辑器才能编写资源脚本(叫ResEdit)。得了我也不想用这种不太成熟的外部工具,打算学习直接用代码创建UI控件。今天就学习如何创建顶层菜单吧。摸索了一下,就弄懂了。

 

1.      使用Code::Blocks创建一个Win32 GUI Project,就叫MyApp吧,此步略过不提。

 

2.      添加一个resoure.h,用于保存资源ID(资源脚本不要,资源ID总是需要的),添加如何代码:


#define  IDM_FILE_NEW   1024
#define  IDM_FILE_OPEN  1025

3.添加创建顶层菜单的代码,首先添加顶层菜单句柄和创建函数,代码如下:

HMENU g_hTopMenu = NULL; // 顶层菜单全局变量

BOOL CreateTopMenu()
{
    g_hTopMenu = CreateMenu();

//创建弹出菜单句柄
HMENU hMenuPopup = CreateMenu();

//在弹出菜单中加入菜单项
::AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_NEW,"&New");
::AppendMenu(hMenuPopup, MF_STRING, IDM_FILE_OPEN,"&Open");

//将弹出菜单的句柄加入到顶级菜单
::AppendMenu(g_hTopMenu,MF_POPUP,(UINT)hMenuPopup,"&File");
}

其次在WinMain函数中调用:

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

  //创建主菜单
   CreateTopMenu();

/* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           g_hTopMenu,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

接着添加菜单的命名消息响应代码,如下:

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case   WM_COMMAND:
          switch(  LOWORD( wParam ))
          {
                  case   IDM_FILE_NEW:
                  ::MessageBox(hwnd,_T("新建文件"),_T("提示"),MB_OK);
                  break;
                   case   IDM_FILE_OPEN:
                    ::MessageBox(hwnd,_T("打开文件"),_T("提示"),MB_OK);
                  break;
                  default:
                  break;
          }
          break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

       这样就实现了不适用资源脚本文件也能创建Win API的应用程序菜单。

 

参考文献:

 

1. Windows API 学习笔记—菜单





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clever101

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值