DirectUI界面编程(六)实现右键弹出菜单
本節向大家介紹一下右鍵彈出菜單是如何實現的。效果如下,在窗口中點擊鼠標右鍵彈出菜單,點擊菜單項能夠響應菜單點擊事件。
使用Duilib庫實現的彈出菜單,實際上也是一個Windows窗口,因此我們需要創建兩個窗口(主窗口和菜單窗口),然后響應主窗口的鼠標右鍵點擊事件,在主窗口右鍵點擊事件的響應函數中獲取鼠標位置,將菜單窗口的位置調整為當前鼠標位置,同時把窗口設置為顯示即可。
菜單窗口的xml布局文件內容如下:
<?xml version="1.0" encoding="utf-8"?> <Window bktrans="true" size="120,102"><Font name="宋體" size="13" bold="false"/><Font name="宋體" size="12"/> <VerticalLayout bkimage="file='menu_bk.png' corner='40,8,8,8'" hole="false"><List header="hidden" inset="8,8,8,8" itemhotimage="file='menu_hot_bk.png' corner='2,2,2,2'" itemdisabledbkcolor="#20000000"><ListContainerElement name="menu_Open" height="22" inset="40,0,0,0"><Label text="打開" mouse="false"/></ListContainerElement><ListContainerElement name="menu_Mark" height="22" inset="40,0,0,0"><Label text="標注" mouse="false"/></ListContainerElement><ListContainerElement name="menu_Delete" height="22" inset="40,0,0,0"><Label text="刪除" mouse="false"/></ListContainerElement><ListContainerElement name="menu_Test" height="22" inset="40,0,0,0"><Label text="測試" mouse="false"/></ListContainerElement></List> </VerticalLayout> </Window>我們需要編寫一個菜單窗口類繼承WindowImplBase來加載xml界面。和上節不同的是我們把這個類的聲明和成員函數的定義放在一個單獨的頭文件中,這樣代碼容易維護一些。新建頭文件MenuWnd.h:
#ifndef _MENUWND_H__ #define _MENUWND_H__ #include "win_impl_base.hpp" #include <Windows.h> class MenuWnd : public WindowImplBase { LPCTSTR GetWindowClassName() const{return L"MenuWnd";} UINT GetClassStyle() const{return UI_CLASSSTYLE_FRAME|CS_DBLCLKS;}tString GetSkinFile(){return L"tutorial6_menu.xml";}void Notify(TNotifyUI& msg){ if(msg.sType == L"itemclick"){//響應菜單事件::MessageBox(NULL,L"hello",L"hello",MB_OK);this->ShowWindow(false);} } }; #endif接下來是主窗口,主窗口的xml界面布局文件內容和上節相同,僅僅是主窗口類的定義有些變化,我們同樣把主窗口類的聲明和定義放在一個單獨的頭文件MyWnd.h中:
#ifndef _MYWND_H__ #define _MYWND_H__ #include "win_impl_base.hpp" #include "MenuWnd.h" #include <Windows.h> class MyWnd : public WindowImplBase { public: MyWnd(){pMenu = new MenuWnd();pMenu->Create(m_hWnd,L"Menu",WS_POPUP,WS_EX_TOOLWINDOW);pMenu->ShowWindow(false);}LPCTSTR GetWindowClassName() const{return L"MyWnd";}UINT GetClassStyle() const{return UI_CLASSSTYLE_FRAME|CS_DBLCLKS;}tString GetSkinFile(){return L"tutorial6.xml";}void Notify(TNotifyUI& msg){if(msg.sType == L"click"){if(msg.pSender->GetName() == L"CloseBtn"){::PostQuitMessage(0);}else if(msg.pSender->GetName() == L"MinBtn"){::SendMessage(m_hWnd,WM_SYSCOMMAND, SC_MINIMIZE, 0);}}} LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam){switch(uMsg){case WM_RBUTTONDOWN:OnRButtonDown(wParam,lParam);return 0; default:return WindowImplBase::HandleMessage(uMsg,wParam,lParam);}}void OnRButtonDown(WPARAM wParam, LPARAM lParam){ int xPos = LOWORD(lParam); int yPos = HIWORD(lParam);POINT pt = {xPos,yPos};ClientToScreen(m_hWnd,&pt); pMenu->ShowWindow(true);::SetWindowPos(pMenu->GetHWND(), NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); } LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){//獲取鼠標點擊位置int xPos = LOWORD(lParam); int yPos = HIWORD(lParam);//將位置轉換為屏幕坐標POINT pt = {xPos,yPos};ClientToScreen(m_hWnd,&pt); RECT menuWndRect = {0};::GetWindowRect(pMenu->GetHWND(),&menuWndRect);if(pt.x <= menuWndRect.left || pt.x >= menuWndRect.right || pt.y <= menuWndRect.top || pt.y >= menuWndRect.bottom){pMenu->ShowWindow(false);}bHandled = FALSE;return 0;}private:MenuWnd* pMenu;}; #endif在主窗口類中定義一個MenuWnd指針類型的成員變量,在構造函數中創建菜單窗口設置顯示風格為隱藏,重寫父類的HandleMessage函數,響應主窗口鼠標右鍵點擊事件,在右鍵點擊事件的響應函數中重新設置菜單位置,并把窗口顯示風格設置為顯示。
最后在WinMain函數中創建并顯示主窗口:
//tutorial6.cpp #include <Windows.h> #include "MyWnd.h" INT WinMain(HINSTANCE hInst,HINSTANCE hPreInst,LPSTR lpCmdLine,INT Show) {CPaintManagerUI::SetInstance(hInst);CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetResourcePath());//創建主窗口MyWnd* pFrame = new MyWnd();pFrame->Create(NULL,L"Tutorial6",UI_WNDSTYLE_FRAME,WS_EX_WINDOWEDGE);pFrame->CenterWindow(); pFrame->ShowWindow(true); CPaintManagerUI::MessageLoop();return 0; }博文源碼:https://github.com/rongbo-j/duilib-tutorial
(參考tutorial6工程)
轉載于:https://www.cnblogs.com/lanzhi/p/6468608.html
總結
以上是生活随笔為你收集整理的DirectUI界面编程(六)实现右键弹出菜单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mongodb安装搭建Replica S
- 下一篇: 有趣又好玩的glm库