Menu-弹出菜单自绘

示例代码:

class CMyWindowMenu
{
public:
HWINDOW m_hWindow;
HELE m_hButton;
CMyWindowMenu()
{
Init();
}
void Init()
{
m_hWindow = XWnd_Create(0, 0, 300, 200, L"炫彩界面库窗口", NULL, window_style_default);
m_hButton = XBtn_Create(20, 50, 80, 30, L"弹出菜单", m_hWindow);
XEle_RegEventCPP1(m_hButton, XE_BNCLICK, &CMyWindowMenu::OnBtnClick);
// XWnd_RegEventCPP(m_hWindow, XWM_MENU_DRAW_BACKGROUND, &CMyWindowMenu::OnWndMenuDrawBackground);
// XWnd_RegEventCPP(m_hWindow, XWM_MENU_DRAWITEM, &CMyWindowMenu::OnWndMenuDrawItem);
XEle_RegEventCPP(m_hButton, XE_MENU_DRAW_BACKGROUND, &CMyWindowMenu::OnWndMenuDrawBackground);
XEle_RegEventCPP(m_hButton, XE_MENU_DRAWITEM, &CMyWindowMenu::OnWndMenuDrawItem);
XWnd_AdjustLayout(m_hWindow);
XWnd_ShowWindow(m_hWindow, SW_SHOW);
}
BOOL 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_AddItemIcon(hMenu, 201, L"111", XC_ID_ROOT, XImage_LoadFile(L"image\\logo.png"), menu_item_flag_check);
XMenu_AddItem(hMenu, 202, L"222", 0, menu_item_flag_check);
//XMenu_AddItem(hMenu, 0, L"",XC_ID_ROOT,menu_item_flag_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, m_hButton);
return 0;
}
int OnWndMenuDrawBackground(HDRAW hDraw, menu_drawBackground_ *pInfo, BOOL *pbHandled)
{
*pbHandled = TRUE;
RECT rect;
XWnd_GetClientRect(pInfo->hWindow, &rect);
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(59, 64, 68,255));
XDraw_FillRect(hDraw, &rect);
return 0;
}
int OnWndMenuDrawItem(HDRAW hDraw, menu_drawItem_* pInfo, BOOL *pbHandled)
{
*pbHandled = TRUE;
int leftWidth = XMenu_GetLeftWidth(pInfo->hMenu);
int leftSpaceText = XMenu_GetLeftSpaceText(pInfo->hMenu);
if (pInfo->nState & menu_item_flag_separator) //分割栏
{
XDraw_SetBrushColor(hDraw, RGB(82, 88, 94));
XDraw_DrawLine(hDraw, pInfo->rcItem.left + 3, pInfo->rcItem.top,
pInfo->rcItem.right - 3, pInfo->rcItem.top);
return 0;
}
if (pInfo->nState & menu_item_flag_select) //选择项
{
XDraw_GradientFill2(hDraw, &pInfo->rcItem, COLORREF_MAKE(63, 151, 216, 255), COLORREF_MAKE(48, 134, 198, 255), GRADIENT_FILL_RECT_V);
} else
{
{
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(82, 88, 94,255));
XDraw_DrawLine(hDraw, pInfo->rcItem.left + 3, pInfo->rcItem.bottom - 1,
pInfo->rcItem.right - 3, pInfo->rcItem.bottom - 1);
}
}
if (pInfo->nState & menu_item_flag_popup) //弹出项
{
//绘制右三角
POINT pt[3];
pt[0].x = pInfo->rcItem.right - 12;
pt[0].y = pInfo->rcItem.top + 5;
pt[1].x = pInfo->rcItem.right - 12;
pt[1].y = pInfo->rcItem.top + 15;
pt[2].x = pInfo->rcItem.right - 7;
pt[2].y = pInfo->rcItem.top + 10;
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(0, 0, 0, 255));
XDraw_FillPolygon(hDraw, pt, 3);
}
if (pInfo->nState &menu_item_flag_check) //勾选
{
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(0, 0, 0,255));
XDraw_DrawLine(hDraw, pInfo->rcItem.left + 5, pInfo->rcItem.top + 8, pInfo->rcItem.left + 9, pInfo->rcItem.top + 13);
XDraw_DrawLine(hDraw, pInfo->rcItem.left + 9, pInfo->rcItem.top + 13, pInfo->rcItem.left + 17, pInfo->rcItem.top + 5);
}
RECT rc = pInfo->rcItem;
rc.left = leftWidth + 5;
const wchar_t *pText = pInfo->pText;
if (pInfo->nState & menu_item_flag_disable)//禁用项
{
if (pText)
{
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(100, 100, 100,255));
XDraw_SetTextAlign(hDraw, DT_VCENTER | DT_SINGLELINE);
XDraw_DrawText(hDraw, pText, wcslen(pInfo->pText), &rc);
}
} else
{
if (pInfo->hIcon)
{
int iconWidth = XImage_GetHeight(pInfo->hIcon);
int iconHeight = XImage_GetWidth(pInfo->hIcon);
int height = pInfo->rcItem.bottom - pInfo->rcItem.top;
if (height >= 2 && iconWidth >= 2 && iconHeight >= 2)
{
int top = (height - iconHeight) / 2;
int left = (leftWidth - iconWidth) / 2;
if (top < 0) top = 0;
if (left < 0) left = 0;
XDraw_Image(hDraw, pInfo->hIcon, left, pInfo->rcItem.top + top);
}
}
if (pText)
{
XDraw_SetBrushColor(hDraw, COLORREF_MAKE(238, 238, 238,255));
XDraw_SetTextAlign(hDraw, DT_VCENTER | DT_SINGLELINE);
XDraw_DrawText(hDraw, pText, wcslen(pInfo->pText), &rc);
}
}
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
#define XE_MENU_DRAW_BACKGROUND
绘制菜单背景, 启用该功能需要调用XMenu_EnableDrawBackground().
Definition: XCGUI.h:927
#define XE_MENU_DRAWITEM
绘制菜单项事件, 启用该功能需要调用XMenu_EnableDrawItem().
Definition: XCGUI.h:931
@ 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 XDraw_DrawLine(HDRAW hDraw, int x1, int y1, int x2, int y2)
绘制_线条 绘制线条.
Definition: DrawDC.cpp:2916
void WINAPI XDraw_FillPolygon(HDRAW hDraw, in_buffer_ POINT *points, int nCount)
绘制_填充多边形 填充多边形.
Definition: DrawDC.cpp:3076
void WINAPI XDraw_FillRect(HDRAW hDraw, RECT *pRect)
@绘制_填充矩形
Definition: DrawDC.cpp:2633
void WINAPI XDraw_DrawText(HDRAW hDraw, const wchar_t *pString, int nCount, RECT *pRect)
@绘制_文本指定矩形
Definition: DrawDC.cpp:3331
void WINAPI XDraw_Image(HDRAW hDraw, HIMAGE hImageFrame, int x, int y)
绘制_图片 绘制图片.
Definition: DrawDC.cpp:3107
void WINAPI XDraw_SetTextAlign(HDRAW hDraw, int nFlags)
绘制_置文本对齐 设置文本对齐.
Definition: DrawDC.cpp:2556
void WINAPI XDraw_SetBrushColor(HDRAW hDraw, COLORREF color)
绘制_置画刷颜色 设置画刷颜色.
Definition: DrawDC.cpp:2536
void WINAPI XDraw_GradientFill2(HDRAW hDraw, RECT *pRect, COLORREF color1, COLORREF color2, int mode)
绘制_渐变填充2 渐变填充,从一种颜色过渡到另一种颜色.
Definition: DrawDC.cpp:2831
void WINAPI XEle_GetRect(HELE hEle, __out RECT *pRect)
元素_取坐标 获取元素坐标.
Definition: EleUI.cpp:3592
int WINAPI XImage_GetHeight(HIMAGE hImage)
图片_取高度 获取图片高度.
Definition: ImageFrame.cpp:1503
HIMAGE WINAPI XImage_LoadFile(const wchar_t *pFileName)
图片_加载从文件 加载图片从文件.
Definition: ImageFrame.cpp:1014
int WINAPI XImage_GetWidth(HIMAGE hImage)
图片_取宽度 获取图片宽度.
Definition: ImageFrame.cpp:1494
#define XC_ID_ERROR
ID错误
Definition: XCGUI.h:462
#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
@ menu_item_flag_separator
分隔栏 ID号任意,ID号被忽略
Definition: XCGUI.h:1354
@ menu_item_flag_popup
弹出
Definition: XCGUI.h:1352
@ menu_item_flag_disable
禁用
Definition: XCGUI.h:1356
@ menu_item_flag_select
选择或鼠标停留
Definition: XCGUI.h:1346
@ menu_item_flag_check
勾选
Definition: XCGUI.h:1350
XEle_RegEventCPP1(hEle, nEvent, memberFunction)
元素_注册事件CPP1 注册元素事件,将类成员函数作为事件响应函数.函数不省略参数.
XEle_RegEventCPP(hEle, nEvent, memberFunction)
元素_注册事件CPP 注册元素事件,将类成员函数作为事件响应函数.函数省略元素自身句柄hEle.
BOOL WINAPI XMenu_Popup(HMENUX hMenu, HWND hParentWnd, int x, int y, HELE hParentEle, menu_popup_position_ nPosition)
菜单_弹出 弹出菜单.
Definition: MenuUI.cpp:923
int WINAPI XMenu_GetLeftWidth(HMENUX hMenu)
菜单_取左侧宽度 获取左侧区域宽度.
Definition: MenuUI.cpp:1067
int WINAPI XMenu_GetNextSiblingItem(HMENUX hMenu, int nID)
菜单_取下一个兄弟项 获取下一个兄弟项.
Definition: MenuUI.cpp:858
int WINAPI XMenu_GetLeftSpaceText(HMENUX hMenu)
菜单_取左侧文本间隔 获取菜单项文本左间隔.
Definition: MenuUI.cpp:1077
void WINAPI XMenu_AddItemIcon(HMENUX hMenu, int nID, const wchar_t *pText, int nParentID, HIMAGE hIcon, int nFlags)
菜单_添加项图标 添加菜单项.
Definition: MenuUI.cpp:790
void WINAPI XMenu_EnableDrawBackground(HMENUX hMenu, BOOL bEnable)
菜单_启用用户绘制背景 是否有用户绘制菜单背景,如果启用XWM_MENU_DRAW_BACKGROUND和XE_MENU_DRAW_BACKGROUND事件有效.
Definition: MenuUI.cpp:898
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
void WINAPI XMenu_EnableDrawItem(HMENUX hMenu, BOOL bEnable)
菜单_启用用户绘制项 是否有用户绘制菜单项,如果启用XWM_MENU_DRAWITEM和XE_MENU_DRAWITEM事件有效.
Definition: MenuUI.cpp:907
BOOL WINAPI XWnd_GetClientRect(HWINDOW hWindow, __out RECT *pRect)
窗口_取客户区坐标 获取窗口客户区坐标;如果是阴影窗口,那么不包含阴影部分.
Definition: WindowUI.cpp:6830
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
void WINAPI XWnd_AdjustLayout(HWINDOW hWindow)
窗口_调整布局 调整窗口布局, 当窗口显示状态才有效.
Definition: WindowUI.cpp:6718
BOOL WINAPI XWnd_ShowWindow(HWINDOW hWindow, int nCmdShow)
@窗口_显示扩展 显示隐藏及控制窗口最大化还原等
Definition: WindowUI.cpp:6387
菜单背景自绘结构
Definition: XCGUI.h:2504
HWINDOW hWindow
当前菜单项的窗口句柄
Definition: XCGUI.h:2508
菜单项自绘结构
Definition: XCGUI.h:2516
const wchar_t * pText
文本
Definition: XCGUI.h:2532
HIMAGE hIcon
菜单项图标
Definition: XCGUI.h:2530
HMENUX hMenu
菜单句柄
Definition: XCGUI.h:2518
int nState
状态 menu_item_flag_
Definition: XCGUI.h:2524
RECT rcItem
坐标
Definition: XCGUI.h:2528
int nID
ID
Definition: XCGUI.h:2522

截图: