#include "cmenu.h"
int CItem::ms_selectitem{ 0 };
WCHAR CMenu::m_windowclassname[]{ L"CMENU" };
HBRUSH CMenu::m_brush; //背景画刷
HBRUSH CMenu::m_selectbrush; //选中的背景画刷
HFONT CMenu::m_font;
HPEN CMenu::m_selectpen; //移动选中项目时圆角矩形画笔同选中的背景色一致
//*****************************CMainmenu********************
CMainmenu* CMainmenu::createcmainmenu(const WCHAR* name, int place, int top)
{
CMainmenu* p = new CMainmenu;
if (p)
{
wcscpy_s(p->m_name, name);
p->m_place = place;
p->m_rect.left = TOOLSEBROADSPACING * gdpi + (TOOLSSPACINGWIDTH * gdpi + gtextwidth) * (place - 1);
p->m_rect.top = top + TOOLSEBROADSPACING * gdpi;
p->m_rect.right = p->m_rect.left + gtextwidth;
p->m_rect.bottom = top + MAX_TOOLS * gdpi - TOOLSEBROADSPACING * gdpi;
}
return p;
}
//******************CItem定义
CItem* CItem::createseparator( int line, int count )
{
CItem* p = new CItem;
if (!p)
return NULL;
p->m_line = line;
p->m_rect.left = groundspacing;
p->m_rect.right = gwindowwidth - groundspacing;
p->m_rect.top = line * groundspacing + (line - 1 - count) * gitemhight + count * 2;
p->m_rect.bottom = p->m_rect.top + 1;
return p;
}
void CItem::drawseparator(HDC hdc)
{
SelectObject(hdc, CMenu::m_selectpen);
MoveToEx(hdc, m_rect.left, m_rect.top, NULL);
LineTo(hdc, m_rect.right, m_rect.top);
MoveToEx(hdc, m_rect.left, m_rect.bottom, NULL);
LineTo(hdc, m_rect.right, m_rect.bottom);
//WCHAR s[50];
//wsprintfW(s, L"pCItem:%d ps:%d pd:%d", m_rect.left, m_rect.top, m_rect.bottom);
//MessageBoxW(NULL, s, L"DEBUG", MB_OK);
}
void CItem::drawitem(HDC hdc)
{
SelectObject(hdc, CMenu::m_font);
SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, m_itemname, -1, &m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
void CItem::drawitemselect(HDC hdc, int flag)
{
SelectObject(hdc, CMenu::m_font);
SetBkMode(hdc, TRANSPARENT);
if (flag)
{
SelectObject(hdc, CMenu::m_selectbrush);
SelectObject(hdc, CMenu::m_selectpen);
RoundRect(hdc, m_rect.left, m_rect.top, m_rect.right, m_rect.bottom, 6 * gdpi, 6 * gdpi);
}
else
FillRect(hdc, &m_rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
DrawText(hdc, m_itemname, -1, &m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
CItem* CItem::createitem(int id, int type, int line, const WCHAR* pname, int count)
{
CItem* p = new CItem;
if (!p)
return NULL;
p->m_itemtype = type; //菜单项,无下级菜单
p->m_itemid = id;
p->m_line = line;
p->m_rect.left = groundspacing;
p->m_rect.right = gwindowwidth - groundspacing;
p->m_rect.top = line * groundspacing + (line - 1 - count) * gitemhight + count * 2;
p->m_rect.bottom = p->m_rect.top + gitemhight;
if (pname)
wcscpy_s(p->m_itemname, pname);
return p;
}
void CItem::seticon(HICON icon)
{
}
void CItem::setname(WCHAR* pname)
{
if (pname)
wcscpy_s(m_itemname, pname);
}
int CItem::ifinrect(const RECT& rect, int cx, int cy)
{
if ((cx > rect.left) && (cx < rect.right))
{
if ((cy > rect.top) && (cy < rect.bottom))
return 1;
}
return 0;
}
CItem* CItem::serchid(CItem* pcitem, int id)
{
CItem* p = pcitem;
while (p)
{
if (p->m_itemid == id)
return p;
else
p = p->m_pnext;
}
return p;
}
void CItem::trackitem(HWND hwnd, int cx, int cy, HWNDtag* phwnd) //设置相应的状态(选中, 反转, 弹出菜单的处理,有WM_MOVEMOUSE调用跟踪
{
CItem* ps{ NULL };
CItem* pd{ NULL };
CItem* pt{ phwnd->m_pcitem };
while (pt)
{
if (ifinrect(pt->m_rect, cx, cy)) //选中的菜单项
pd = pt;
if (pt->m_itemid == ms_selectitem) //上次选中的菜单项
ps = pt;
pt = pt->m_pnext;
}
if (ps == pd) //两次菜单项相同,即鼠标选中后未有移动到选中菜单范围之外
return;
HDC hdc = GetDC(hwnd);
//SelectObject(hdc, CMenu::m_font);
//SetBkMode(hdc, TRANSPARENT);
if (ps) //上次选中的菜单项还原
{
if (phwnd->m_pnext)
{
if (pd)
{
if (pd->m_itemid == phwnd->m_pnext->m_itemid)
{
ms_selectitem = pd->m_itemid;
ReleaseDC(hwnd, hdc);
return;
}
/*else
{
CItem* p = phwnd->m_pcitem;
while (p)
{
if (p->m_itemid == phwnd->m_pnext->m_itemid)
break;
p = p->m_pnext;
}
if (p)
{
//FillRect(hdc, &p->m_rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
//DrawText(hdc, p->m_itemname, -1, &p->m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
//ms_selectitem = 0;
p->drawitemselect(hdc, 0);
}
}*/
}
DestroyWindow(phwnd->m_pnext->m_hwnd);
delete phwnd->m_pnext;
phwnd->m_pnext = NULL;
ms_selectitem = 0;
}
//FillRect(hdc, &ps->m_rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
//DrawText(hdc, ps->m_itemname, -1, &ps->m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
ps->drawitemselect(hdc, 0);
}
else //ps为0,上次未选中(ms_selectitem = 0) **或者选中的目标在下级菜单上,只考虑第二种
{
if (ms_selectitem && phwnd->m_pnext)
{
if (pd && (pd->m_itemid == phwnd->m_pnext->m_itemid))
{
ms_selectitem = pd->m_itemid;
if (phwnd->m_pnext->m_pnext)
{
CItem* p = serchid(phwnd->m_pnext->m_pcitem, phwnd->m_pnext->m_pnext->m_itemid);
if (p)
{
HDC h = GetDC(phwnd->m_pnext->m_hwnd);
p->drawitemselect(h, 0);
ReleaseDC(phwnd->m_pnext->m_hwnd, h);
}
// 窗口不同,下面方法也行
//InvalidateRect(phwnd->m_pnext->m_hwnd, 0, 1);
//UpdateWindow(phwnd->m_pnext->m_hwnd);
//
DestroyWindow(phwnd->m_pnext->m_pnext->m_hwnd);
delete phwnd->m_pnext->m_pnext;
phwnd->m_pnext->m_pnext = NULL;
}
ReleaseDC(hwnd, hdc);
return;
}
CItem* p = phwnd->m_pcitem;
while (p)
{
if (p->m_itemid == phwnd->m_pnext->m_itemid)
break;
p = p->m_pnext;
}
if (p)
{
//FillRect(hdc, &p->m_rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
//DrawText(hdc, p->m_itemname, -1, &p->m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
p->drawitemselect(hdc, 0);
}
DestroyWindow(phwnd->m_pnext->m_hwnd);
delete phwnd->m_pnext;
phwnd->m_pnext = NULL;
ms_selectitem = 0;
}
}
if (pd) //选中菜单项
{
if (pd->m_itemtype != 3)
{
//SelectObject(hdc, CMenu::m_selectbrush);
//SelectObject(hdc, CMenu::m_selectpen);
//RoundRect(hdc, pd->m_rect.left, pd->m_rect.top, pd->m_rect.right, pd->m_rect.bottom, 6 * gdpi
没有合适的资源?快使用搜索试试~ 我知道了~
windows应用菜单的实现
共14个文件
h:5个
cpp:4个
ico:4个
需积分: 9 0 下载量 22 浏览量
2022-03-11
20:17:57
上传
评论
收藏 29KB ZIP 举报
温馨提示
支持2k分辨率,完成菜单的框架,菜单项的具体细节可改写绘制函数
资源详情
资源评论
资源推荐
收起资源包目录
clover.zip (14个子文件)
cmenu.h 4KB
resource.h 669B
favicon48.ico 9KB
clover.cpp 11KB
caption.h 1KB
tools.cpp 251B
favicon.ico 1KB
tools.h 318B
cmenu.cpp 24KB
favicon32.ico 4KB
caption.cpp 8KB
clover.h 891B
small.ico 44KB
resource.rc 2KB
共 14 条
- 1
wangxing1689
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0