+ -
当前位置:首页 → 问答吧 → 使用API函数如何添加菜单

使用API函数如何添加菜单

时间:2011-12-11

来源:互联网

今天在学习资源相关知识,按照书上的例子编写,一直调试不过,可能对菜单的创建过程还是不太明了。
下面我提供一段简单的窗口代码,希望有人帮我添加上创建菜单的代码,先谢谢了。菜单很简单,类似于标准windows程序的文件菜单。
结构为
文件
  打开
  保存
  另存为
  分割线
  退出



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

const char clsname[]="mywindows";

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nShowCmd);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nShowCmd))
return FALSE;

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

BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;

wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_HAND);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=clsname;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;

return RegisterClass(&wndclass);
}

BOOL InitWindows(HINSTANCE hInstance,int nShowCmd)
{
HWND hwnd;
HMENU hmenu;
hmenu=LoadMenu(hInstance,muname);
hwnd=CreateWindow(clsname,"Application",WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

if(!hwnd)
return FALSE;
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{

switch(msg)
{
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"Exit the application?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;

}

作者: ivanwu1020   发布时间: 2011-12-11

C/C++ code

#include "stdafx.h"
#include <windows.h>
#pragma comment(linker,"/subsystem:windows")
#include "resource.h"

const char clsname[]="mywindows";

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nShowCmd);

int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg;
    if(!InitWindowsClass(hInstance))
        return FALSE;
    if(!InitWindows(hInstance,nShowCmd))
        return FALSE;

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

BOOL InitWindowsClass(HINSTANCE hInstance)
{
    WNDCLASS wndclass;

    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_HAND);
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=WndProc;
    wndclass.lpszClassName=clsname;
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;

    return RegisterClass(&wndclass);
}

BOOL InitWindows(HINSTANCE hInstance,int nShowCmd)
{
    HWND hwnd;
    HMENU hmenu;
    hmenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
    hwnd=CreateWindow(clsname,"Application",WS_OVERLAPPEDWINDOW,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

    if(!hwnd)
        return FALSE;

    SetMenu(hwnd,hmenu);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{

    switch(msg)
    {
    case WM_CLOSE:
        if(IDYES==MessageBox(hwnd,"Exit the application?","message",MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;

}


其实加一句SetMenu就行了

作者: maoxing63570   发布时间: 2011-12-11

简单一点,把这里修改一下
wndclass.lpszMenuName=NULL;
 
在resource View中添加Menu资源

作者: VisualEleven   发布时间: 2011-12-11

引用 2 楼 visualeleven 的回复:
简单一点,把这里修改一下
wndclass.lpszMenuName=NULL;

在resource View中添加Menu资源



这个我是知道的,可是现在在学习API,所以想深入了解下,还是谢谢你。

作者: ivanwu1020   发布时间: 2011-12-11

引用 1 楼 maoxing63570 的回复:
C/C++ code


#include "stdafx.h"
#include <windows.h>
#pragma comment(linker,"/subsystem:windows")
#include "resource.h"

const char clsname[]="mywindows";

LRESULT CALLBACK WndProc(HWND hwn……


能写下这段代码么,
BOOL SetMenu(HWND hWnd,HMENU hMenu)
我知道这是给指定窗口设置菜单,那么hMenu这个菜单句柄通过什么函数获得或者创建?谢谢了

作者: ivanwu1020   发布时间: 2011-12-11

C/C++ code

BOOL WINAPI SetMenu(
  __in      HWND hWnd,
  __in_opt  HMENU hMenu
);


函数作用为:Assigns a new menu to the specified window.
注意保证hMenu已正确加载。

作者: fight_in_dl   发布时间: 2011-12-11

引用 5 楼 fight_in_dl 的回复:
C/C++ code


BOOL WINAPI SetMenu(
__in HWND hWnd,
__in_opt HMENU hMenu
);


函数作用为:Assigns a new menu to the specified window.
注意保证hMenu已正确加载。


前辈:
我想了解下 hMenu=LoadMenu(hinstance,"menuname")这个第二个参数菜单名是怎么和我要设计的菜单关联起来的,也就是说菜单的定义过程。

作者: ivanwu1020   发布时间: 2011-12-11

hMenu=LoadMenu(hinstance,"menuname") LoadMenu是从资源中加载菜单
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
 LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
CreateMenu
InsertMenuItem
SetMenu

作者: gameslq   发布时间: 2011-12-11

引用 7 楼 gameslq 的回复:
hMenu=LoadMenu(hinstance,"menuname") LoadMenu是从资源中加载菜单
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
CreateMenu
InsertMenuItem……


我就是想用API函数手动的写菜单,能帮我写个样板么?
格式我在一楼写了,先谢谢了。

作者: ivanwu1020   发布时间: 2011-12-11

引用 8 楼 ivanwu1020 的回复:

引用 7 楼 gameslq 的回复:
hMenu=LoadMenu(hinstance,"menuname") LoadMenu是从资源中加载菜单
使用LoadMenu前需要在资源中定义好资源,比如资源ID是IDM_MAINMENU
就可以
LoadMenu(hinstance,MAKEINTRESOURCE(IDM_MAINMENU))
如果用API建立菜单需要用函数
Creat……

1#不是把代码给你了吗?自己没有试一下吗?

作者: VisualEleven   发布时间: 2011-12-11