0%

Windows程序设计画哆啦A梦

Windows程序设计

最近看Windows程序设计,看到有牛人用原生函数实现哆啦A梦,非常漂亮。但是在个人(Windows10,vs2013)环境中,上述代码存在错误。主要是在SelectObject函数位置。修改错误,并更改配色方案。
可能是SelectObject更新了,直接在SelectObject()中传入

1
2
3
4
HGDIOBJ SelectObject(
HDC hdc,
HGDIOBJ h
);

当为设备环境选择对象并使用之后,需要还原原始的环境对象,这里有两种方法。

  1. 使用SelectObject的返回值

    1
    2
    3
    4
    5
    6
    7
    8
    HBRUSH hBrush;//要替换的环境对象
    HGDIOBJ origin;//保存被替换的原始环境对象,看到过把origin 和hBrush同类型的,但是在windows10,vs2013中,这种定义会报错。
    hBrush = CreateSolidBrush(RGB(0,159,232));//具体实例
    //绘画过程省略

    origin = SelectObject(hdc, hBrush);
    //使用hBrush绘画
    SelectObject(hdc, origin);//恢复原始环境对象,实际上当被替换的对象是HPEN实例时,将origin声明为HGDIOBJ可行的。
  2. 使用DeleteObject

    1
    2
    3
    4
    5
    HBRUSH hBrush;//要替换的环境对象

    hBrush = CreateSolidBrush(RGB(0,159,232));//具体实例
    //绘画过程省略
    DeleteObject(hBrush);

效果与代码

效果图如下
哆啦A梦

代码下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <windows.h>
#include <math.h>

#define NUM 1000
#define TWOPI (2 * 3.1415926)

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = 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(/*GRAY*/WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}

RegisterClass(&wndclass);

hwnd = CreateWindow(szAppName, // window class name
TEXT("The Hello Program"), // 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);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int cxClient, cyClient;
HBRUSH hBrush;
int i;
POINT apt[NUM],
leftHand[] = {
cxClient / 2 - 60, cyClient / 2 - 20,
cxClient / 2 - 100, cyClient / 2 + 10,
cxClient / 2 - 100, cyClient / 2 + 30,
cxClient / 2 - 60, cyClient / 2 + 10
},
rightHand[] = {
cxClient / 2 + 60, cyClient / 2 - 20,
cxClient / 2 + 100, cyClient / 2 + 10,
cxClient / 2 + 100, cyClient / 2 + 30,
cxClient / 2 + 60, cyClient / 2 + 10
};

switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam) + 120;
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);

Ellipse(hdc, cxClient / 2 - 70, cyClient / 2 + 55, cxClient / 2, cyClient / 2 + 85); //脚
Ellipse(hdc, cxClient / 2, cyClient / 2 + 55, cxClient / 2 + 70, cyClient / 2 + 85);

hBrush = CreateSolidBrush(RGB(0,159,232));
SelectObject(hdc, hBrush);

// Rectangle (hdc, cxClient / 2 - 60, cyClient / 2 - 60, cxClient / 2 +60, cyClient / 2 + 70); //身体
RoundRect(hdc, cxClient / 2 - 60, cyClient / 2 - 60, cxClient / 2 + 60, cyClient / 2 + 70, 50, 20);

DeleteObject(hBrush);


SelectObject(hdc, GetStockObject(WHITE_BRUSH));
//肚子部分
Ellipse(hdc, cxClient / 2 - 50, cyClient / 2 - 50, cxClient / 2 + 50, cyClient / 2 + 50);

hBrush = CreateSolidBrush(RGB(0, 159, 232));
SelectObject(hdc, hBrush);
//头部
Ellipse(hdc, cxClient / 2 - 100, cyClient / 2 - 220, cxClient / 2 + 100, cyClient / 2 - 20);
DeleteObject(hBrush);

SelectObject(hdc, GetStockObject(WHITE_BRUSH));

Ellipse(hdc, cxClient / 2 - 85, cyClient / 2 - 170, cxClient / 2 + 85, cyClient / 2 - 20); //脸部
Ellipse(hdc, cxClient / 2 - 40, cyClient / 2 - 190, cxClient / 2, cyClient / 2 - 140); //眼眶左
Ellipse(hdc, cxClient / 2, cyClient / 2 - 190, cxClient / 2 + 40, cyClient / 2 - 140); //眼眶右

