[原]使用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 学习笔记—菜单





作者:clever101 发表于2012-2-15 22:16:21 原文链接
阅读:652 评论:3 查看评论
posted @ 2012-02-15 22:16  程序员天下  阅读(414)  评论(0编辑  收藏  举报