把程序的定义与实现部分用头文件与源文件分开

首先,创建一个名为DrawTest的MFC应用程序。

    源文件地址:http://down.qiannao.com/space/file/luowei505050/-6211-7684-7a0b-5e8f/DrawTest.rar/.page

  1. 在框架窗口中无法响应鼠标单击事件。

    在CMainFrame类上右键Add Windows Message Hander… -> WM_LBUTTONDOWN -> Add And Edit.在CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)函数中添加:

    MessageBox("MainFrame Clicked");

    运行,点击鼠标没反应,是因为客户窗口总是在框架窗口之上,因此在这里框架窗口没法响应鼠标单击事件。因此响应事件一般都写在客户窗口中。

     

2.在客户窗口响应鼠标事件

    在CDrawTestView类上右键Add Windows Message Hander… -> WM_LBUTTONDOWN -> Add And Edit .在CDrawTestView::OnLButtonDown(UINT nFlags, CPoint point)添加:

MessageBox("MainFrame Clicked");

运行,点击鼠标,弹出消息框! OK! ^_^

 

3.因此创建画笔要在客户区的View类中创建。

1.在DrawTestView类中创建一个BOOL 类型的Private成员变量m_bDraw和一个CPoint类型的Private成员变量m_ptOrigin.并在构造函数当中:

    m_ptOrigin=0;

    m_bDraw=FALSE;

2. 在CDrawTestView::OnLButtonDown(UINT nFlags, CPoint point)给两个成员变量赋初值。

    m_ptOrigin=point;

    m_bDraw=TRUE;

    3.     在CDrawTestView类上右键Add Windows Message Hander… -> WM_LBUTTONUP-> Add And Edit.在CDrawTestView::OnLButtonUp(UINT nFlags, CPoint point)添加代码。

 

一、首先从画线开始再循序渐进

方法一:

    HDC hdc;    //定义一个HDC, Handle to a device context (DC).a Win32 Simple Data Types.

hdc=::GetDC(m_hWnd);//调用全局的GetDC函数,为指定的客户区域获取一个显示设备的句柄。

MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);// MoveToEx函数更新当前点的位置,返回先前点的位置。x,y分别为当前点的横纵坐标。

LineTo(hdc,point.x,point.y);// The LineTo function draws a line from the current position up to, but not including, the specified point

    ::ReleaseDC(m_hWnd,hdc);

    /* The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of device context. It frees only common and window device contexts. It has no effect on class or private device contexts. The return value specifies whether the device context is released. If the device context is released, the return value is 1. If the device context is not released, the return value is zero.*/

编译运行,OK ! ^_^

 

方法二:

    CDC* pDC=GetDC();

/* The CDC class encapsulates the functionality of a Windows device-context, which is a GDI-managed structure containing information about the operating modes and current selections of a device, such as a display or printer.*/

    pDC->MoveTo(m_ptOrigin);

    pDC->LineTo(point);

    ReleaseDC(pDC);

编译运行,ok!!

 

方法三:

    CClientDC dc(this); //只能画到客户区

/* The CClientDC class is derived from CDC and takes care of calling the Windows functions GetDC at construction time and ReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window.很明显我们用CClientDC创建画笔比较简洁*/

//CClientDC dc(GetParent());    //可以画到非客户区

/* The GetParent function retrieves a handle to the specified child window's parent window .If the function succeeds, the return value is a handle to the parent window. If the window has no parent window, the return value is NULL. To get extended error information, call GetLastError. */

dc.MoveTo(m_ptOrigin);

dc.LineTo(point);

 

方法四:

//CWindowDC dc(this); //只能画到客户区

/* The CWindowDC class is derived from CDC. It calls the Windows functions GetWindowDC at construction time and ReleaseDC at destruction time.*/

//CWindowDC dc(GetParent());//可以画到非客户区

CWindowDC dc(GetDesktopWindow());//可以画到整个屏幕区

/*The GetDesktopWindow function returns a handle to the desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which all icons and other windows are painted. This function has no parameters. The return value is a handle to the desktop window. */