SelectObject(hdc, GetStockObject(BLACK_BRUSH));
//眼睛左
Ellipse(hdc, cxClient / 2 - 15, cyClient / 2 - 155, cxClient / 2 - 10, cyClient / 2 - 150);
//眼睛右
Ellipse(hdc, cxClient / 2 + 10, cyClient / 2 - 155, cxClient / 2 + 15, cyClient / 2 - 150);

DeleteObject(hBrush);

hBrush = CreateSolidBrush(RGB(255, 0, 0));
SelectObject(hdc, hBrush);
//鼻子
Ellipse(hdc, cxClient / 2 - 10, cyClient / 2 - 150, cxClient / 2 + 10, cyClient / 2 - 130);

MoveToEx(hdc, cxClient / 2, cyClient / 2 - 130, NULL);
LineTo(hdc, cxClient / 2, cyClient / 2 - 60);

MoveToEx(hdc, cxClient / 2 - 40, cyClient / 2 - 75, NULL); //嘴
for (i = 0; i < NUM / 2; i++)
{
apt[i].x = cxClient / 2 - 40 + i * 160 / NUM;
apt[i].y = cyClient / 2 - 75 + (int)(30 * sin(TWOPI * i / NUM)) / 2;
LineTo(hdc, apt[i].x, apt[i].y);
}

MoveToEx(hdc, cxClient / 2 - 60, cyClient / 2 - 140, NULL); //胡须
LineTo(hdc, cxClient / 2 - 20, cyClient / 2 - 120);

MoveToEx(hdc, cxClient / 2 - 60, cyClient / 2 - 110, NULL);
LineTo(hdc, cxClient / 2 - 20, cyClient / 2 - 110);

MoveToEx(hdc, cxClient / 2 - 60, cyClient / 2 - 80, NULL);
LineTo(hdc, cxClient / 2 - 20, cyClient / 2 - 100);

MoveToEx(hdc, cxClient / 2 + 60, cyClient / 2 - 140, NULL);
LineTo(hdc, cxClient / 2 + 20, cyClient / 2 - 120);

MoveToEx(hdc, cxClient / 2 + 60, cyClient / 2 - 110, NULL);
LineTo(hdc, cxClient / 2 + 20, cyClient / 2 - 110);

MoveToEx(hdc, cxClient / 2 + 60, cyClient / 2 - 80, NULL);
LineTo(hdc, cxClient / 2 + 20, cyClient / 2 - 100);

//口袋
SelectObject(hdc, GetStockObject(WHITE_BRUSH));

Chord(hdc, cxClient / 2 - 40, cyClient / 2 - 40, cxClient / 2 + 40, cyClient / 2 + 40,
cxClient / 2 - 40, cyClient / 2 + 10, cxClient / 2 + 40, cyClient / 2 + 10);



hBrush = CreateSolidBrush(RGB(255, 0, 0)); //脖子上的套圈
SelectObject(hdc, hBrush);

RoundRect(hdc, cxClient / 2 - 70, cyClient / 2 - 40, cxClient / 2 + 70, cyClient / 2 - 20, 20, 20);

DeleteObject(hBrush);

hBrush = CreateSolidBrush(RGB(0, 159, 232)); //手臂
SelectObject(hdc, hBrush);

SetPolyFillMode(hdc, WINDING);
Polygon(hdc, leftHand, 4);

SetPolyFillMode(hdc, WINDING);
Polygon(hdc, rightHand, 4);

DeleteObject(hBrush);

//手
SelectObject(hdc, GetStockObject(WHITE_BRUSH));

Ellipse(hdc, cxClient / 2 - 115, cyClient / 2 + 5, cxClient / 2 - 85, cyClient / 2 + 35);
Ellipse(hdc, cxClient / 2 + 115, cyClient / 2 + 5, cxClient / 2 + 85, cyClient / 2 + 35);

hBrush = CreateSolidBrush(RGB(250, 255, 150)); //铃铛
SelectObject(hdc, hBrush);

Ellipse(hdc, cxClient / 2 - 10, cyClient / 2 - 35, cxClient / 2 + 10, cyClient / 2 - 15);

DeleteObject(hBrush);


SelectObject(hdc, GetStockObject(BLACK_BRUSH));

Ellipse(hdc, cxClient / 2 - 4, cyClient / 2 - 29, cxClient / 2 + 4, cyClient / 2 - 21);

MoveToEx(hdc, cxClient / 2, cyClient / 2 - 25, NULL);
LineTo(hdc, cxClient / 2, cyClient / 2 - 15);

EndPaint(hwnd, &ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

}
return DefWindowProc(hwnd, message, wParam, lParam);
}

坚持原创技术分享,您的支持将鼓励我继续创作!