首页 > 技术文章 > CGridCtrl添加右键菜单

qq76211822 2015-11-02 11:12 原文

头文件下添加:

afx_msg void OnMergeCell();
    afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/);

添加映射:

ON_COMMAND(IDM_MergeCell, OnMergeCell)//IDM_MergeCell在resource.h添加一下就可以了
ON_WM_CONTEXTMENU()

 

.cpp(合并单元格)

void CAttendance_Guest_Day_View::OnMergeCell()
{
    CCellRange cellRange = m_HFlexGrid.GetSelectedCellRange();
    m_HFlexGrid.MergeCells(cellRange);
    m_HFlexGrid.Refresh();
}

void CAttendance_Guest_Day_View::OnContextMenu(CWnd* pWnd, CPoint point)
{
    ClientToScreen(&point);
    CRect rectCustom;
    GetDlgItem(IDC_CUSTOM1)->GetWindowRect(&rectCustom);
    if ((point.x > rectCustom.left && point.x < rectCustom.right) && (point.y > rectCustom.top && point.y < rectCustom.bottom) ){
        POINT pointOfCustom;
        CMenu menu;
        GetCursorPos(&pointOfCustom);
        menu.CreatePopupMenu();
        menu.AppendMenu(MF_STRING, IDM_MergeCell, _T("合并单元格"));
        menu.TrackPopupMenu(0, pointOfCustom.x, pointOfCustom.y, this);
    }
}

 

推荐阅读