win32day07-图形绘制/GDI绘图对象-画笔/画刷
Posted 吴英强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win32day07-图形绘制/GDI绘图对象-画笔/画刷相关的知识,希望对你有一定的参考价值。
图形绘制
1.1 图形绘制的方式
获取到绘图句柄-设备描述表(DC),使用相应
的绘图的API,在设备上绘制图形.
1.2 颜色
R\\G\\B三色,每种颜色8位,共24位颜色.
32位颜色:颜色数量24位颜色(8-8-8),多出来的8位
表示灰度.
16位:颜色数量2的16次方.(5-6-5)
Win32下,颜色的定义COLORREF(DWORD), RGB宏定义颜色
COLORREF nColor = RGB( 0, 0, 0 );//黑色
COLORREF nColor = RGB( 255,255,255 );//白色
COLORREF nColor = RGB( 255,0, 0 );//红色
从一个颜色中获取RGB三色:
int nBlue = GetBValue( nColor );
int nRed = GetRValue( nColor );
int nGreen= GetGValue( nColor );
1.3 点的绘制和获取绘制: COLORREF SetPixel(
HDC hdc, //DC句柄
int X, //x坐标
int Y, //y坐标
COLORREF crColor ); // 点的颜色
获取: COLORREF GetPixel(
HDC hdc, //DC句柄
int XPos, //x坐标
int nYPos ); //y坐标
返回指定坐标位置的点的颜色
1.4 直线的绘制
MoveToEx 移动当前点到指定位置
LineTo 从当前点绘制直线到指定位置
1.5 弧的绘制
Arc和AngleArc提供不同的绘制弧的方式:
1.5.1 BOOL Arc( HDC hdc,
int nLeftRect, // 外切矩形的坐标左
int nTopRect,//外切矩形的坐标上
int nRightRect,//外切矩形的坐标右
int nBottomRect,//外切矩形的坐标下
int nXStartArc,//起始切割半径的X坐标
int nYStartArc,//起始切割半径的Y坐标
int nXEndArc, //终止切割半径的X坐标
int nYEndArc ); //终止切割半径的X坐标
可以使用SetArcDirection函数,设置Arc函数切割方向:顺时针和逆时针.
1.5.2 BOOL AngleArc(
HDC hdc, // handle to device context
int X, //圆心的X坐标
int Y, //圆心的Y坐标
DWORD dwRadius,//圆的半径
FLOAT eStartAngle,//开始角度
FLOAT eSweepAngle );//夹角
1.6 折线
1.6.1 BOOL Polyline(
HDC hdc, //DC句柄
CONST POINT *lppt,//Polyline顶点的坐标数组
int cPoints ); //顶点数组的长度
1.6.2 PolylineTo 与Polyline类似, 只是在
绘制Polyline前,从当前点( 没有设置的话,是0,0点)使用LineTo绘制直线到Polyline的第一个顶点.
1.6.3 绘制多组折线 PolyPolyline
BOOL PolyPolyline( HDC hdc,
CONST POINT *lppt,//所有点的数组
CONST DWORD *lpdwPolyPoints,//每组点的数量
DWORD cCount );//分组的数量
例如:
DWORD nGroup[] = 4, 3 ;
PolyPolyline( hDC, ptPolyLine,nGroup, 2 );
1.7 Bizer曲线
BOOL PolyBezier(HDC hdc,
CONST POINT *lppt,//点数组,最少4个点
DWORD cPoints );//点的数量
4个点: 1和4是端点,2.3点是控制点
7个点: 1.4.7是端点,其余是控制点
1.8 多样式的线条
BOOL PolyDraw( HDC hdc,
CONST POINT *lppt,//各个点的数组
CONST BYTE *lpbTypes, //从某点到下一点的绘制方式 pointer to line and curve identifiers
int cCount); //点的数量
lpbTypes - PT_MOVETO 移动到该点
PT_LINETO 绘直线
PT_BIZERTO Biezer曲线
1.9 矩形的绘制
1.9.1 普通矩形 Rectangle
1.9.2 带圆角的矩形
BOOL RoundRect( HDC hdc,
int nLeftRect, //左上X坐标
int nTopRect, //左上Y坐标
int nRightRect,//右下X坐标
int nBottomRect, //右下Y坐标
int nWidth, //生成圆角的椭圆的宽度
int nHeight );//生成圆角的椭圆的高度
1.10 椭圆和圆
BOOL Ellipse( HDC hdc,
int nLeftRect, //外切矩形左上X坐标
int nTopRect,//外切矩形左上Y坐标
int nRightRect, //外切矩形右下X坐标
int nBottomRect); //外切矩形右下Y坐标
1.11 饼Pie
BOOL Pie( HDC hdc,
int nLeftRect, //外切矩形左上X坐标
int nTopRect, //外切矩形左上Y坐标
int nRightRect, //外切矩形右下X坐标
int nBottomRect, //外切矩形右下Y坐标
int nXRadial1,//切割起始半径X坐标
int nYRadial1,//切割起始半径Y坐标
int nXRadial2,//切割终止半径X坐标
int nYRadial2 );//切割终止半径Y坐标
1.12 弦
BOOL Chord( HDC hdc,
int nLeftRect, //外切矩形左上X坐标
int nTopRect, //外切矩形左上Y坐标
int nRightRect, //外切矩形右下X坐标
int nBottomRect, //外切矩形右下Y坐标
int nXRadial1,//切割起始半径X坐标
int nYRadial1,//切割起始半径Y坐标
int nXRadial2,//切割终止半径X坐标
int nYRadial2 );//切割终止半径Y坐标
1.13 多边形
BOOL Polygon( HDC hdc,
CONST POINT *lpPoints, //多边形的顶点
int nCount ); //顶点的数量
PolyPolygon 可以绘制多组多边形
GDI绘图对象 - 画笔
2.1 画笔的作用可以控制线条的颜色、样式、宽度。
2.2 画笔的使用
2.2.1 创建画笔 CreatePen
HPEN CreatePen(
int fnPenStyle, //画笔的样式
int nWidth, //画笔的宽度
COLORREF crColor);//画笔的颜色
2.2.2 置成当前DC可以使用的画笔
HGDIOBJ SelectObject(
HDC hdc, // 当前DC的句柄
HGDIOBJ hgdiobj );//要使用的GDI对象句柄
返回当前DC原来使用的同类型的GDI对象句柄。
2.2.3 绘制图形
2.2.4 从当前DC中取出画笔,即将旧画笔
放入当前DC中
SelectObject( hDC, hOldPen );
2.2.5 销毁画笔
DeleteObject( hPen );
画笔使用时,由于绘图资源有限,一定要
注意释放。
GDI绘图对象 -画刷
3.1 画刷的作用
填充封闭图形,包括样式、颜色等。
3.2 画刷的使用
3.2.1 创建画刷
CreateSolidBrush 创建实心画刷
CreateHatchBrush 创建阴影线的画刷
3.2.2 置成当前DC可以使用的画刷
SelectObject 和画笔使用方式类似
3.2.3 绘制图形
3.2.4 取出画刷
SelectObject 和画笔使用方式类似
3.2.5 销毁画刷
DeleteObject 和画笔使用方式类似
// WinDraw.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdio.h"
#include "resource.h"
HINSTANCE g_hInst = NULL;
int g_nDrawType = 0;
COLORREF g_nPenColor = RGB( 0, 0, 0 );
int g_nPenStyle = PS_SOLID;
int g_nPenWdith = 1;
COLORREF g_nBrushColor = RGB( 255, 255, 255 );
int g_nBrushStyle = 0xFFFFFFFF;
void DrawPixel( HDC hDC )
COLORREF nColor = RGB( 0, 0, 0 );
SetPixel( hDC, 100, 100, nColor );
void GetPixelColor( HDC hDC )
//获取点的颜色
COLORREF nColor =
GetPixel( hDC, 100, 100 );
//获取颜色的三色值
int nRed = GetRValue( nColor );
int nGreen = GetGValue( nColor );
int nBlue = GetBValue( nColor );
CHAR szText[260] = 0 ;
sprintf( szText,
"COLOR=%08X, RED=%d GREEN=%d BLUE=%d",
nColor, nRed, nGreen, nBlue );
MessageBox( NULL, szText, "WinDraw", MB_OK );
void DrawLine( HDC hDC )
MoveToEx( hDC, 0, 0, NULL );
LineTo( hDC, 500, 500 );
MoveToEx( hDC, 500, 0, NULL );
LineTo( hDC, 0, 500 );
void DrawArc( HDC hDC )
//逆时针的方式
SetArcDirection( hDC,
AD_COUNTERCLOCKWISE );
//通过外切矩形和切割线
Arc( hDC, 400, 200, 500, 300,
500, 200, 400, 200);
//顺时针的方式
SetArcDirection( hDC,
AD_CLOCKWISE );
Arc( hDC, 500, 200, 600, 300,
600, 200, 500, 200);
MoveToEx( hDC, 200, 200, NULL );
//指定圆心和角度
AngleArc( hDC, 200, 200, 100,
60, 240 );
LineTo( hDC, 200, 200 );
void DrawPolyLine( HDC hDC )
POINT ptPolyLine[7] = 0 ;
ptPolyLine[0].x = 100;
ptPolyLine[0].y = 100;
ptPolyLine[1].x = 200;
ptPolyLine[1].y = 100;
ptPolyLine[2].x = 200;
ptPolyLine[2].y = 200;
ptPolyLine[3].x = 300;
ptPolyLine[3].y = 200;
ptPolyLine[4].x = 300;
ptPolyLine[4].y = 300;
ptPolyLine[5].x = 400;
ptPolyLine[5].y = 300;
ptPolyLine[6].x = 400;
ptPolyLine[6].y = 400;
//Polyline( hDC, ptPolyLine, 7 );
//PolylineTo( hDC, ptPolyLine, 3 );
DWORD nGroup[] = 4, 3 ;
PolyPolyline( hDC, ptPolyLine,nGroup, 2 );
void DrawBizer( HDC hDC )
POINT ptBizer[7] = 0 ;
ptBizer[0].x = 100; //端点
ptBizer[0].y = 100; //
ptBizer[1].x = 100; //控制点
ptBizer[1].y = 50; //
ptBizer[2].x = 300; //控制点
ptBizer[2].y = 150; //
ptBizer[3].x = 300; //端点
ptBizer[3].y = 100; //
ptBizer[4].x = 300; //控制点
ptBizer[4].y = 400;
ptBizer[5].x = 400; //控制点
ptBizer[5].y = 200;
ptBizer[6].x = 500; //端点
ptBizer[6].y = 300;
PolyBezier( hDC, ptBizer, 7 );
MoveToEx( hDC, ptBizer[0].x,
ptBizer[0].y, NULL );
LineTo( hDC, ptBizer[1].x, ptBizer[1].y );
MoveToEx( hDC, ptBizer[3].x,
ptBizer[3].y, NULL );
LineTo( hDC, ptBizer[2].x, ptBizer[2].y );
void DrawPolyDraw( HDC hDC )
POINT ptDraw[4] = 0 ;
ptDraw[0].x = 100;
ptDraw[0].y = 100;
ptDraw[1].x = 200;
ptDraw[1].y = 100;
ptDraw[2].x = 200;
ptDraw[2].y = 200;
ptDraw[3].x = 300;
ptDraw[3].y = 200;
BYTE ptType[4] = 0;
ptType[0] = PT_MOVETO;
ptType[1] = PT_LINETO;
ptType[2] = PT_MOVETO;
ptType[3] = PT_LINETO;
PolyDraw( hDC, ptDraw, ptType, 4 );
void DrawRect( HDC hDC )
//矩形
Rectangle( hDC, 100, 100, 200, 200 );
//带圆角矩形
RoundRect( hDC, 300, 100, 400, 200, 50, 50 );
void DrawEllipse( HDC hDC )
//圆
Ellipse( hDC, 100, 100, 200, 200 );
//椭圆
Ellipse( hDC, 300, 100, 500, 200 );
void DrawPie( HDC hDC )
Pie( hDC, 100, 100, 500, 400,
500, 100, 100, 100 );
void DrawChord( HDC hDC )
Chord( hDC, 100, 100, 500, 400,
500, 100, 100, 100 );
void DrawPloygon( HDC hDC )
POINT ptPloygon[4] = 0 ;
ptPloygon[0].x = 100;
ptPloygon[0].y = 100;
ptPloygon[1].x = 200;
ptPloygon[1].y = 100;
ptPloygon[3].x = 600;
ptPloygon[3].y = 300;
ptPloygon[2].x = 500;
ptPloygon[2].y = 300;
Polygon( hDC, ptPloygon, 4 );
void OnPaint( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
PAINTSTRUCT ps = 0 ;
HDC hDC = BeginPaint( hWnd, &ps );
//创建画笔
HPEN hPen = CreatePen( g_nPenStyle,
g_nPenWdith, g_nPenColor );
//设置画笔到当前DC
HPEN hOldPen = (HPEN)
SelectObject( hDC, hPen );
//创建画刷
HBRUSH hBrush = NULL;
if( g_nBrushStyle == 0xFFFFFFFF )
hBrush = CreateSolidBrush( g_nBrushColor );
else
hBrush = CreateHatchBrush(
g_nBrushStyle, g_nBrushColor );
//设置画刷到当前DC
HBRUSH hOldBrush = (HBRUSH)
SelectObject( hDC, hBrush );
//绘制图形
switch( g_nDrawType )
case ID_SETPIXEL:
DrawPixel( hDC );
break;
case ID_GETPIXEL:
GetPixelColor( hDC );
break;
case ID_LINE:
DrawLine( hDC );
break;
case ID_ARC:
DrawArc( hDC );
break;
case ID_POLYLINE:
DrawPolyLine( hDC );
break;
case ID_DRAWBIZER:
DrawBizer( hDC );
break;
case ID_POLYDRAW:
DrawPolyDraw( hDC );
break;
case ID_RECT:
DrawRect( hDC );
break;
case ID_ELLIPSE:
DrawEllipse( hDC );
break;
case ID_PIE:
DrawPie( hDC );
break;
case ID_CHORD:
DrawChord( hDC );
break;
case ID_POLYGON:
DrawPloygon( hDC );
break;
//取出画刷
SelectObject( hDC, hOldBrush );
//删除画刷
DeleteObject( hBrush );
//取出画笔
SelectObject( hDC, hOldPen );
//销毁画笔
DeleteObject( hPen );
EndPaint( hWnd, &ps );
void OnCommand( HWND hWnd, UINT nMsg,
WPARAM wParam, LPARAM lParam )
int nCmdID = LOWORD( wParam );
switch( nCmdID )
case ID_SETPIXEL:
case ID_GETPIXEL:
case ID_LINE:
case ID_ARC:
case ID_POLYLINE:
case ID_DRAWBIZER:
case ID_POLYDRAW:
case ID_RECT:
case ID_ELLIPSE:
case ID_PIE:
case ID_CHORD:
case ID_POLYGON:
//保存绘制图形类型
g_nDrawType = nCmdID;
//刷新窗口
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_REDPEN:
//保存画笔颜色
g_nPenColor = RGB( 255, 0, 0 );
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_PSDASH:
g_nPenStyle = PS_DASH;
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_PEN5:
g_nPenWdith = 5;
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_PEN1:
g_nPenWdith = 1;
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_REDBRUSH:
g_nBrushColor = RGB( 255, 0, 0 );
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_SOLIDBRUSH:
g_nBrushStyle = 0xFFFFFFFF;
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_HSDIAGCROSS:
g_nBrushStyle = HS_DIAGCROSS;
InvalidateRect( hWnd, NULL, TRUE );
break;
case ID_EXIT:
PostQuitMessage( 0 );
break;
LRESULT CALLBACK WndProc( HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam )
switch( nMsg )
case WM_PAINT:
OnPaint( hWnd, nMsg, wParam, lParam );
break;
case WM_COMMAND:
OnCommand( hWnd, nMsg, wParam, lParam );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
return DefWindowProc( hWnd, nMsg,
wParam, lParam );
BOOL RegisterWnd( LPSTR pszClassName )
WNDCLASSEX wce = 0 ;
wce.cbSize = sizeof( wce );
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE+1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW|CS_VREDRAW;
ATOM nAtom = RegisterClassEx( &wce );
if( 0 == nAtom )
return FALSE;
return TRUE;
HWND CreateWnd( LPSTR pszClassName )
HMENU hMenu = LoadMenu( g_hInst,
MAKEINTRESOURCE(IDR_MAIN) );
HWND hWnd = CreateWindowEx( 0,
pszClassName, "MyWnd",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMenu, g_hInst,
NULL );
return hWnd;
void DisplayWnd( HWND hWnd )
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
void Message( )
MSG msg = 0 ;
while( GetMessage( &msg, NULL, 0, 0 ) )
TranslateMessage( &msg );
DispatchMessage( &msg );
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
g_hInst = hInstance;
RegisterWnd( "MYWND" );
HWND hWnd = CreateWnd( "MYWND" );
DisplayWnd( hWnd );
Message( );
return 0;
以上是关于win32day07-图形绘制/GDI绘图对象-画笔/画刷的主要内容,如果未能解决你的问题,请参考以下文章