dc.MoveTo(m_ptOrigin);

dc.LineTo(point);

 

二、创建画笔

方法一:

//创建特色的画笔

CPen Pen(PS_SOLID,5,RGB(0,255,0)); //线形PS_SOLID实线;PS_DASH阴影线、PS_DOT点线 (线宽小于等于1)

/*The CPen class encapsulates a Windows graphics device interface (GDI) pen. The CPen function is the Constructs function of the CPen class .More information see MSDN.

The RGB macro selects a red, green, blue (RGB) color based on the arguments supplied and the color capabilities of the output device. The return value is the resultant RGB color. */

CClientDC dc(this);

CPen *pOldPen=dc.SelectObject(&Pen); //SelectObject函数是将笔选到设备描述表当中,并返回被替换之前的那个笔的指针

dc.MoveTo(m_ptOrigin);

dc.LineTo(point);

dc.SelectObject(pOldPen);

 

//创建用颜色加载的画刷

CBrush brush(RGB(255,0,0));

/* The CBrush class encapsulates a Windows graphics device interface (GDI) brush. To use a CBrush object, construct a CBrush object and pass it to any CDC member function that requires a brush. */

CClientDC dc(this);

dc.FillRect(CRect(m_ptOrigin,point),&brush);

/* The FillRect function fills a rectangle by using the specified brush. This function includes the left and top borders, but excludes the right and bottom borders of the rectangle. The CRect class is similar to a Windows RECT structure. CRect also includes member functions to manipulate CRect objects and Windows RECT structures. The CRect class is similar to a Windows RECT structure. CRect also includes member functions to manipulate CRect objects and Windows RECT structures.*/

 

//创建用位图加载的画刷

CBitmap bitmap;

/* The CBitmap class encapsulates a Windows graphics device interface (GDI) bitmap and provides member functions to manipulate the bitmap. To use a CBitmap object, construct the object, attach a bitmap handle to it with one of the initialization member functions, and then call the object's member functions.*/

bitmap.LoadBitmap(IDB_BITMAP1);

/* Initializes the object by loading a named bitmap resource from the application's executable file and attaching the bitmap to the object. */

CBrush brush(&bitmap);

CClientDC dc(this);

dc.FillRect(CRect(m_ptOrigin,point),&brush);

 

//创建透明画刷

CClientDC dc(this);

//用GetStockObject返回一个空的画刷的HGDIOBJ句柄,并强制转换成画刷句柄

//再由FromHandle函数获取画刷句柄,并返回一个画刷对象的指针

CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

//这里FromHandle是一个静态的成员函数,所有可以这样CBrush::FromHandle引用。

CBrush *pOldBrush=dc.SelectObject(pBrush);

dc.Rectangle(CRect(m_ptOrigin,point));

dc.SelectObject(pOldBrush);

 

//创建可以画连续线段的画笔

//在CDrawTestView类中添加一个WM_MOUSEMOVE消息函数,添加以下代码

CClientDC dc(this);

    CPen pen(PS_SOLID,5,RGB(0,255,0));

    CPen *pOldPen=dc.SelectObject(&pen);

    if(TRUE==m_bDraw)

    {

        dc.SetROP2(R2_COPYPEN);

//设置绘画模式,有R2_COPYPEN 、R2_NOT、R2_NOTMERGEPEN、R2_MERGEPEN、R2_MERGEPENNOT等

        dc.MoveTo(m_ptOrigin);

        dc.MoveTo(m_ptOrigin);

        dc.LineTo(point);

        m_ptOrigin=point;//将原点值改变为刚换完这个线段的终点值

    }

    dc.SelectObject(pOldPen);

 

//画一个扇形

//在CDrawTestView类中添加一个CPoint类型Private成员变量

// 在CDrawTestView::OnLButtonDown(UINT nFlags, CPoint point)函数中,将

    m_ptOrigin=m_ptOld=point;//当鼠标点时,将原点、旧的点、新的点赋值一致

    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗哥分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值