GDI相关基础知识

原文链接:http://blog.csdn.net/poem_qianmo/article/details/7333886

GDI(Graphics Device Interface) 图形设备接口,掌管了所有显像设备的图像显示及输出功能。Windows系统现在的图形操作环境都是他的功劳。

Ⅰ.坐标与DC

*创建一个窗口,显示的屏幕上便划分出三个区域,即屏幕区(Screen),窗口区(Window)与内部窗口区(Client);

* Device Context (设备内容)即DC,DC就是程序可以进行绘图的地方;

* 在处理WM_paint以外的地方要取得窗口DC,若使用GetDC()函数取得窗口DC后,必须使用ReleaseDC()函数将DC释放。方法如下:

HDC GetDC(HWND hwnd); //取得DC

Int ReleaseDC(HWND hWnd,HDC getedhdc);//释放DC,若运行成功,返回整数1,若失败返回0 getedhdc要释放的dc

 

II. 画笔与画刷

* 画笔与画刷都是GDI中所定义的图形对象,画笔是线条的样式。画刷是封闭图形内部填充的样式。可以自定义绘图所用的画笔与画刷样式,系统预设画笔样式为BLACK_PEN,画刷样式为NULL_BRUSH。

* 自定义画笔或画刷样式,用下面3个API函数:

HPEN CreatePen(int 样式, int 宽度, COLORREF 颜色 );   //建立画笔HBRUSH CreateHatchBrush(COLORREF 颜色);            //建立阴影画刷HBRUSH CreateSolidBrush(COLORREF 颜色);         //建立单色画刷

* GDI对象使用的基本过程是:建立——选用——删除,GDI对象有:画笔,画刷,位图,字体,区域及调色板等。

1、建立新画笔与新画刷之后,必须在所要进行绘图的DC中选择它们,才会产生预期的画笔与画刷效果,我们采用SelectObject()函数:

 HGDIOBJ SelectObject(HDC hdc,HGDIOBJ  GDI对象);     //选用GDI对象,返回先前使用的GDI对象

2、GDI对象一旦建立就会占用部分内存,一旦不使用就务必用DeleteObject将他们删除:

BOOL DeleteObject(HGDIOBJ DGI对象);             //删除GDI对象删除成功返回布尔值“ture”,若失败返回“FALSE”

 

III.GDI绘图函数

* virtual BOOL TextOut(

   int x,          //输出字符串的X坐标

   int y,           //输出字符串的Y坐标

   LPCTSTR lpszString,        //字符串指针

   int nCount      //字符串的长度

);

BOOL TextOut(

  _In_  HDC hdc,

  _In_  int nXStart,

  _In_  int nYStart,

  _In_  LPCTSTR lpString,

  _In_  int cchString

);
The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color.
 
* 多边形函数

Polygon()   绘制封闭多边形

PolyLine()  绘制多边线条

PolybneTo()  以当前画笔所在位置绘制多边线条

PolyPolygon()  绘制多个封闭多边形

PolyPolyline()  绘制多个多边线条

BOOL Polygon(

  _In_  HDC hdc,

  _In_  const POINT *lpPoints,

  _In_  int nCount

);

Parameters

hdc [in]

A handle to the device context.

lpPoints [in]

A pointer to an array of POINT structures that specify the vertices of the polygon, in logical coordinates.

nCount [in]

The number of vertices in the array. This value must be greater than or equal to 2.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

Remarks

The polygon is closed automatically by drawing a line from the last vertex to the first.

 

* 封闭图形函数

1.画矩形的GDI函数

BOOL Rectangle(

   int x1,

   int y1,

   int x2,

   int y2 

);

BOOL Rectangle(

   LPCRECT lpRect 

);

2.画椭圆的GDI函数

BOOL Ellipse(

   int x1,

   int y1,

   int x2,

   int y2 

);

BOOL Ellipse(

   LPCRECT lpRect 

);

3.画圆角矩形

BOOL RoundRect(

   int x1,

   int y1,

   int x2,

   int y2,

   int x3,

   int y3 

);

BOOL RoundRect(

   LPCRECT lpRect,

      POINT point 

);

4.画扇形

BOOL Pie(

   int x1,

   int y1,

   int x2,

   int y2,

   int x3,

   int y3,

   int x4,

   int y4 

);

BOOL Pie(

   LPCRECT lpRect,

      POINT ptStart,

      POINT ptEnd 

);

5.画弓形

BOOL Chord(

   int x1,

   int y1,

   int x2,

   int y2,

   int x3,

   int y3,

   int x4,

   int y4 

);

BOOL Chord(

   LPCRECT lpRect,

   POINT ptStart,

   POINT ptEnd 

);

你可能感兴趣的:(基础)