龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

VC++50个必做例子之鼠标划线

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
在视图类开头定义 class CMouseView : public CView { protected: // create from serialization only CMouseView(); DECLARE_DYNCREATE(CMouseView) // my add CString m_ClassName; int m_Dragging; //鼠标状态 HCURSOR m_HCross; //鼠标形状

在视图类开头定义

class CMouseView : public CView
{
protected: // create from serialization only
    CMouseView();
    DECLARE_DYNCREATE(CMouseView)
// my add
    CString m_ClassName;
    int m_Dragging;  //鼠标状态
    HCURSOR m_HCross; //鼠标形状
    CPoint m_PointOld; //鼠标的初始地点
    CPoint m_pointOrigin; //鼠标原点
在构造函数中对

CMouseView::CMouseView()
{
    // TODO: add construction code here
    m_Dragging=0; //鼠标的状态为0,没有按下,1为按下
    m_HCross = AfxGetApp()->LoadStandardCursor(IDC_CROSS); //获得鼠标的形状赋值

m_HCross 存放程序的光标句柄,AfxGetApp()获得应用程序类对象的指针,这个指针用于调用应用程序类对象的成员函数LoadStandardCursor()传送光标的句柄。光标句柄参数参考LoadStandardCursor()函数

添加一个鼠标的lbuttondown 消息,然后添加代码,(晕,书是用vc++7.0,看来很多东西以后要自己想了)

(cheat---遇到困难了吗,祝贺你,你获得了进步的机会~)

 添加

void CMouseView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_pointOrigin = point; //用鼠标的坐标的赋值
    m_PointOld = point; /用鼠标的坐标的赋值
    SetCapture();  //捕获鼠标
    m_Dragging = 1; //设置鼠标状态

    RECT Rect; //定义窗口范围
    GetClientRect(&Rect); //获得客户区的范围
    ClientToScreen(&Rect); //转换为屏幕坐标
    ::ClipCursor(&Rect); // 限定鼠标范围
 

添加鼠标移动,消息

 

void CMouseView::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    ::SetCursor(m_HCross); //显示鼠标形状
   

    if(m_Dragging)
    {
        CClientDC ClientDC(this);  //获得视窗设备表述表
        ClientDC.SetROP2(R2_NOT);//生成逆转当前屏幕颜色来绘图方式,
        ClientDC.MoveTo(m_pointOrigin);//画
        ClientDC.LineTo(m_PointOld);//画起点--终点线
        ClientDC.MoveTo(m_pointOrigin);
        ClientDC.LineTo(m_PointOld);

        m_pointOrigin = point;

    }
   
    CView::OnMouseMove(nFlags, point);
}
添加鼠标up消息

 

void CMouseView::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    if(m_Dragging)
    {
       m_Dragging = 0;
       ::ReleaseCapture();
       ::ClipCursor(NULL);

        CClientDC ClientDC(this);
        ClientDC.SetROP2(R2_NOT);
           ClientDC.MoveTo(m_pointOrigin);
        ClientDC.LineTo(m_PointOld);
        ClientDC.SetROP2(R2_COPYPEN);
        ClientDC.MoveTo(m_pointOrigin);
        ClientDC.LineTo(m_PointOld);
    }
    CView::OnLButtonUp(nFlags, point);
}

精彩图集

赞助商链接