Win32 菜单光标
2012-02-14 12:47 Clingingboy 阅读(1158) 评论(0) 编辑 收藏 举报
漏了写总是心里不舒服
2.1 如何动态添加和删除菜单项
四个方法:AppendMenu,InsertMenu,RemoveMenu,DeleteMenu
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnAppendMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 4)
{
//在菜单末端添加菜单项
pSubMenu->AppendMenu(MF_STRING, ID_TEST_MENU, _T("新菜单项"));
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnInsertMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 4)
{
//在菜单指定位置添加菜单项
pSubMenu->InsertMenu(1, MF_BYPOSITION, ID_TEST_MENU, _T("新菜单项"));
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnRemoveMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 5)
{
//删除菜单项
pSubMenu->RemoveMenu(4, MF_BYPOSITION);
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnDeleteMenu()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 5)
{
//删除菜单项
pSubMenu->DeleteMenu(4, MF_BYPOSITION);
//重画菜单
DrawMenuBar();
}
}
2.2 如何在系统菜单中添加菜单项
GetSystemMenu方法
//获得系统菜单
CMenu* pMenu = GetSystemMenu(FALSE);
//删除系统菜单项
pMenu->RemoveMenu(SC_MOVE, MF_STRING);
//添加菜单项
pMenu->InsertMenu(0, MF_BYPOSITION, ID_TEST_MENU, _T("测试菜单项"));
2.3 如何禁用关闭按钮
EnableMenuItem方法
//获得系统菜单
CMenu* pMenu = GetSystemMenu(FALSE);
//禁用关闭按钮
pMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
2.4 如何启用和禁用菜单项
使用CCmdUI对象
void CMainFrame::OnTestMenu1()
{
m_bEnable1 = FALSE;
m_bEnable2 = TRUE;
}
void CMainFrame::OnUpdateTestMenu1(CCmdUI* pCmdUI)
{
//启用或禁用菜单1
pCmdUI->Enable(m_bEnable1);
}
void CMainFrame::OnTestMenu2()
{
m_bEnable1 = TRUE;
m_bEnable2 = FALSE;
}
void CMainFrame::OnUpdateTestMenu2(CCmdUI* pCmdUI)
{
//启用或禁用菜单2
pCmdUI->Enable(m_bEnable2);
}
2.5 如何设置菜单项的检查状态
CheckMenuItem方法
CheckMenuRadioItem则是单选
void CMainFrame::OnUpdateTestMenu(CCmdUI* pCmdUI)
{
CMenu menu;
//menu.CheckMenuItem();
//设置菜单项检查状态
pCmdUI->SetCheck(m_nCheck);
}
2.6 如何实现快捷菜单(右键菜单)
TrackPopupMenu方法
void CDemoView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
//加载菜单
if (!menu.LoadMenu(IDR_MENU))
{
return;
}
//获得子菜单
CMenu* pPopupMenu = menu.GetSubMenu(0);
//弹出菜单
pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y, pWnd);
}
2.7 如何获得光标的位置
m_Point为mousemove时的坐标
void CDemoView::OnDraw(CDC* pDC)
{
//获得客户区坐标
CRect rect;
GetClientRect(rect);
//绘制十字光标
pDC->MoveTo(0, m_Point.y);
pDC->LineTo(rect.Width(), m_Point.y);
pDC->MoveTo(m_Point.x, 0);
pDC->LineTo(m_Point.x, rect.Height());
//输出光标坐标
CString strText =_T("");
strText.Format(_T("%d, %d"), m_Point.x, m_Point.y);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextAlign(TA_RIGHT | TA_BOTTOM);
pDC->TextOut(m_Point.x, m_Point.y, strText);
}
2.8 如何限制光标的移动范围
ClipCursor方法
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//获得客户区坐标
CRect rect;
GetClientRect(rect);
rect.left = rect.left + rect.Width() / 4;
rect.right = rect.right - rect.Width() / 4;
rect.top = rect.top + rect.Height() / 4;
rect.bottom = rect.bottom - rect.Height() / 4;
//映射屏幕坐标
ClientToScreen(rect);
//限制光标移动范围
ClipCursor(&rect);
CView::OnLButtonDown(nFlags, point);
}
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
//光标自由移动
ClipCursor(NULL);
CView::OnLButtonUp(nFlags, point);
}
2.9 如何使用预定义光标
LoadStandardCursor方法和SetCursor方法
BOOL CDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
//设置光标
::SetCursor(m_hCursor);
return TRUE;
// return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonDown(nFlags, point);
}
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonUp(nFlags, point);
}
2.10 如何使用自定义光标
LoadCursor方法载入
BOOL CDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
//设置光标
::SetCursor(m_hCursor);
return TRUE;
// return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR2);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonDown(nFlags, point);
}
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonUp(nFlags, point);
}
2.11 如何使用等待光标
BeginWaitCursor和EndWaitCursor
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//启动等待光标
BeginWaitCursor();
//休眠
Sleep(5000);
//结束等待光标
EndWaitCursor();
CView::OnLButtonDown(nFlags, point);
}