標籤:右鍵菜單
一 右鍵菜單
1 右鍵菜單
當在視窗點擊滑鼠右鍵時,彈出的菜單。
2 右鍵菜單的使用
2.1 建立菜單
CreatePopupMenu
2.2 菜單增加
AppendMenu
2.3 菜單的顯示、
BOOL TrackPopupMenu( HMENU hMenu, //顯示的菜單控制代碼 UINT uFlags, //顯示的方式 int x, //菜單的X螢幕座標 int y, //菜單的Y螢幕座標 int nReserved, //保留,必須為0 HWND hWnd, //處理功能表命令的視窗控制代碼 CONST RECT *prcRect ); //忽略
2.4 菜單的命令處理
WM_COMMAND
2.5 使用右鍵菜單的位置
2.5.1 WM_RBUTTONUP 訊息
在WM_RBUTTONUP中,添加菜單的建立及顯示,
右鍵訊息座標,轉換成螢幕座標使用.
ClientToScreen.
2.5.2 WM_CONTEXTMENU 訊息
用於顯示右鍵的菜單的訊息.
WPARAM - 右鍵抬起時對應視窗控制代碼
LPARAM - 右鍵抬起時滑鼠的螢幕座標位置
LOWORD(lParam) - X螢幕座標
HIWORD(lParam) - Y螢幕座標
2.5.3 WM_RBUTTONUP和WM_CONTEXTMENU對比
1) 座標系不同, WM_RBUTTONUP客戶區座標,WM_CONTEXTMENU螢幕座標
2) 先有WM_RBUTTONUP訊息,後有WM_CONTEXTMENU訊息
/* File : winPopMenu.cpp * Auth : sjin * Date : 20140706 * Mail : [email protected] */ #include <Windows.h> #include <stdio.h>HINSTANCE g_hInst = NULL;void OnRButtonUp( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ){ // 建立彈出式菜單 HMENU hPopMenu = CreatePopupMenu( ); // 增加功能表項目 AppendMenu( hPopMenu, MF_STRING, 1001, "測試1"); AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL ); AppendMenu( hPopMenu, MF_STRING, 1002, "退出"); // 擷取菜單位置 POINT point = { 0 }; point.x = LOWORD( lParam ); point.y = HIWORD( lParam ); ClientToScreen( hWnd, &point ); // 顯示菜單 TrackPopupMenu( hPopMenu, TPM_LEFTALIGN, point.x, point.y, 0, hWnd, NULL );}void OnContextMenu( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ){ // 建立彈出式菜單 HMENU hPopMenu = CreatePopupMenu( ); // 增加功能表項目 AppendMenu( hPopMenu, MF_STRING, 1001, "測試2"); AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL ); AppendMenu( hPopMenu, MF_STRING, 1002, "退出"); // 座標擷取 int nX = LOWORD( lParam ); int nY = HIWORD( lParam ); // 顯示菜單 TrackPopupMenu( hPopMenu, TPM_LEFTALIGN, nX, nY, 0, hWnd, NULL ); // 刪除菜單 DestroyMenu( hPopMenu );}void OnCommand( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ){ int nCmdID = LOWORD( wParam ); switch( nCmdID ) { case 1001: MessageBox( NULL, "Hello Popmenu", "PopMenu", MB_OK ); break; case 1002: PostQuitMessage( 0 ); break; }}LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ){ switch( nMsg ) { case WM_RBUTTONUP: //OnRButtonUp( hWnd, nMsg, wParam, lParam ); break; case WM_CONTEXTMENU: OnContextMenu( hWnd, nMsg, wParam, lParam ); break; case WM_COMMAND: OnCommand( hWnd, nMsg, wParam, lParam ); break; case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, nMsg, wParam, lParam );}BOOL RegisterWnd( LPSTR pszClassName ){ WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hbrBackground = HBRUSH(COLOR_WINDOW); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInst; wce.lpfnWndProc = WndProc; wce.lpszClassName = pszClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW|CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if( 0 == nAtom ) { return FALSE; } return TRUE;}HWND CreateWnd( LPSTR pszClassName ){ HWND hWnd = CreateWindowEx( 0, pszClassName, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL ); return hWnd;}void DisplayWnd( HWND hWnd ){ ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd );}void Message( ){ MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); }}int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message( ); return 0;}