1.菜单
1.01 菜单栏
// 注册目录测试函数
ON_COMMAND(ID_INSERT_MENU, OnMenuTest)
void CMainFrame::OnMenuTest()
{
//获得主菜单
CMenu* pMenu = GetMenu();
//获得子菜单
CMenu* pSubMenu = pMenu->GetSubMenu(4);
if (pSubMenu->GetMenuItemCount() == 4)
{
//在菜单末端添加菜单项
pSubMenu->AppendMenu(MF_STRING, ID_TEST_MENU, _T("New"));
//在菜单指定位置添加菜单项
//pSubMenu->InsertMenu(4, MF_BYPOSITION, ID_TEST_MENU, _T("add"));
//移除菜单项
//pSubMenu->RemoveMenu(4, MF_BYPOSITION);
//删除菜单项
//pSubMenu->DeleteMenu(4, MF_BYPOSITION);
//重画菜单
DrawMenuBar();
}
}
void CMainFrame::OnUpdateTestMenu1(CCmdUI* pCmdUI)
{
//启用或禁用菜单1
pCmdUI->Enable(m_bEnable1);
}
void CMainFrame::OnUpdateTestMenu(CCmdUI* pCmdUI)
{
//设置菜单项检查状态
pCmdUI->SetCheck(m_nCheck);
}
1.02 右键菜单
//获得系统菜单
CMenu* pMenu = GetSystemMenu(FALSE);
if (pMenu)
{
//删除系统菜单项
pMenu->RemoveMenu(SC_MOVE, MF_STRING);
//添加菜单项
pMenu->InsertMenu(0, MF_BYPOSITION, ID_TEST_MENU,_T("测试菜单项"));
//禁用关闭按钮
pMenu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
//重画菜单
DrawMenuBar();
}
1.03 获得光标坐标
void CDemoView::OnMouseMove(UINT nFlags, CPoint point)
{
//保存光标坐标
m_Point = point;
//刷新客户区
Invalidate();
CView::OnMouseMove(nFlags, point);
}
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);
}
1.03 限制光标异动范围
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);
}
1.04 预定义光标
OnCreate是在窗体创建时调用的。
OnInitialUpdate视图窗口完全建立后第一个被框架调用的函数。
// 定义光标对象
HCURSOR m_hCursor;
/* 初始化光标 */
void CDemoView::OnInitialUpdate()
{
CView::OnInitialUpdate();
//加载光标
m_hCursor = AfxGetApp()->LoadStandardCursor(IDC_WAIT);
}
/* 添加SetCursor事件 */
BOOL CDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
//设置光标
::SetCursor(m_hCursor);
return TRUE;
//return CView::OnSetCursor(pWnd, nHitTest, message);
}
/* 切换光标 */
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonUp(nFlags, point);
}
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonDown(nFlags, point);
}
1.05 使用自定义光标
添加资源文件 Cursor1.cur
添加资源文件 Cursor2.cur
/* 切换光标 */
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonDown(nFlags, point);
}
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//加载光标
m_hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR2);
//设置光标
::SetCursor(m_hCursor);
CView::OnLButtonDown(nFlags, point);
}
1.06 设置光标等待
void CDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
//启动等待光标
BeginWaitCursor();
//休眠
Sleep(5000);
//结束等待光标
EndWaitCursor();
CView::OnLButtonDown(nFlags, point);
}