用得GIRDCTRL
我在其他帖子上看到了一个程序,但是,其中许多地方不理解,哪位大侠给解释下
void CGridCtrl::OnRButtonDown(UINT nFlags, CPoint point) 

CCellID cell = GetCellFromPt(point); //问题1:这个函数是判断点的位置?不知道怎么写,哪个大哥给写一段?
if (!IsValid(cell)) return; 
SetFocus(); // Auto-destroy any InPlaceEdit's 
SetFocusCell(-1,-1); 
SetFocusCell(max(cell.row, m_nFixedRows), 
max(cell.col, m_nFixedCols)); CCellRange cellrange = GetCellRange(); 
CRect rect; 
GetCellRangeRect(cellrange,&rect); //问题2:这个是获取CELLRANGE的RECT,是不是将每个单元格的RECT相加?
if( !rect.PtInRect(point) ) 
return; 
CCellID nCellID = GetCellFromPt(point); 
// ClientToScreen(&point); //设置文档修改标志 
// e_SetDocModifiedFlag(); CMenu m_menu; 
HINSTANCE hOldIns = AfxGetResourceHandle(); 
// AfxSetResourceHandle(::GetModuleHandle("lm_standardlib.dll")); 
if (m_menu.LoadMenu(IDR_CLASS_RIGHT)) 

CSStockDlg * dlg = (CSStockDlg *)GetParent(); //问题3:这个dlg是个什么含义的?在gridctrl中没有这个东西的。
//得到对应的子菜单 
CMenu* pPopup = NULL; 
if( nCellID.row==0 ) //对于没有输入人名信息的,菜单不显示 
pPopup = m_menu.GetSubMenu(1); 
else 
pPopup = m_menu.GetSubMenu(0); 
//修改子菜单的状态 
if( nCellID.row == 0 ) 

if (dlg->m_badding || (!dlg->m_strname.IsEmpty() && !dlg->m_strnumber.IsEmpty() && 
!dlg->m_strpersonnumber.IsEmpty())) 
pPopup->EnableMenuItem(IDM_ROW_INSERT_UP,MF_GRAYED|MF_BYCOMMAND); 
else { 
pPopup->EnableMenuItem(IDM_ROW_INSERT_UP,MF_GRAYED); 
pPopup->EnableMenuItem(IDM_ROW_INSERT_DOWN,MF_GRAYED); 

pPopup->EnableMenuItem(IDM_DEL_CURENT_ROW,MF_GRAYED|MF_BYCOMMAND); 

//修改子菜单的状态 ASSERT(pPopup != NULL); 
ClientToScreen(&point); 
DWORD nSel = pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON| TPM_NONOTIFY | TPM_RETURNCMD, 
point.x, point.y, this); 
pPopup->DestroyMenu(); 
switch(nSel)//nSel is the id of the command selected . if no selection was made ,nSel = 0. 

case IDM_EDIT_ITEM://编辑ITEM 
OnLButtonDblClk(nFlags,point); 
break; 
case IDM_ROW_INSERT_UP://插入行--在前 
this->InsertRows(1); 
break; 
case IDM_ROW_INSERT_DOWN://插入行---在后 

if(nCellID.row==0)//如果是固定行,特殊处理 
m_idCurrentCell = nCellID; 
this->InsertRows(0); 
break; 

case IDM_DEL_CURENT_ROW://删除行 

int nRow = GetFocusCell().row; 
DeleteDbRow(nRow); 
DeleteRow(nRow); 
Invalidate(); 
break; 

case IDC_CLEAR_ITEM://清空单元格 

this->SetItemText(nCellID.row,nCellID.col,_T("")); 
this->InvalidateCellRect(nCellID); 
break; 

case IDM_CHANGE_ITEM_FONT://设置单元格字体 

ControlChangeFont(); 
break; 

case IDM_CHANGE_ALL_FONT://变换所有字体 

ControlChangeAllFont(); 
break; 

