***VC++学习--《VC++深入详解》孙鑫 第六章 菜单 学习心得***
今天学习了 《VC++深入详解》孙鑫 第六章 菜单,练习了书上的实例程序,记录在此,顺便写点心得,o(∩_∩)o…
exploer 2008年1月15日9:10:56
注:转载请注明出处。
CChileView
/*
*****************************
*使用VC的组建和控件库实现快捷菜单
*如下所示:(默认)

*详细步骤见《VC++深入详解》P188
*****************************
*/
void CChildView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Pop-up Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_CHILD_VIEW));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
}
/*
**********************************
*手工实现右键单击弹出快捷菜单,使用TrackPopupMen()函数实现
快捷菜单:

Line à OnLine
Circleà OnCircle
Rectà OnRect
*******************************************
*/
void CChildView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu menu;
menu.LoadMenu(IDR_TRACKPOPUP);
CMenu *pPopup=menu.GetSubMenu(0);
ClientToScreen(&point);//注意要转换客户坐标系为屏幕坐标系
pPopup->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON ,point.x,point.y,this);
CWnd ::OnRButtonDown(nFlags, point);
}
void CChildView::OnLine()
{
// TODO: Add your command handler code here
MessageBox("Line!");
}
void CChildView::OnRect()
{
// TODO: Add your command handler code here
MessageBox("Rect!");
}
void CChildView::OnCircle()
{
// TODO: Add your command handler code here
MessageBox("Circle!");
}
CMainFrame
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
…………………………………………
…………………………………………………………………………..
GetMenu()->GetSubMenu(1)->CheckMenuItem(2,MF_BYPOSITION|MF_CHECKED);//使用索引号定位
GetMenu()->GetSubMenu(1)->CheckMenuItem(ID_EDIT_CUT,MF_BYCOMMAND|MF_UNCHECKED);//采用ID定位
GetMenu()->GetSubMenu(1)->SetDefaultItem(2,TRUE);//设置默认菜单项
//使用位图菜单项,其中IDB_UNCHECKED,IDB_CHECKED 分别为菜单//项选中和未选中时的位图ID
m_bitmapUnChecked.LoadBitmap(IDB_UNCHECKED);
m_bitmapChecked.LoadBitmap(IDB_CHECKED);
GetMenu()->GetSubMenu(2)->SetMenuItemBitmaps(1,MF_BYPOSITION,&m_bitmapChecked,&m_bitmapUnChecked);//使用位图菜单
//SetMenu(NULL);//移除菜单
/**/
CMenu menu;
menu.LoadMenu(IDR_NEWMENU);//载入新菜单
/*
载入的新菜单为:


*/
//menu.LoadMenu(IDR_MAINFRAME);
SetMenu(&menu);
menu.Detach();//使句柄和菜单对象分离,避免产生错误
/*
CString str;
str.Format("x=%d,y=%d",GetSystemMetrics(SM_CXMENUCHECK),GetSystemMetrics(SM_CYMENUCHECK));
// Format一个很有用的格式化IO函数,类似于C函数库中的printf,sprintf
MessageBox(str);
*/
//GetMenu()->GetSubMenu(2)->EnableMenuItem(1,MF_DISABLED|MF_BYPOSITION|MF_GRAYED);//禁用菜单项
return 0;
}
备注:鼠标单击等事件使用的都是客户区坐标
点击此处查看原文 >>
系统分类:
软件开发 | 用户分类:
| 来源:
原创