有关绘图

来源:互联网 发布:淘宝客户关系管理在哪 编辑:程序博客网 时间:2024/04/29 23:36

1.使用SDK获取DC句柄
HDC hdc;
hdc=::GetDc(m_hWnd);    //获取DC句柄
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);    //释放DC

2.利用CDC类指针和CWin类成员函数获取DC
CDC *pDC=GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);

3.利用CClientDC对象 (CClientDC类从CDC类派生来的)
CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
----------
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.

4.利用CWindowDC对象 (CWindowDC类从CDC类派生来的)
CWindowDC dc(this);//
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
-----------
The CWindowDC class is derived from CDC. It calls the Windows functionsGetWindowDC at construction time andReleaseDC at destruction time. This means that a CWindowDC object accesses the entire screen area of a CWnd (both client and nonclient areas).

5.GetParent()得到父窗口指针; GetDesktopWindow()得到屏幕窗口指针

6.利用画笔改变线条颜色和类型:
CPen pen(PS_DOT,1,RGB(0,255,0));    //构造画笔对象
CClientDC dc(this);CPen *pOldPen=dc.SelectObject(&pen);    //将画笔选入DC
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(pOldPen);    //恢复先前的画笔

7.使用画刷 (通常利用画刷去填充矩形区域):
使用单色画刷
CBrush brush(RGB(255,0,0));    //构造画刷对象
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);    //用指定的画刷去填充矩形区域

使用位图画刷
CBitmap bitmap;//构造位图对象 (使用前需要初试化)
bitmap.LoadBitmap(IDB_BITMAP1);    //初试化位图对象
CBrush brush(&bitmap);    //构造位图画刷
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);    //用指定的位图画刷去填充矩形区域

使用透明画刷
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));    //获取透明画刷对象指针
CClientDC dc(this);
CBrush *pOldBrush=dc.SelectObject(pBrush); //将透明画刷选入DC
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldBrush);//释放透明画刷
----------
The GetStockObject function retrieves a handle to one of the predefined stock pens, brushes, fonts, or palettes.
HGDIOBJ GetStockObject(
                                      int fnObject   // type of stock object
                                 );

Returns a pointer to a CBrush object when given a handle to a Windows HBRUSH object.
static CBrush* PASCAL FromHandle( HBRUSH hBrush );//FromHandle是一个静态方法.故可用CBrush::FromHandle()形式调用。
----------
1)静态方法不属于某一个具体对象.而属于类本身.在类加载的时候就已经为类静态方法分配了代码去.故可用CBrush::FromHandle()形式调用。
2)静态方法中.不能引用非静态的数据成员和方法。
3)静态数据成员需要在类外单独做初始化.形式如: 变量类型 类名::变量名=初始值;

8.CDC::SetROP2方法
int SetROP2( int nDrawMode );
Sets the current drawing mode.

原创粉丝点击