代码改变世界

Visual C++ 光标

2011-04-05 13:58  Clingingboy  阅读(3199)  评论(1编辑  收藏  举报

 

2.7 如何获得光标的坐标

在鼠标相关事件中获取坐标

void CDemoView::OnMouseMove(UINT nFlags, CPoint point) 
{
    //保存光标坐标
    m_Point    = point;

    //刷新客户区
    Invalidate();

    CView::OnMouseMove(nFlags, point);
}

 

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);
}

 

2.9 如何使用预定义光标

  1. LoadStandardCursor同LoadCursor
  2. SetCursor
  3. GetCursor
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方法载入

image

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(2000);

    //结束等待光标
    EndWaitCursor();

    CView::OnLButtonDown(nFlags, point);
}

参考:

image