即使没有顶级菜单栏,也可以使用菜单,你可以让弹出菜单出现在屏幕的任何位置。一种途径是作为对鼠标右键单击响应来激活相应的弹出菜单,这就是浮动菜单。
注意比较一下普通菜单和浮动菜单的区别,普通菜单可以看我的上一篇博客。
下面来讲讲步骤,步骤跟普通菜单的步骤基本一直,不过有一点值得注意,在编辑菜单选项的时候,应该...,还是截图吧,一目了然
也就是说,把普通菜单的popmenu当成了子菜单选项
如果你像下面这样编辑的话:制作浮动菜单的时候,只能选取某一个子菜单。
效果则会变成这样:
下面小牛试刀:
先看看代码:
#include<windows.h>#include"resource.h"LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter);HINSTANCE hInst;int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state){ static TCHAR szAppName[]=TEXT("sample"); HWND hwnd; MSG msg; WNDCLASS wndclass; hInst=hInstance; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hIcon=LoadIcon(NULL,IDI_ERROR); wndclass.hInstance=hInstance; wndclass.lpfnWndProc=WindowProc; wndclass.lpszClassName=szAppName; wndclass.lpszMenuName=NULL; wndclass.style=CS_HREDRAW|CS_VREDRAW; if(!RegisterClass(&wndclass)) { MessageBox(NULL,TEXT("the program require the window nt"),TEXT("tips"),MB_ICONERROR); return 0; } hwnd=CreateWindow( szAppName, // registered class name TEXT("this is title"), // window name WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // horizontal position of window CW_USEDEFAULT, // vertical position of window CW_USEDEFAULT, // window width CW_USEDEFAULT, // window height NULL, // handle to parent or owner window NULL, // menu handle or child identifier hInstance, // handle to application instance NULL // window-creation data); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;}LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter){ static HMENU hMenu1; HDC hdc; PAINTSTRUCT ps; static POINT point; static HBRUSH hbrush=CreateSolidBrush(RGB(0,255,0));//默认画刷是绿色的 static RECT rect; switch(uMsg) { case WM_CREATE: hMenu1=LoadMenu(hInst,MAKEINTRESOURCE(MENUIID));//在这里加载喔,跟普通菜单有点不一样 hMenu1=GetSubMenu(hMenu1,0);//选择子菜单,这里添加资源的时候,有一点技巧 return 0; case WM_RBUTTONUP: GetCursorPos(&point);//获取鼠标当前位置 TrackPopupMenu(hMenu1,TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,0,hwnd,NULL);//浮动菜单 return 0; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_FILE_NEW: case ID_FILE_OPEN: case ID_FILE_SAVE: case ID_FILE_SAVEAS: MessageBox(NULL,TEXT("你选择了File中的选项"),TEXT("别紧张,提示而已"),MB_OK);//弹出一个提示 return 0; case ID_EDIT_CUT: case ID_EDIT_COPY: case ID_EDIT_PASTE: MessageBeep(MB_ICONEXCLAMATION);//发出系统清脆的声音 return 0; case ID_BACKGROUND_RED: hbrush=CreateSolidBrush(RGB(255,0,0)); //SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(1));//这个画刷只能画灰色和黑色 InvalidateRect(hwnd,&rect,TRUE);//使得客户区无效,从而传递WM_PAINT消息 return 0; case ID_BACKGROUND_GREEN: hbrush=CreateSolidBrush(RGB(0,255,0));//绿色的画刷 InvalidateRect(hwnd,&rect,TRUE);//使得客户区无效,从而传递WM_PAINT消息 return 0; case ID_BACKGROUND_BLUE: hbrush=CreateSolidBrush(RGB(0,0,255));//蓝色的画刷 InvalidateRect(hwnd,&rect,TRUE);//使得客户区无效,从而传递WM_PAINT消息 return 0; } return 0; case WM_PAINT: GetClientRect(hwnd,&rect); hdc=BeginPaint(hwnd,&ps); FillRect(hdc,&rect,hbrush); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam);}
效果图不截,你们也应该知道是怎样了的吧。