#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* The 'main' function of Win32 GUI programs: this is where execution starts */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口类名*/ HWND hwnd; /*窗口句柄*/ MSG msg; /*消息*/ WNDCLASS wndclass; /*窗口类*/ /***********第一步:注册窗口类*************/ /*为窗口类各个字段赋值*/ wndclass.style = CS_HREDRAW | CS_VREDRAW; /*窗口风格*/ wndclass.lpfnWndProc = WndProc; /*窗口过程*/ 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= szClassName; /*窗口类名*/ /*注册窗口*/ RegisterClass(&wndclass); /*************第二步:创建窗口(并让窗口显示出来)***************/ hwnd = CreateWindow( szClassName, /*窗口名字*/ TEXT("Welcome"), /*窗口标题*/ WS_OVERLAPPEDWINDOW, /*窗口风格*/ CW_USEDEFAULT, /*初始化x轴的位置*/ CW_USEDEFAULT, /*初始化y轴的位置*/ 640, /*窗口宽度*/ 480, /*窗口高度*/ NULL, /*父窗口句柄*/ NULL, /*窗口菜单句柄*/ hInstance, /*当前窗口句柄*/ NULL /*不使用该值*/ ); if(hwnd == NULL) { MessageBox(NULL, "创建窗口出错!", "Error", MB_OK); return -1; } /*显示窗口*/ ShowWindow(hwnd, nCmdShow); /*更新(绘制)窗口*/ UpdateWindow(hwnd); /*************第三步:消息循环*************/ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); /*翻译消息*/ DispatchMessage(&msg); /*分派消息*/ } return msg.wParam; /*当GetMessage程序返回FALSE是程序结束*/ } /*************第四步:窗口过程*****************/ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; /*定义一个画笔句柄,定义为静态变量*/ static HPEN hPen; /*定义一个画刷句柄,定义为静态变量*/ static HBRUSH hSolidBrush; switch(message) { case WM_CREATE: { /*创建笔触大小为2个像素的红色点线画笔,保存句柄到hPen变量*/ hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); /*创建实心画刷,保存到句柄hSolidBrush变量*/ hSolidBrush = CreateSolidBrush(RGB(12, 0, 255)); break; } case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); SelectObject(hdc, hPen); /*选入画笔到设备环境*/ SelectObject(hdc, hSolidBrush); /*选入画刷到设备环境*/ Ellipse(hdc, 0, 100, 200, 200); EndPaint(hWnd, &ps); break; } /*窗口销毁消息*/ case WM_DESTROY: { /*处理WM_DESTROY消息时删除之前我们创建的一切GDI对象*/ DeleteObject(hPen); DeleteObject(hSolidBrush); PostQuitMessage(0); break; } default: { return DefWindowProc(hWnd, message, wParam, lParam); } } return 0; }
CreateHatchBrush 函数可以用来创建一个指定颜色的含有特定阴影样式的画刷,原型为:
HBRUSH CreateHatchBrush(int fnStyle, //画刷样式COLORREF crColor //画刷颜色);
fnStyle 可以有6种取值:
HS_BDIGONAL:45度向上,自左至右的阴影(///)
HS_CROSS:表示水平直线和垂直直线交叉阴影(+++)
HS_DIAGCROSS:45度交叉阴影(XXX)
HS_FDIAGONAL:45度向下自左至右的阴影(\\\)
HS_HORIZONTAL:水平阴影(---)
HS_VERTICAL:垂直阴影
定义:
//创建绿色交叉阴影画刷,保存句柄到 hHatchBrush 变量 hHatchBrush = CreateHatchBrush(HS_DIAGCROSS,RGB(0,255,0));