VC 菜单栏的自绘(一)

今天我们来学习下 如何模拟系统自带的菜单效果。

1.创建一个对话框工程

2.新建一个面板类继CPanel承自CWnd,重载PreCreateWindow函数和Create函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
BOOL CPanel::Create( LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
    return CWnd::Create(_T("PANEAL"),lpszWindowName,  dwStyle,  rect,  pParentWnd,  nID);
}
 
BOOL CPanel::PreCreateWindow(CREATESTRUCT& cs)
{
    HINSTANCE hInstance = AfxGetInstanceHandle();
    ASSERT(hInstance);
 
    WNDCLASSEX wcex;
    wcex.cbSize=sizeof(WNDCLASSEX);
    BOOL bRet = GetClassInfoEx(hInstance,cs.lpszClass,&wcex);
    if (bRet)
    {
        return TRUE;
    }
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wcex.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
    wcex.hIcon = (HICON)LoadIcon(hInstance,MAKEINTRESOURCE(IDR_MAINFRAME));
    wcex.hIconSm = (HICON)LoadIcon(hInstance,MAKEINTRESOURCE(IDR_MAINFRAME));
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = AfxWndProc;
    wcex.lpszClassName = cs.lpszClass;
    wcex.lpszMenuName = NULL;
    wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    bRet = ::RegisterClassEx(&wcex);
    return bRet;
 
    //return CWnd::PreCreateWindow(cs);
}

 

 3.新建一个CMenuBar类 继承自CPanel

4.在对话框里创建CMenuBar

if (m_MenuBar.m_hWnd == NULL)
 {
  CRect rcClient,rcMenuBar;
  GetClientRect(rcClient);
  rcMenuBar.left = (rcClient.Width()-150)/2;
  rcMenuBar.top = (rcClient.Height()-22)/2;
  rcMenuBar.bottom = rcMenuBar.top+22;
  rcMenuBar.right = rcMenuBar.left+150;
  m_MenuBar.Create(_T(""),WS_CHILD|WS_VISIBLE,rcMenuBar,this,10001);
   }

创建后运行如下:

 VC 菜单栏的自绘(一)

5.建一个CMenuItem类,用来绘制子菜单

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class CMenuItem
{
public:
    CMenuItem();
    ~CMenuItem();
public:
    void DrawMenu(CDC* pDC);
public:
    enum STATE{Normal=1,Hover,Pressed};
public:
    CRect m_rcMenu;
    STATE m_state;
    CString m_strText;
    HMENU m_hPopUpMenu;
};

 

VC 菜单栏的自绘(一)
void CMenuItem::DrawMenu(CDC* pDC)
{
    int OldbkMode = pDC->SetBkMode(TRANSPARENT);
    switch(m_state)
    {
    case Normal:
        {

        }
        break;

    case Pressed:
        {
            CPen pen(PS_SOLID,1,0x123456);
            CPen*pOldPen = pDC->SelectObject(&pen);
            pDC->Rectangle(m_rcMenu);
            pDC->SelectObject(pOldPen);
        }
        break;

    case Hover:
        {
            CPen pen(PS_SOLID,1,0x888888);
            CPen*pOldPen = pDC->SelectObject(&pen);
            pDC->Rectangle(m_rcMenu);
            pDC->SelectObject(pOldPen);
        }
        break;
    default:
        ASSERT(FALSE);
        break;
    }
    pDC->DrawTextEx(m_strText,m_rcMenu,DT_SINGLELINE|DT_CENTER|DT_VCENTER,NULL);
    pDC->SetBkMode(OldbkMode);
}
VC 菜单栏的自绘(一)

6.CMenuBar里面添加2个函数 1个用来载入资源 另一个判断处鼠标位置状态绘制边框

VC 菜单栏的自绘(一)
void CMenuBar::LoadMenuFromResource(UINT nIDResource)
{
    m_Menu.LoadMenu(nIDResource);
    int nCount = m_Menu.GetMenuItemCount();
    for (int i = 0;i<nCount;i++)
    {
        CString strText;
        m_Menu.GetMenuString(i,strText,MF_BYPOSITION);
        CMenuItem* pMenuItem = new CMenuItem;
        pMenuItem->m_strText = strText;
        pMenuItem->m_hPopUpMenu = m_Menu.GetSubMenu(i)->m_hMenu;
        pMenuItem->m_rcMenu = CRect(i*50,0,i*50+50,22);
        pMenuItem->m_state = CMenuItem::Normal;
        m_vecMenuItem.push_back(pMenuItem);
    }
    Invalidate();
}

