本文主要是介绍Visual C++游戏编程基础之画笔、画刷的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、概念
画笔:线条的样式
画刷:封闭图形内部填充的样式
二、重要函数介绍
1.HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor);
function:指定的样式、宽度和颜色创建画笔
nPenStyle:实线、虚线、点线等等
nWidth:线宽
crColor:颜色
2.HBRUSH CreateHatchBrush(int fnStyle, COLORREF clrref);
function:建立阴影画刷
fnStyle:指定刷子阴如式,如----、++++等等
cirref:指定用于阴影刷子的前景色
3.CreateSolidBrush(COLORREF clrref);
function:创建一个具有指定颜色的逻辑刷子
4.HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj);
function:选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
5.WINGDIAPI BOOL WINAPI MoveToEx(HDC hdc,int X,int Y,LPPOINT lpPoint);
function:将当前绘图位置移动到某个具体的点
hdc:设备上下文句柄
x,y:新位置的坐标
lpPoint:存放上一个点的位置
6.WINGDIAPI BOOL WINAPI LineTo(HDChdc,intX,intY,);
function:用当前画笔画一条线,从当前位置连到一个指定的点
7.BOOL Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
function:画一个矩形
三、实现代码
#include "stdafx.h"HINSTANCE hInst;
HPEN hPen[7];//定义画笔
HBRUSH hBru[7];//定义画刷
//画笔数组包含7种样式
int sPen[7] = {PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};
//阴影画刷数组包含6种样式
int sBru[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{MSG msg;MyRegisterClass(hInstance);if (!InitInstance (hInstance, nCmdShow)) {return FALSE;}while (GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;
}ATOM MyRegisterClass(HINSTANCE hInstance)
{WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = NULL;wcex.hCursor = NULL;wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = "canvas";wcex.hIconSm = NULL;return RegisterClassEx(&wcex);
}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{HWND hWnd;HDC hdc;int i;hInst = hInstance;hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (!hWnd) {return FALSE;}MoveWindow(hWnd,10,10,650,350,true);ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);for(i=0;i<=6;i++){hPen[i] = CreatePen(sPen[i],1,RGB(255,0,0));//颜色为红色if(i==6)hBru[i] = CreateSolidBrush(RGB(0,255,0));//建立绿色画刷elsehBru[i] = CreateHatchBrush(sBru[i],RGB(0,255,0));//建立6种阴影画刷}hdc = GetDC(hWnd);//成功,返回指定窗口客户区的设备上下文环境;失败,返回值为NullMyPaint(hdc);ReleaseDC(hWnd,hdc);return TRUE;
}void MyPaint(HDC hdc)
{int i,x1,x2,y;for(i=0;i<=6;i++){y = (i+1) * 30;SelectObject(hdc,hPen[i]);//该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象MoveToEx(hdc,30,y,NULL); //移动画笔至线条起点LineTo(hdc,100,y); //绘制线条至指定坐标}x1 = 120;x2 = 180;for(i=0;i<=6;i++){SelectObject(hdc,hBru[i]);Rectangle(hdc,x1,30,x2,y); //绘制矩形区域,阴影画刷填充该矩形区域x1 += 70;x2 += 70;}
}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{PAINTSTRUCT ps;HDC hdc;int i;switch (message) {case WM_PAINT: hdc = BeginPaint(hWnd, &ps);MyPaint(hdc);EndPaint(hWnd, &ps);break;case WM_DESTROY: for(i=0;i<=6;i++){DeleteObject(hPen[i]); DeleteObject(hBru[i]); }PostQuitMessage(0);break;default: return DefWindowProc(hWnd, message, wParam, lParam);}return 0;
}
四、效果
这篇关于Visual C++游戏编程基础之画笔、画刷的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!