/* 画笔练习程序, by netrookie, ChinaUnix.
* 请参考《windows程序设计》第五章:图形基础,画笔部分
* 此程序通过一个switch结构, 告诉我们怎样创建画笔和删除画笔。
*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("MyPen");
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 = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static int cxClient, cyClient, i;
HDC hdc;
PAINTSTRUCT ps;
HPEN hPen;
LOGPEN logPen;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
i = 0;
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
switch(i) {
case 0:
hPen = CreatePen(PS_SOLID, 2, RGB(10, 10, 10));
break;
case 1:
hPen = CreatePen(PS_DASH, 1, RGB(20, 20, 20));
break;
case 2:
hPen = CreatePen(PS_DOT, 1, RGB(255, 30, 30));
break;
case 3:
hPen = CreatePen(PS_DASHDOT, 1, RGB(40, 40, 40));
break;
case 4:
hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(50, 50, 50));
break;
case 5:
logPen.lopnStyle = PS_SOLID;
logPen.lopnColor = RGB(0, 60, 60);
/*
* 此处是非常有意思的,下面有说明
*/
logPen.lopnWidth.x = 5;
hPen = CreatePenIndirect(&logPen);
break;
default:
hPen = GetStockObject(BLACK_PEN);
break;
}
SelectObject(hdc, hPen);
MoveToEx(hdc, 0, (i + 1) * cyClient / 10, NULL);
LineTo(hdc, cxClient, (i + 1) * cyClient / 10);
if(i++ <= 5)
InvalidateRect(hwnd, NULL, FALSE);
EndPaint(hwnd, &ps);
// 删除GDI对象, 释放内存!!
DeleteObject(hPen);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|