int CMenuBar::HitTest(CPoint pt)
{
    int nItem = HTERROR;
    int nCount = GetMenuItemCount();
    for (int i = 0;i<nCount;i++)
    {
        CMenuItem* pMenuItem = m_vecMenuItem.at(i);
        if (pMenuItem)
        {
            BOOL bInRect = pMenuItem->m_rcMenu.PtInRect(pt);
            if (bInRect)
            {
                if (pMenuItem->m_state != CMenuItem::Hover)
                {
                    pMenuItem->m_state = CMenuItem::Hover;
                    InvalidateRect(pMenuItem->m_rcMenu);
                }
                nItem = i;
            }
            else
            {
                if (pMenuItem->m_state != CMenuItem::Normal)
                {
                    pMenuItem->m_state = CMenuItem::Normal;
                    InvalidateRect(pMenuItem->m_rcMenu);
                }    
            }
        }        
    }

    return nItem;
}
VC 菜单栏的自绘(一)

载入资源的时候 把所有信息保存起来 放入一个vector中
所以 还要添加2个变量

public:
    CMenu m_Menu;
    vector<CMenuItem*> m_vecMenuItem;

再在menubar创建的地方添加载入资源函数,别忘了在资源中 添加菜单资源哦,我这里添加了3个菜单项。

7.响应MouseMove消息

VC 菜单栏的自绘(一)
void CMenuBar::OnMouseMove(UINT nFlags, CPoint point)
{
    int nCount = HitTest(point);
    CPanel::OnMouseMove(nFlags, point);
}
VC 菜单栏的自绘(一)

8.响应wm_paint 消息

VC 菜单栏的自绘(一)
void CMenuBar::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    
    int nCount = GetMenuItemCount();//子项个数
    CFont font;
    font.CreateFont(14,0,0,0,FW_MEDIUM,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,_T("Arial"));
    dc.SelectObject(&font);
    for (int i = 0; i<nCount;i++)
    {
        CMenuItem * pMenuItem = m_vecMenuItem.at(i);
        pMenuItem->DrawMenu((CDC*)&dc);
    }
}
VC 菜单栏的自绘(一)

 

好了,编译运行 我们看到边框随鼠标移动也跟着移动了

9.但是鼠标离开的时候 还有边框显示在上面。所以我们要捕获一下鼠标离开的状态,在mousemove里面如下处理:

VC 菜单栏的自绘(一)
void CMenuBar::OnMouseMove(UINT nFlags, CPoint point)
{
    TRACKMOUSEEVENT tme;
    tme.cbSize = sizeof(TRACKMOUSEEVENT);
    tme.dwFlags = TME_LEAVE;
    tme.dwHoverTime = 1;
    tme.hwndTrack= this->m_hWnd;
    TrackMouseEvent(&tme);

    int nCount = HitTest(point);
    CPanel::OnMouseMove(nFlags, point);
}
VC 菜单栏的自绘(一)

然后手动添加鼠标离开的消息响应函数

    ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
VC 菜单栏的自绘(一)
//鼠标离开后全部设为normal状态
LRESULT CMenuBar::OnMouseLeave(WPARAM,LPARAM)
{
    int nCount = GetMenuItemCount();
    for (int i = 0;i<nCount;i++)
    {
        CMenuItem* pMenuItem = m_vecMenuItem.at(i);
        if (pMenuItem)
        {
            if (pMenuItem->m_state != CMenuItem::Normal)
            {
                pMenuItem->m_state = CMenuItem::Normal;
                InvalidateRect(pMenuItem->m_rcMenu);
            }    
        }        
    }
    return 0;
}
VC 菜单栏的自绘(一)

运行下看看 感觉还不错吧。

暂时写到这吧,全部写完后我会把示例代码传上来的。

第一次写博客,写的不好,或大家不明白的地方,欢迎大家批评指正。

好了我要去写年终总结了。。。

VC 菜单栏的自绘(一)

上一篇:Python 字典(Dictionary)操作详解


下一篇:2019.02.17 spoj Query on a tree VII(链分治)