case IDM_BEST_VIEW://最佳显示 

OnDisplayMutiline(); 
break; 

case IDM_DELETEALL: 

OnDeleteAllRecords();//删除所有行 
Invalidate(); 
break; 

case IDM_DEL_PROTECT: 

OnDelProtect();//去掉打印标志,重新打印 
break; 

default : 
break; 
}如果上面的比较烦,哪位大哥好好心,给我写个在GRIDCTRL的单元格弹出右键菜单的程序?这个帖子重分求援,已经难我2天了。

解决方案 »

  1.   

    1 不是判断点位置 是得到单击那个单元格.2看定义 
    CCellRange CGridCtrl::GetCellRange() const
    {
        return CCellRange(0, 0, GetRowCount() - 1, GetColumnCount() - 1);
    }
    个人感觉 这个没啥用!3 完全去掉即可!要我就这么写了void CGridCtrl::OnRButtonDown(UINT nFlags, CPoint point) 

    CCellID cell = GetCellFromPt(point); //问题1:这个函数是判断点的位置?不知道怎么写,哪个大哥给写一段?
    if (!IsValid(cell)) return; 
    SetFocus(); // Auto-destroy any InPlaceEdit's //得到对应的子菜单 
    CMenu* pPopup = NULL; 
    if( nCellID.row==0 ) //对于没有输入人名信息的,菜单不显示 
    pPopup = m_menu.GetSubMenu(1); 
    else 
    pPopup = m_menu.GetSubMenu(0); 
    //修改子菜单的状态 
    if( nCellID.row == 0 ) 

    pPopup->EnableMenuItem(IDM_ROW_INSERT_UP,MF_GRAYED|MF_BYCOMMAND); 
    else { 
    pPopup->EnableMenuItem(IDM_ROW_INSERT_UP,MF_GRAYED); 
    pPopup->EnableMenuItem(IDM_ROW_INSERT_DOWN,MF_GRAYED); 

    pPopup->EnableMenuItem(IDM_DEL_CURENT_ROW,MF_GRAYED|MF_BYCOMMAND); 

    //修改子菜单的状态 ASSERT(pPopup != NULL); 
    ClientToScreen(&point); 
    DWORD nSel = pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON| TPM_NONOTIFY | TPM_RETURNCMD, 
    point.x, point.y, this); 
    pPopup->DestroyMenu(); 
    switch(nSel)//nSel is the id of the command selected . if no selection was made ,nSel = 0. 

    case IDM_EDIT_ITEM://编辑ITEM 
    OnLButtonDblClk(nFlags,point); 
    break; 
    case IDM_ROW_INSERT_UP://插入行--在前 
    this->InsertRows(1); 
    break; 
    case IDM_ROW_INSERT_DOWN://插入行---在后 

    if(nCellID.row==0)//如果是固定行,特殊处理 
    m_idCurrentCell = nCellID; 
    this->InsertRows(0); 
    break; 

    case IDM_DEL_CURENT_ROW://删除行 

    int nRow = GetFocusCell().row; 
    DeleteDbRow(nRow); 
    DeleteRow(nRow); 
    Invalidate(); 
    break; 

    case IDC_CLEAR_ITEM://清空单元格 

    this->SetItemText(nCellID.row,nCellID.col,_T("")); 
    this->InvalidateCellRect(nCellID); 
    break; 

    case IDM_CHANGE_ITEM_FONT://设置单元格字体 

    ControlChangeFont(); 
    break; 

    case IDM_CHANGE_ALL_FONT://变换所有字体 

    ControlChangeAllFont(); 
    break; 

    case IDM_BEST_VIEW://最佳显示 

    OnDisplayMutiline(); 
    break; 

    case IDM_DELETEALL: 

    OnDeleteAllRecords();//删除所有行 
    Invalidate(); 
    break; 

    case IDM_DEL_PROTECT: 

    OnDelProtect();//去掉打印标志,重新打印 
    break; 

    default : 
    break; 
    }