DirectUI界面编程(六)实现右键弹出菜单

本节向大家介绍一下右键弹出菜单是如何实现的。效果如下,在窗口中点击鼠标右键弹出菜单,点击菜单项能够响应菜单点击事件。

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工程)

上一篇:vc++ 如何添加右键弹出菜单


下一篇:win32进阶之路:程序托盘图标+右键弹出菜单