Windows在基于图形设备接口的视频显示器和打印机上显示图像。GDI
5.1.1 GDI原理
GDI是为了兼容于设备无关的图形。
图形输出设备分为两大类,光栅设备和矢量设备。GDI是图形设备硬件的一种高级接口。
GDI总的来说是一个静态系统,对动画支持有限。如果需要为游戏编写复杂的动画,还是需要使用DirectX 或者OpenGL
5.1.2 GDI函数使用
GDI函数的分类
*获取和释放设备环境的函数 BeginPaint/ EndPaint GetDC/ Release DC
*获取设备环境信息的函数 GetTextMetrics
*绘图函数
*设置和获取设备环境属性的函数 Set* Get*
*使用GDI“对象”的函数
5.1.3 GDI的基本图形
*线条和曲线,
直线,矩形,椭圆,椭圆弧线,贝塞尔曲线。GDI使用当前选入设备环境的画笔绘制线条。
*可被填充的封闭区域
GDI使用画刷对象填充封闭的区域。可以使纯色,某种填充模式也可以是不停重复的位图。
* 位图
二维的位数组对应显示设备上的一个像素。GDI支持设备相关位图和设备无关位图DIB
* 文本
和印刷相关,GDI中文本处理最庞大。
5.1.4 其他
其他GDI函数
* 映射模式和转换 坐标转换,图形旋转等。
* 图元文件(metafile)是以二进制形式存储的GDI命令的集合。主要通过剪贴板转换矢量图形绘制的表现形式
* 区域(region)
* 路径 GDI内部的直线和曲线的集合
* 剪裁 当绘图被限制在特定的区域就称为剪裁,可以是矩形或者非矩形。 通常被指定为一个区域或者一个路径
* 调色板 仅支持256色时才能使用自定义调色板。windows保留20种,你可以改变其他236种色彩
* 打印
5.2 设备环境
要在图形设备上绘制图形,必须获得设备环境的句柄。(DC)。windows把这个句柄交给你的程序,也就交给你使用权。接着将GDI函数中将这个句柄作为一个参数,
告诉windows在哪个设备上进行绘图。
5.2.1 获取设备环境的句柄
最常用方法是在WM_PAINT消息
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, *ps);
在处理非WM_PAINT消息时由windows程序获取
hdc = GetDC(hwnd);
ReleaseDC(hwnd, hdc); 可以绘制整个客户区,但是并不使得客户区从无效变为有效。
还可以获得整个窗口的设备环境
hdc = GetWindowDC(hwnd);
ReleaseDC(hwnd, hdc); 如果使用这个还应该捕获WM_NCPAINT 非客户区绘制信息
以上获取的DC可以在视频显示器上与一个特定的窗口相关联的设备环境。 还有一个更通用的用于获取设备环境句柄的函数是
CreateDC
hdc = CreateDC(pszDriver, pszDevice, pszOutput, pData);
DeleteDC(hdc);
例如可以获得当前整个屏幕的设备环境句柄
hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
打印机的设备环境句柄就是用CreateDC函数来获取的。
有时候仅需要获取设备环境的信息,而不需要绘制任何东西。可以用CreateIC获取 信息上下文句柄。
hdc = CreateIC(TEXT("DISPLAY"), NULL, NULL, NULL);
处理位图时,有时候可能会用到一个“内存设备环境”
hdcMem = CreateCompatibleDC(hdc);
DeleteDC(hdcMem);
图元文件是二进制编码的GDI函数调用集合,可以获取一个图元文件的设备环境
hdcMeta = CreateMetaFile(pszFilename);
hmf = CloseMetaFile(hdcMeta);
使用hdcMeta所做的任何GDI调用都不会显示出来,他会成为图元文件的一部分。
5.2.2 获取设备环境的信息
设备环境通常指的是物理显示设备,如视频显示器或者打印机。
iValue = GetDeviceCaps(hdc, iIndex);
5.2.3 DEVCAPS1 程序
DEVCAPS1 程序显示了在使用视频显示设备环境时,可以从GetDeviceCaps函数得到的部分信息。
#include <windows.h>
#define NUMLINES ((int) (sizeof devcaps / sizeof(devcaps[0])))
struct
{
int iIndex;
TCHAR* szLabel;
TCHAR* szDesc;
}
devcaps[] =
{
HORZSIZE, TEXT("HORZSIZE"), TEXT("Width in millimeters:"),
VERTSIZE, TEXT("VERTSIZE"), TEXT("Height in millimeters:"),
HORZRES, TEXT("HORZRES"), TEXT("Width in pixels:"),
VERTRES, TEXT("VERTRES"), TEXT("Height in raster lines:"),
BITSPIXEL, TEXT("BITSPIXEL"), TEXT("Color bits per pixel:"),
PLANES, TEXT("PLANES"), TEXT("Number of color planes:"),
NUMBRUSHES, TEXT("NUMBRUSHES"), TEXT("Number of device brushes:"),
NUMPENS, TEXT("NUMPENS"), TEXT("Number of device pens:"),
NUMMARKERS, TEXT("NUMMARKERS"), TEXT("Number of device markers:"),
NUMFONTS, TEXT("NUMFONTS"), TEXT("Number of device fonts:"),
NUMCOLORS, TEXT("NUMCOLORS"), TEXT("Number of device colors:"),
PDEVICESIZE, TEXT("PDEVICESIZE"), TEXT("Size of device structure:"),
ASPECTX, TEXT("ASPECTX"), TEXT("Relative width of pixel:"),
ASPECTY, TEXT("ASPECTY"), TEXT("Relative height of pixel:"),
ASPECTXY, TEXT("ASPECTXY"), TEXT("Relative diagonal of pixel:"),
LOGPIXELSX, TEXT("LOGPIXELSX"), TEXT("Horizontal dots per inch:"),
LOGPIXELSY, TEXT("LOGPIXELSY"), TEXT("Vertical dots per inch:"),
SIZEPALETTE, TEXT("SIZEPALETTE"), TEXT("Number of palette entries:"),
NUMRESERVED, TEXT("NUMRESERVED"), TEXT("Reserved palette entries:"),
COLORRES, TEXT("COLORRES"), TEXT("Actual color resolution:")
};
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("DevCaps1");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Device Capabilities"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar;
TCHAR szBuffer[10];
HDC hdc;
int i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message) //get the message
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for (i = 0; i < NUMLINES; ++i)
{
TextOut(hdc, 0, cyChar * i,
devcaps[i].szLabel,
lstrlen(devcaps[i].szLabel));
TextOut(hdc, 14 * cxCaps, cyChar*i,
devcaps[i].szDesc,
lstrlen(devcaps[i].szDesc));
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
TextOut(hdc, 14 * cxCaps + 35 * cxChar, cyChar * i,
szBuffer,
wsprintf(szBuffer, TEXT("%5d"),
GetDeviceCaps(hdc, devcaps[i].iIndex)));
SetTextAlign(hdc, TA_LEFT | TA_TOP);
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
显示结果
5.2.4 设备的尺寸
当你需要显示1英寸的正方形,GetDevieCaps函数可以帮助获取有关输出设备的物理尺寸信息。
本书约定,分辨率指 没度量单位中含的像素数。像素规模 1024x768 之类
可以使用GetSystemMetrics函数时使用SM_CXSCREEN 和SM_CYSCREEN参数来获取显示器的像素规模。
或者GetDeviceCaps 使用HORZRES 和VERTRES 参数来获得同样的值。
传统排版中,字体字符的大小是由“点值”来表示的,1点大约是1/72英寸。在计算机排版中,1点通常假定正好是1/72英寸。
理论上来说,一个字体的点值是从合格字体中,最高字符的顶部到下部字符底端的距离,这里不包括重音符号。例如,10磅的字体从上到下的
距离是10/72英寸。 用TEXTMETRIC结构的术语来说,字号等于字段tmHeight 减去 tmInternalLeading.
windows系统字体都被假定是一个10点的字并使用12点的行间距。
在Windows程序中,通过调用GetDeviceCaps函数,可以获取用户在控制面板程序中选择以每英寸的橡树点数为单位的假定的分辨率。
为了获取这些值,需要LOGPIXELSX 和LOGPIXELSY 逻辑像素。以每英寸的像素计算的非实际分辨率。
5.2.5 色彩ABC
真彩色 full color 24bit RGB
高彩色 16bit 5bitR 6bitG 5bitB
256色 索引色
16色
色彩平面的数目可以由下函数获得
iPlanes = GetDeviceCaps(hdc, PLANES);
每个像素的颜色位数可以由下面的调用获得
iBitsPixel = GetDeviceCaps(hdc, BITSPIXEL); // in this computer it is 32bit
视频适配器支持的色彩数可由一下公式计算:
iColors = 1 << (iPlanes * iBitsPixel);
通过NUMCOLORS参数获取的值可能一样也可能不一样
iColors = GetDeviceCaps(hdc, NUMCOLORS);
在GDI中使用一个32bit长整数表示颜色
在Wingdi.h中有定义
#define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
在16bit或者256色的视频适配器上,windows采用抖动来仿真。使设备能够显示更多色彩
crPureColor = GetNearestColor(hdc, crColor);
5.2.6 设备环境属性
当一个程序获取一个设备环境句柄时,windows设置所有属性为默认值。
表5-1 |
设备环境属性 | 默认值 | 修改该值的函数 | 取得该值的函数 |
Mapping Mode | MM_TEXT | SetMapMode | GetMapMode |
Window Origin | (0, 0) | SetWindowOrgEx OffsetWindowOrgEx | GetWindowOrgEx |
Viewport Origin | (0, 0) | SetViewportOrgEx OffsetViewportOrgEx | GetViewportOrgEx |
Window Extents | (1, 1) | SetWindowExtEx SetMapMode ScaleWindowExtEx | GetWindowExtEx |
Viewport Extents | (1, 1) | SetViewportExtEx SetMapMode ScaleViewportExtEx | GetViewportExtEx |
Pen | BLACK_PEN | SelectObject | SelectObject |
Brush | WHITE_BRUSH | SelectObject | SelectObject |
Font | SYSTEM_FONT | SelectObject | SelectObject |
Bitmap | None | SelectObject | SelectObject |
Current Position | (0, 0) | MoveToEx LineTo PolylineTo PolyBezierTo | GetCurrentPositionEx |
Background Mode | OPAQUE | SetBkMode | GetBkMode |
Background Color | White | SetBkColor | GetBkColor |
Text Color | Black | SetTextColor | GetTextColor |
Drawing Mode | R2_COPYPEN | SetROP2 | GetROP2 |
Stretching Mode | BLACKONWHITE | SetStretchBltMode | GetStretchBltMode |
Polygon Fill Mode | ALTERNATE | SetPolyFillMode | GetPolyFillMode |
Intercharacter Spacing | 0 | SetTextCharacterExtra | GetTextCharacterExtra |
Brush Origin | (0, 0) | SetBrushOrgEx | GetBrushOrgEx |
Clipping Region | None | SelectObject SelectClipRgn IntersectClipRgn OffsetClipRgn ExcludeClipRect SelectClipPath | GetClipBox |
5.2.7 保存设备环境
每次获取hDC 都会初始化,设定好以后释放所有设定都会丢失。
在注册窗口类的时候将窗口样式设定为CS_OWNDC
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
现在每个基于此窗口类创建的窗口都有他的私有设备环境,当窗口被销毁时,这个设备环境依然存在。使用CS_OWNDC只需要初始化设备环境一次,例如
case WM_CREATE:
hdc = GetDC(hwnd);
//initialize device context attributes
ReleaseDC(hwnd, hdc);
在改变这些属性以前,他们一直有效。
CS_OWNDC 只影响通过BeginPaint和GetDC获取的设备环境。其他函数如GetWindowDC获取的设备环境不受影响。即使使用了CS_OWNDC hdc也应该在退出窗口过程前被释放
可以临时保存设备环境的某些属性,然后修改以后再复原继续使用。
int idSaved = SaveDC(hdc);
然后再还原
RestoreDC(hdc, idSaved);
可以在ReleaseDC之前多次调用SaveDC
5.3 点和线的绘制
理论上,所有图形设备驱动程序所需要的是SetPixel和GetPixel,其余一切可以在GDI中使用更高层的例程来处理。问题是性能
5.3.1 像素设定
SetPixel(hdc, x, y, crColor); //crColor COLORREF
crColor = GetPixel(hdc, x, y); // 获取指定像素的颜色
5.3.2 直线
LineTo 直线
Polyline PolylineTo 多条首位相连的直线构成的折线
PolyPolyline 画多条折线
Arc 画椭圆弧线
PolyBezier PolyBezierTo 画贝赛尔曲线
WinNT增加的
ArcTo 和AngleArc 画椭圆弧线
PolyDraw 画多条贝塞尔曲线和首位相连的直线构成的折线
画线以后会填充所画图形的封闭区域
Rectangle 矩形
Ellipse 画椭圆
RoundRect 画圆角矩形
Pie 画椭圆的一部分,看起来像扇形
Chord 画出由弦分割出的部分椭圆,形状呈弓形
设备环境有5个属性会影响这些函数所绘的线条外观:当前画笔位置(LineTo, PolylineTo, PolyBezierTo, ArcTo),画笔,背景模式,背景颜色和绘图模式。
为了画一条直线
MoveToEX(hdc, xBeg, yBeg, NULL); //指定起点,最后一个POINT指针指向当前点
LineTo(hdc, xEnd, yEnd); //从当前点画线到终点
GetCurrentPositionEx(hdc, &pt); //获取当前的位置
从左上开始在客户区画一个网格
GetClientRect(hwnd, &rect);
for (int x = 0; x < rect.right; x += 100)
{
MoveToEx(hdc, x, 0, NULL);
LineTo(hdc, x, rect.bottom);
}
for (int y = 0; y < rect.bottom; y += 100)
{
MoveToEx(hdc, 0, y, NULL);
LineTo(hdc, rect.right, y);
}
画连续的直线
POINT apt[5] = {100, 100, 200, 100, 200, 200, 100, 200, 100, 100};
MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
for(i = 1; i < 5; ++i)
LineTo(hdc, apt[i].x, apt[i].y);
或者使用
Polyline(hdc, apt, 5);
或者
MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
PolylineTo(hdc, apt+1, 4);
一个绘制正弦曲线的例子
#include <windows.h>
#include <math.h>
#define NUM 1000
#define TWOPI (2 * 3.14159)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SineWave");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Sine Wave Using Polyline"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
HDC hdc;
int i;
PAINTSTRUCT ps;
POINT apt[NUM];
switch (message) //get the message
{
case WM_CREATE:
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
MoveToEx(hdc, 0, cyClient / 2, NULL);
LineTo(hdc, cxClient, cyClient / 2);
for (i = 0; i < NUM; ++i)
{
apt[i].x = i * cxClient / NUM;
apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM)));
}
Polyline(hdc, apt, NUM);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
计算好所有点的坐标一次性用Polyline函数绘制,因为是在设备驱动层次上实现的,所以比调用1000次LineTo函数要快。
5.3.3 边框绘制函数
画好以后会填充颜色
Rectangle(hdc, xLeft, yTop, xRight, yBottom);
边界偏差(off-by-one)问题。windows在边框内绘图。
考虑一下函数调用
Rectangle(hdc, 1, 1, 5, 4);
严格来说Rectangle并不是一个画线函数,他会填充这个矩形。因为使用白色填充区域所以看不出填充效果。
椭圆形
Ellipse(hdc, xLeft, yTop, xRight, yBottom);
画圆角矩形
RoundRect(hdc, xLeft, yTop, xRight, yBottom, xCornerEllipse, yCornerEllipse);
xCornerEllipse = (xRight - xLeft) / 4;
yCornerEllipse = (yBottom - yTop) / 4;
Arc (hdc, xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd, yEnd);
Chord(hdc, xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd, yEnd);
Pie(hdc, xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd, yEnd);
一个LINEDEMO绘制各种线条的例子 矩形,椭圆,圆角矩形和两条直线
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("LineDemo");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Line Demonstration"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient;
HDC hdc;
PAINTSTRUCT ps;
switch (message) //get the message
{
case WM_CREATE:
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
Rectangle(hdc, cxClient / 8, cyClient / 8, 7 * cxClient / 8, 7 * cyClient / 8);
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, cxClient, cyClient);
MoveToEx(hdc, 0, cyClient, NULL);
LineTo(hdc, cxClient, 0);
Ellipse(hdc, cxClient / 8, cyClient / 8, 7 * cxClient / 8, 7 * cyClient / 8);
RoundRect(hdc, cxClient / 4, cyClient / 4, 3 * cxClient / 4, 3 * cyClient / 4, cxClient / 4, cyClient / 4);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
执行结果
5.3.4 贝塞尔样条曲线
一条二位贝塞尔样条曲线使用四个点定义:两个端点和两个控点,两个端点表示起点和终点。控点就好像“磁铁”一样把曲线从两个端点间的直线处吸弯。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Bezier");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Bezier Splines"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void DrawBezier (HDC hdc, POINT apt[])
{
PolyBezier (hdc, apt, 4);
MoveToEx (hdc, apt[0].x, apt[0].y, NULL);
LineTo (hdc, apt[1].x, apt[1].y);
MoveToEx (hdc, apt[2].x, apt[2].y, NULL);
LineTo (hdc, apt[3].x, apt[3].y);
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static POINT apt[4];
static int cxClient, cyClient;
HDC hdc;
PAINTSTRUCT ps;
switch (message) //get the message
{
case WM_CREATE:
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
apt[0].x = cxClient / 4;
apt[0].y = cyClient / 2;
apt[1].x = cxClient / 2;
apt[1].y = cyClient / 4;
apt[2].x = cxClient / 2;
apt[2].y = 3 * cyClient / 4;
apt[3].x = 3 * cxClient / 4;
apt[3].y = cyClient / 2;
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
{
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(WHITE_PEN));
DrawBezier(hdc, apt);
if (wParam & MK_LBUTTON)
{
apt[1].x = LOWORD(lParam);
apt[1].y = HIWORD(lParam);
}
if (wParam & MK_RBUTTON)
{
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
SelectObject(hdc, GetStockObject(BLACK_PEN));
DrawBezier(hdc, apt);
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_PAINT:
InvalidateRect(hwnd, NULL, TRUE);
hdc = BeginPaint(hwnd, &ps);
DrawBezier(hdc, apt);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
运行结果
在windows98中不需要知道这些公式直接使用
PolyBezier(hdc, apt, iCount);
或者
PolyBezierTo(hdc, apt, iCount);
5.3.5 使用现有画笔
使用任何一个画线函数,windows都会使用当前选入设备环境的“画笔”来绘制直线。
画笔决定了线条的颜色,宽度和样式(实现,点或者虚线)
BLACK_PEN
WHITE_PEN
NULL_PEN
HPEN hPen;
hPen = GetStockObject(WHITE_PEN); //获得白色画笔
SelectObject(hdc, hPen); //将画笔选入设备环境
或者
SelectObject(hdc, GetStockObject(WHITE_PEN));
SelectObject返回先前一个选入设备环境的画笔句柄。
5.3.6 创建,选择和删除画笔
一般过程
CreatePen 或者CreatePenIndirect
使用以后需要调用DeleteObject函数来删除你创建的画笔
逻辑画笔是一种GDI对象,还有画刷,位图,区域,字体和调色板。
* 最终应当删除你所创建的所有GDI对象
* 当GDI被选入一个有效的设备环境时,不要删除他
* 不要删除备用对象
hPen = CreatePen(iPenStyle, iWidth, crColor);
iPenStyle 画笔样式
crColor 一个COLORREF值
LOGPEN logpen;
hPen = CreatePenIndirect(&logpen);
可以在WM_CREATE: 时候创建三种画笔
hPen1 = CreatePen(PS_SOLID, 1, 0);
hPen2 = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
hPen3 = CreatePen(PS_POT, 0, 0);
在WM_PAINT消息选入一支画笔
SelectObject(hdc, hPen2);
...
SelectObject(hdc, hPen1);
...
在WM_DESTROY消息销毁画笔
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hPen3);
---
不要删除当前选入设备环境的句柄
SelectObject(hdc, CreatePen(PS_DASH, 0, RGB(255, 0, 0)));
...
Delete(SelectObject(hdc, GetStockObject(BLACK_PEN)));
GetObject(hPen, sizeof(LOGPEN), (LPVOID)&logpen);
获得当前选入设备环境的画笔句柄
hPen = GetCurrentObject(hdc, OBJ_PEN);
DeleteObject(SelectObject,(hdc, hPen));
如果有一个画笔句柄,LOGPEN可以通过调用GetObject函数获得
GetObject(hPen, sizeof(LOGPEN), (LPVOID) &logpen);
hPen = GetCurrentObject(hdc, OBJ_PEN);
5.3.7 填充空隙
改变windows填充空隙的背景颜色
SetBkColor(hdc, crColor);
获得当前的背景颜色 GetBkColor
将背景模式改为TRANSPARENT 来阻止Windows填充空隙
SetBkMode(hdc, TRANSPARENT);
GetBkMode // TRANSPARENT, OPAQUE
5.3.8 绘图模式
SetROP2(hdc, iDrawMode);
iDrawMode = GetROP2(hdc);
5.4 绘制填充区域
Rectangle
Ellipse
RoundRect
Chord
Pie
Polygon
PolyPolygon
填充背景颜色用画刷
HBRUSH hbrush
hbrush = GetStockObject(GRAY_BRUSH); //获取备用画刷
SelectObject(hdc, hBrush); //将画刷选入设备环境
SelectObject(hdc, GetStockObject(NULL_PEN)); // 将NULL_PEN画笔选入设备环境
SelectObject(hdc, GetStockObject(NULL_BRUSH)); //只想绘制边框线 不想填充图形内部
5.4.1 Polygon函数和多边形填充模式
SetPolyFillMode(hdc, iMode); //设置多边形的填充模式 ALTERNATE, WINDING
一个ALTERNATE 和WINDING模式下绘制多边形填充模式的例子
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("AltWind");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Alternate and Winding Fill Modes"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static POINT aptFigure[10] = {10, 70, 50, 70, 50, 10, 90, 10, 90, 50,
30, 50, 30, 90, 70, 90, 70, 30, 10, 30};
static int cxClient, cyClient;
int i;
HDC hdc;
PAINTSTRUCT ps;
POINT apt[10];
switch (message) //get the message
{
case WM_CREATE:
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(GRAY_BRUSH));
for (i = 0; i < 10; ++i)
{
apt[i].x = cxClient * aptFigure[i].x / 200;
apt[i].y = cyClient * aptFigure[i].y / 100;
}
SetPolyFillMode(hdc, ALTERNATE);
Polygon(hdc, apt, 10);
for (i = 0; i < 10; ++i)
{
apt[i].x += cxClient / 2;
}
SetPolyFillMode(hdc, WINDING);
Polygon(hdc, apt, 10);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
结果
5.4.2 用画刷填充内部
hBrush = CreateSolidBrush(crColor);
阴影标记画刷
hBrush = CreateHatchBrush(iHatchStyle, crColor);
hBrush = CreateBrushIndirect(&logbrush); //建立逻辑画刷
SelecObject(hdc, hBrush);
然后
DeleteObject(hBrush);
获取画刷信息
GetObject(hBrush, sizeof(LOGBRUSH), (LPVOID)&logbrush);
5.5 GDI映射模式
SetMapMode(hdc, iMapMode);
iMapMode = GetMapMode(hdc);
5.5.2 设备坐标系统
GetWindowDC 获得的hdc 在GDI函数调用中,逻辑坐标会被默认映射为全窗口坐标
ClientToScreen 客户区到屏幕坐标
ScreenToClient 屏幕坐标转换到客户求
5.5.3 视口和窗口
窗口 逻辑坐标 视口 设备坐标
如果原点为(0,0) 可以简化如下
点(xWinExt, yWinExt)是在逻辑坐标系下的窗口范围, 点(xViewExt, yViewExt)是在设备坐标系下的视口范围。
Windows提供两个函数进行设备点与逻辑点的转换
DPtoLP (hdc, pPoints, iNumber);
LPtoDP(hdc, pPoints, iNumber);
5.5.4 使用MM_TEXT
SetViewportOrgEX
SetWindowOrgEx
5.5.5 度量映射模式
5.5.6 自定义映射模式
5.5.7 WHATSIZE程序
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("WhatSize");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("What Size is the Window?"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void Show(HWND hwnd, HDC hdc, int xText, int yText, int iMapMode, TCHAR *szMapMode)
{
TCHAR szBuffer[60];
RECT rect;
SaveDC(hdc);
SetMapMode(hdc, iMapMode);
GetClientRect(hwnd, &rect);
DPtoLP(hdc, (PPOINT)&rect, 2);
RestoreDC(hdc, -1);
TextOut(hdc, xText, yText, szBuffer,
wsprintf(szBuffer, TEXT("%-20s %7d %7d %7d %7d"), szMapMode,
rect.left, rect.right, rect.top, rect.bottom));
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static TCHAR szHeading[] =
TEXT("Mapping Mode Left Right Top Bottom");
static TCHAR szUndLine[] =
TEXT("------------ ---- ----- --- ------");
static int cxChar, cyChar;
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message) //get the message
{
case WM_CREATE:
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, 1, 1, NULL);
SetViewportExtEx(hdc, cxChar, cyChar, NULL);
TextOut(hdc, 1, 1, szHeading, lstrlen(szHeading));
TextOut(hdc, 1, 2, szUndLine, lstrlen(szUndLine));
Show(hwnd, hdc, 1, 3, MM_TEXT, TEXT("TEXT (pixels)"));
Show(hwnd, hdc, 1, 4, MM_LOMETRIC, TEXT("LOMETRIC (.1 mm)"));
Show(hwnd, hdc, 1, 5, MM_HIMETRIC, TEXT("HIMETRIC (.01 mm)"));
Show(hwnd, hdc, 1, 6, MM_LOENGLISH, TEXT("LOENGLISH (.01 in)"));
Show(hwnd, hdc, 1, 7, MM_HIENGLISH, TEXT("HIENGLISH (.001 in)"));
Show(hwnd, hdc, 1, 8, MM_TWIPS, TEXT("TWIPS (1/1440 in)"));
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
运行结果如下
5.6 矩形,区域和剪裁
5.6.1 处理矩形
FillRect (hdc, &rect, bBrush); //填充矩形,不用选入画刷
FrameRect (hdc, &rect, hBrush); // 使用画刷绘制一个矩形框,但是它并不填充矩形
InvertRect (hdc, &rect); //翻转矩形内的所有像素
SetRect(&rect, xLeft, yTop, xRight, yBottom); // 直接设定rect的值
OffsetRect(&rect, x, y); //沿着x轴和y轴平移几个单位
InflateRect(&rect, x, y); //增大或减小矩形的尺寸
SetRectEmpty(&rect); //将矩形的各字段设置为0
CopyRect(&DestRect, &SrcRect); //将一个矩形结构复制到另一个矩形结构
IntersectRect(&DestRect, &SrcRect1, &SrcRect2); // 获得两个矩形的交集
UnionRect(&DestRect, &SrcRect1, &SrcRect2); //获得两个矩形的并集
bEmpty = IsRectEmpty(&rect); //判断矩形是否为空
bInRect = PtInRect(&rect, point); //判断点是否在举行内部
5.6.2 随机矩形
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); 立即返回
GetMessage 等待同步
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message = WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//do other thing.
}
}
一个随机矩形的例子
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
void DrawRectangle(HWND);
int cxClient, cyClient;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("RandRect");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Random Rectangles"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
DrawRectangle(hwnd);
}
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) //get the message
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void DrawRectangle(HWND hwnd)
{
HBRUSH hBrush;
HDC hdc;
RECT rect;
if (cxClient == 0 || cyClient == 0)
return;
SetRect(&rect, rand() % cxClient, rand() % cyClient,
rand() % cxClient, rand() % cyClient);
hBrush = CreateSolidBrush(
RGB(rand() % 256, rand() % 256, rand() % 256));
hdc = GetDC(hwnd);
FillRect(hdc, &rect, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
5.6.3 建立和绘制区域
一个区域是对显示器一块空间的描述,可以使矩形,多边形和椭圆的组合。
hRgn = CreateRectRgn(xLeft, yTop, xRight, yBottom);
或者
hRgn = CreateRectRgnIndirect(&rect);
建立椭圆区域
hRgn = CreateEllipticRgn(xLeft, yTop, xRight, yBottom);
或者
hRgn = CreateEllipticRgnIndirect(&rect);
创建圆角矩形区域
hRgn = CreateRoundRectRgn
多边形区域
hRgn = CreatePolygonRgn(&point, iCount, iPolyFillMode);
CombineRgn 合并区域
FillRgn(hdc, hRgn, hBrush);
FrameRgn(hdc, hRgn, hBrush, xFrame, yFrame);
InvertRgn(hdc, hRgn);
PaintRgn(hdc, hRgn);
用完以后要销毁区域
DeleteObject(hRgn);
5.6.4 矩形与区域的剪裁
使用InvalidateRect 是矩形区域无效 并产生一个WM_PAINT 消息
使用GetUpdateRect 获取无效矩形坐标
使用ValidateRect函数使客户区的矩形有效
类似区域的函数
InvalidateRgn (hwnd, hRgn, bErase);
ValidateRgn(hwnd, hRgn);
将区域选入设备环境
SelectObject(hdc, hRgn);
或者
SelectClipRgn(hdc, hRgn);
当把区域选入设备环境以后可以删除它,以为GDI为剪裁区域做了一个副本。
ExcludeClipRect 从剪裁区域中去除一个矩形
InteresctClipRect 建立一个新的剪裁区域
OffsetClipRgn 把剪裁区域移动到客户区的另外一个部分
5.6.5 Clover程序
#include <windows.h>
#include <math.h>
#define TWO_PI (2.0 * 3.14159)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.
int cxClient, cyClient;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Clover");
HWND hwnd;
MSG msg;
WNDCLASS wndClass; //The window Class
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName;
//Register the Window Class to the Windows System.
if (!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("This program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
//This function will generate an WM_CREATE message.
hwnd = CreateWindow(szAppName, //Window class name
TEXT("Draw a Clover"), //Window caption
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
CW_USEDEFAULT, //initial x size
CW_USEDEFAULT, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); //This function will generate a WM_PAINT message.
/* The message loop for this program.
if received the WM_QUIT message, the function will return 0.*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HRGN hRgnClip;
static int cxClient, cyClient;
double fAngle, fRadius;
HCURSOR hCursor;
HDC hdc;
HRGN hRgnTemp[6];
int i;
PAINTSTRUCT ps;
switch (message) //get the message
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
ShowCursor(TRUE);
if (hRgnClip)
DeleteObject(hRgnClip);
hRgnTemp[0] = CreateEllipticRgn(0, cyClient / 3,
cxClient / 2, 2 * cyClient / 3);
hRgnTemp[1] = CreateEllipticRgn(cxClient / 2, cyClient / 3,
cxClient, 2 * cyClient / 3);
hRgnTemp[2] = CreateEllipticRgn(cxClient / 3, 0,
2 * cxClient / 3, cyClient / 2);
hRgnTemp[3] = CreateEllipticRgn(cxClient / 3, cyClient / 2,
2 * cxClient / 3, cyClient);
hRgnTemp[4] = CreateRectRgn(0, 0, 1, 1);
hRgnTemp[5] = CreateRectRgn(0, 0, 1, 1);
hRgnClip = CreateRectRgn(0, 0, 1, 1);
CombineRgn(hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR);
CombineRgn(hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR);
CombineRgn(hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR);
for (i = 0; i < 6; ++i)
DeleteObject(hRgnTemp[i]);
SetCursor(hCursor);
ShowCursor(FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
SelectClipRgn(hdc, hRgnClip);
fRadius = _hypot(cxClient / 2.0, cyClient / 2.0);
for (fAngle = 0.0; fAngle < TWO_PI; fAngle += TWO_PI / 360)
{
MoveToEx(hdc, 0, 0, NULL);
LineTo(hdc, (int)(fRadius * cos(fAngle) + 0.5),
(int)(-fRadius * sin(fAngle) + 0.5));
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
DeleteObject(hRgnClip);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void DrawRectangle(HWND hwnd)
{
HBRUSH hBrush;
HDC hdc;
RECT rect;
if (cxClient == 0 || cyClient == 0)
return;
SetRect(&rect, rand() % cxClient, rand() % cyClient,
rand() % cxClient, rand() % cyClient);
hBrush = CreateSolidBrush(
RGB(rand() % 256, rand() % 256, rand() % 256));
hdc = GetDC(hwnd);
FillRect(hdc, &rect, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
程序执行结果