Menu-弹出菜单

menu 弹出菜单事件

步骤:

  1. XMenu_Create()创建弹出菜单.
  2. XMenu_AddItem()添加菜单项.
  3. XMenu_Popup()显示弹出菜单.

示例代码:

#pragma comment(lib, "XCGUI.lib")
#include "xcgui.h"
class CMyWindowMenu
{
public:
HWINDOW m_hWindow;
CMyWindowMenu()
{
Init();
}
void Init()
{
m_hWindow = XWnd_Create(0, 0, 300, 200, L"炫彩界面库窗口", NULL, window_style_default);
HELE hButton = XBtn_Create(20, 50, 80, 20, L"弹出菜单", m_hWindow);
XEle_RegEventCPP1(hButton, XE_BNCLICK, &CMyWindowMenu::OnBtnClick);
//通过UI元素接收菜单事件
//XEle_RegEventCPP(hButton, XE_MENU_DRAW_BACKGROUND, &CMyWindowMenu::OnWndMenuDrawBackground);
//XEle_RegEventCPP(hButton, XE_MENU_DRAWITEM, &CMyWindowMenu::OnWndMenuDrawItem);
//XEle_RegEventCPP(hButton, XE_MENU_POPUP, &CMyWindowMenu::OnWndMenuPopup);
//XEle_RegEventCPP(hButton, XE_MENU_POPUP_WND, &CMyWindowMenu::OnWndMenuPopupWnd);
//通过窗口接收菜单事件
XWnd_RegEventCPP(m_hWindow, XWM_MENU_POPUP, &CMyWindowMenu::OnWndMenuPopup);
XWnd_RegEventCPP(m_hWindow, XWM_MENU_POPUP_WND, &CMyWindowMenu::OnWndMenuPopupWnd);
XWnd_RegEventCPP(m_hWindow, XWM_MENU_SELECT, &CMyWindowMenu::OnWndMenuSelect);
XWnd_RegEventCPP(m_hWindow, XWM_MENU_EXIT, &CMyWindowMenu::OnWndMenuExit);
XWnd_RegEventCPP(m_hWindow, XWM_MENU_DRAW_BACKGROUND, &CMyWindowMenu::OnWndMenuDrawBackground);
XWnd_RegEventCPP(m_hWindow, XWM_MENU_DRAWITEM, &CMyWindowMenu::OnWndMenuDrawItem);
XWnd_ShowWindow(m_hWindow, SW_SHOW);
}
int OnBtnClick(HELE hEventEle, BOOL *pbHandled)
{
RECT rcButton;
XEle_GetRect(hEventEle, &rcButton);
POINT pt = { rcButton.left,rcButton.bottom };
//创建弹出菜单
HMENUX hMenu = XMenu_Create();
//XMenu_EnableDrawItem(hMenu,TRUE);
//XMenu_EnableDrawBackground(hMenu,TRUE);
XMenu_SetBkImage(hMenu, XImage_LoadFile(L"image\\111.jpg"));
XMenu_AddItemIcon(hMenu, 201, L"111", XC_ID_ROOT, XImage_LoadFile(L"image\\icon.png"), menu_item_flags_check);
XMenu_AddItem(hMenu, 202, L"222");
//XMenu_AddItem(hMenu, 0, L"",XC_ID_ROOT,menu_item_flags_separator); //分隔栏
XMenu_AddItem(hMenu, 203, L"333");
XMenu_AddItem(hMenu, 204, L"444", 203);
XMenu_AddItem(hMenu, 205, L"555", 203);
XMenu_AddItem(hMenu, 206, L"666", 203);
XMenu_AddItem(hMenu, 207, L"777", 206);
XMenu_AddItem(hMenu, 208, L"888", 206);
XMenu_AddItem(hMenu, 209, L"999", 206);
::ClientToScreen(XWnd_GetHWND(m_hWindow), &pt);
XMenu_Popup(hMenu, XWnd_GetHWND(m_hWindow), pt.x, pt.y); //由窗口接收菜单事件
//XMenu_Popup(hMenu, XWnd_GetHWND(m_hWindow), pt.x, pt.y, hButton); //由按钮元素接收菜单事件
return 0;
}
int OnWndMenuPopup(HMENUX hMenu, BOOL *pbHandled)
{
XTRACE("menu-XWM_MENU_POPUP\n");
return 0;
}
int OnWndMenuPopupWnd(HMENUX hMenu, menu_popupWnd_* pInfo, BOOL *pbHandled)
{
XTRACE("menu-XWM_MENU_POPUP_WND nParent=%d \n", pInfo->nParentID);
return 0;
}
int OnWndMenuSelect(int nID, BOOL *pBool)
{
XTRACE("menu-XWM_MENU_SELECT item:%d\n", nID);
return 0;
}
int OnWndMenuExit(BOOL *pbHandled)
{
XTRACE("menu-XWM_MENU_EXIT exit \n");
return 0;
}
int OnWndMenuDrawBackground(HDRAW hDraw, menu_drawBackground_ *pInfo, BOOL *pbHandled)
{
//自绘背景
*pbHandled = TRUE;
return 0;
}
int OnWndMenuDrawItem(HDRAW hDraw, menu_drawItem_* pInfo, BOOL *pbHandled)
{
//自绘菜单项
*pbHandled = TRUE;
return 0;
}
};
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
XInitXCGUI(TRUE);
CMyWindowMenu MyWindow;
return TRUE;
}
#define XE_BNCLICK
按钮点击事件.
Definition: XCGUI.h:939
@ window_style_default
窗口样式-控制按钮: 居中 图标, 标题, 关闭按钮, 最大化按钮, 最小化按钮
Definition: XCGUI.h:398
HELE WINAPI XBtn_Create(int x, int y, int cx, int cy, const wchar_t *pName, HXCGUI hParent)
按钮_创建 创建按钮元素
Definition: ButtonUI.cpp:1765
void WINAPI XEle_GetRect(HELE hEle, __out RECT *pRect)
元素_取坐标 获取元素坐标.
Definition: EleUI.cpp:3592
HIMAGE WINAPI XImage_LoadFile(const wchar_t *pFileName)
图片_加载从文件 加载图片从文件.
Definition: ImageFrame.cpp:1014
#define XC_ID_ROOT
根节点
Definition: XCGUI.h:461
void WINAPI XExitXCGUI()
炫彩_退出 退出界面库释放资源. 不可在dllmain()中卸载, 否则会死锁
Definition: App.cpp:572
void WINAPI XRunXCGUI()
炫彩_运行 运行消息循环,当炫彩窗口数量为0时退出.
Definition: App.cpp:564
BOOL WINAPI XInitXCGUI(BOOL bD2D)
炫彩_初始化 初始化界面库.
Definition: App.cpp:557
XEle_RegEventCPP1(hEle, nEvent, memberFunction)
元素_注册事件CPP1 注册元素事件,将类成员函数作为事件响应函数.函数不省略参数.
XWnd_RegEventCPP(hWindow, nEvent, memberFunction)
窗口_注册事件CPP 注册窗口事件,将类成员函数作为事件响应函数.函数省略参数窗口自身句柄hWindow.
#define XWM_MENU_EXIT
菜单退出
Definition: XCGUI.h:718
#define XWM_MENU_POPUP_WND
菜单弹出窗口
Definition: XCGUI.h:710
#define XWM_MENU_DRAWITEM
绘制菜单项事件, 启用该功能需要调用XMenu_EnableDrawItem().
Definition: XCGUI.h:726
#define XWM_MENU_POPUP
菜单弹出
Definition: XCGUI.h:706
#define XWM_MENU_SELECT
菜单选择
Definition: XCGUI.h:714
#define XWM_MENU_DRAW_BACKGROUND
绘制菜单背景, 启用该功能需要调用XMenu_EnableDrawBackground().
Definition: XCGUI.h:722
BOOL WINAPI XMenu_Popup(HMENUX hMenu, HWND hParentWnd, int x, int y, HELE hParentEle, menu_popup_position_ nPosition)
菜单_弹出 弹出菜单.
Definition: MenuUI.cpp:923
void WINAPI XMenu_SetBkImage(HMENUX hMenu, HIMAGE hImage)
菜单_置背景图片 设置菜单背景图片.
Definition: MenuUI.cpp:948
void WINAPI XMenu_AddItemIcon(HMENUX hMenu, int nID, const wchar_t *pText, int nParentID, HIMAGE hIcon, int nFlags)
菜单_添加项图标 添加菜单项.
Definition: MenuUI.cpp:790
HMENUX WINAPI XMenu_Create()
菜单_创建 创建菜单.默认弹出菜单窗口关闭后自动销毁.
Definition: MenuUI.cpp:762
void WINAPI XMenu_AddItem(HMENUX hMenu, int nID, const wchar_t *pText, int nParentID, int nFlags)
菜单_添加项 添加菜单项.
Definition: MenuUI.cpp:777
HWND WINAPI XWnd_GetHWND(HWINDOW hWindow)
窗口_取HWND 获取HWND句柄.
Definition: WindowUI.cpp:6168
HWINDOW WINAPI XWnd_Create(int x, int y, int cx, int cy, const wchar_t *pTitle, HWND hWndParent, int XCStyle)
窗口_创建 创建窗口
Definition: WindowUI.cpp:5964
BOOL WINAPI XWnd_ShowWindow(HWINDOW hWindow, int nCmdShow)
@窗口_显示扩展 显示隐藏及控制窗口最大化还原等
Definition: WindowUI.cpp:6387
菜单背景自绘结构
Definition: XCGUI.h:2504
菜单项自绘结构
Definition: XCGUI.h:2516
菜单-弹出窗口信息
Definition: XCGUI.h:2494
int nParentID
父项ID
Definition: XCGUI.h:2498

截图: