分享

VC绘图初步

 戴维图书馆 2013-01-28

VC绘图初步

(2008-12-20 15:23:03)
标签:

绘图

杂谈

分类: 工程分享

要做路径规划东东了,仿真需要陈vc,先扫扫盲吧!

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派生自CDC
  构造CClientDC对象的时候自动调用GetDC,而在析构的时候自动调用ReleaseDC.从而我们不需要
  显示的去调用这两个函数.只需要仅仅构造一个CClientDC对象.

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.

画线:

CClientDC dc(this);

dc.SetROP2( R2_NOT);

dc.MoveTo(p_Orgpt);

dc.LineTo(p_Endpt);

dc.MoveTo(p_Orgpt);

dc.LineTo(p_Endpt);

p_Endpt = point;//这里要记住哦!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多