質問編集履歴
1
コード全文追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,192 +18,268 @@
|
|
18
18
|
|
19
19
|
### 該当のソースコード
|
20
20
|
|
21
|
-
|
22
|
-
|
23
21
|
```C
|
24
22
|
|
23
|
+
#ifndef UNICODE
|
24
|
+
|
25
|
+
#define UNICODE
|
26
|
+
|
27
|
+
#endif
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
#include <Windows.h>
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
#pragma comment(lib,"msimg32.lib")
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int imgWidth = 300;
|
44
|
+
|
45
|
+
int imgHeight = 500;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
const wchar_t CLASS_NAME[] = L"Sample";
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
WNDCLASSEX wc = {
|
58
|
+
|
59
|
+
sizeof(WNDCLASSEX),CS_VREDRAW | CS_HREDRAW,WindowProc,0,
|
60
|
+
|
61
|
+
0,hInstance,NULL,LoadCursor(NULL, IDC_ARROW),
|
62
|
+
|
63
|
+
(HBRUSH)WHITE_BRUSH,NULL,CLASS_NAME,NULL
|
64
|
+
|
65
|
+
};
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
RegisterClassEx(&wc);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
HWND hwnd = CreateWindowEx(
|
74
|
+
|
75
|
+
0, CLASS_NAME, L"Quick Image Rendering", WS_OVERLAPPEDWINDOW,
|
76
|
+
|
77
|
+
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
78
|
+
|
79
|
+
NULL, NULL, hInstance, NULL
|
80
|
+
|
81
|
+
);
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
if (hwnd == NULL)return 0;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
ShowWindow(hwnd, nCmdShow);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
MSG msg = {};
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
while (GetMessage(&msg, NULL, 0, 0))
|
98
|
+
|
99
|
+
{
|
100
|
+
|
101
|
+
TranslateMessage(&msg);
|
102
|
+
|
103
|
+
DispatchMessage(&msg);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
return 0;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
114
|
+
|
115
|
+
switch (uMsg) {
|
116
|
+
|
117
|
+
case WM_DESTROY:
|
118
|
+
|
119
|
+
PostQuitMessage(0);
|
120
|
+
|
121
|
+
return 0;
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
case WM_CREATE:
|
126
|
+
|
127
|
+
break;
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
case WM_SIZE:
|
132
|
+
|
133
|
+
InvalidateRect(hwnd, NULL, TRUE);
|
134
|
+
|
135
|
+
UpdateWindow(hwnd);
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
break;
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
case WM_PAINT:
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
HDC hdc; //for window
|
148
|
+
|
149
|
+
HDC himgdc; //for image
|
150
|
+
|
151
|
+
BLENDFUNCTION bf;
|
152
|
+
|
153
|
+
BITMAPINFO bmpi;
|
154
|
+
|
155
|
+
HBITMAP hbitmap;
|
156
|
+
|
157
|
+
VOID* pv;
|
158
|
+
|
159
|
+
int winWidth, winHeight;
|
160
|
+
|
161
|
+
RECT rClient;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
hdc = GetDC(hwnd);
|
166
|
+
|
167
|
+
GetClientRect(hwnd, &rClient);
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
winWidth = rClient.right - rClient.left;
|
172
|
+
|
173
|
+
winHeight = rClient.bottom - rClient.top;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
himgdc = CreateCompatibleDC(hdc);
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
ZeroMemory(&bmpi, sizeof(BITMAPINFO));
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
186
|
+
|
187
|
+
bmpi.bmiHeader.biWidth = imgWidth;
|
188
|
+
|
189
|
+
bmpi.bmiHeader.biHeight = imgHeight;
|
190
|
+
|
191
|
+
bmpi.bmiHeader.biPlanes = 1;
|
192
|
+
|
193
|
+
bmpi.bmiHeader.biBitCount = 32;
|
194
|
+
|
195
|
+
bmpi.bmiHeader.biCompression = BI_RGB;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
hbitmap = CreateDIBSection(himgdc, &bmpi, DIB_RGB_COLORS, &pv, NULL, 0x0);
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
SelectObject(himgdc, hbitmap);
|
204
|
+
|
205
|
+
|
206
|
+
|
25
|
-
//
|
207
|
+
//DIBの動的作成
|
208
|
+
|
26
|
-
|
209
|
+
DWORD **image = (DWORD**)malloc(sizeof(DWORD*)*imgWidth);
|
210
|
+
|
211
|
+
DWORD *image_c = (DWORD*)malloc(sizeof(DWORD)*imgWidth*imgHeight);
|
212
|
+
|
213
|
+
for (int i = 0; i < imgWidth; i++)image[i] = image_c + i * imgHeight;
|
214
|
+
|
215
|
+
for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)image[i][j] = 0x00000000;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
for (int i = imgWidth/2+1; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)image[i][j] = 0x7f7f0000;
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
DWORD *dib = (DWORD*)calloc(sizeof(DWORD)*imgHeight*imgWidth, sizeof(sizeof(DWORD)*imgHeight*imgWidth));
|
224
|
+
|
225
|
+
for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)dib[i + j * imgWidth] = image[i][j];
|
226
|
+
|
227
|
+
//DIBの動的作成ここまで
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
pv = dib;
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
bf.BlendOp = AC_SRC_OVER;
|
236
|
+
|
237
|
+
bf.BlendFlags = 0;
|
238
|
+
|
239
|
+
bf.SourceConstantAlpha = 0xff;
|
240
|
+
|
241
|
+
bf.AlphaFormat = AC_SRC_ALPHA;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if (!AlphaBlend(
|
246
|
+
|
247
|
+
hdc, 0, 0, winWidth, winHeight,
|
248
|
+
|
27
|
-
i
|
249
|
+
himgdc, 0, 0, imgWidth, imgHeight, bf))
|
250
|
+
|
251
|
+
return 0;
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
DeleteObject(hbitmap);
|
256
|
+
|
257
|
+
DeleteDC(himgdc);
|
258
|
+
|
259
|
+
DeleteDC(hdc);
|
260
|
+
|
261
|
+
free(dib);
|
262
|
+
|
263
|
+
free(image_c);
|
264
|
+
|
265
|
+
free(image);
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
break;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
278
|
+
|
279
|
+
}
|
28
280
|
|
29
281
|
```
|
30
282
|
|
31
|
-
procedureのみ
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
```C
|
36
|
-
|
37
|
-
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
38
|
-
|
39
|
-
switch (uMsg) {
|
40
|
-
|
41
|
-
case WM_DESTROY:
|
42
|
-
|
43
|
-
PostQuitMessage(0);
|
44
|
-
|
45
|
-
return 0;
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
case WM_CREATE:
|
50
|
-
|
51
|
-
break;
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
case WM_SIZE:
|
56
|
-
|
57
|
-
InvalidateRect(hwnd, NULL, TRUE);
|
58
|
-
|
59
|
-
UpdateWindow(hwnd);
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
break;
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
case WM_PAINT:
|
68
|
-
|
69
|
-
{
|
70
|
-
|
71
|
-
HDC hdc; //for window
|
72
|
-
|
73
|
-
HDC himgdc; //for image
|
74
|
-
|
75
|
-
BLENDFUNCTION bf;
|
76
|
-
|
77
|
-
BITMAPINFO bmpi;
|
78
|
-
|
79
|
-
HBITMAP hbitmap;
|
80
|
-
|
81
|
-
VOID* pv;
|
82
|
-
|
83
|
-
int winWidth, winHeight;
|
84
|
-
|
85
|
-
RECT rClient;
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
hdc = GetDC(hwnd);
|
90
|
-
|
91
|
-
GetClientRect(hwnd, &rClient);
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
winWidth = rClient.right - rClient.left;
|
96
|
-
|
97
|
-
winHeight = rClient.bottom - rClient.top;
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
himgdc = CreateCompatibleDC(hdc);
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
ZeroMemory(&bmpi, sizeof(BITMAPINFO));
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
110
|
-
|
111
|
-
bmpi.bmiHeader.biWidth = imgWidth;
|
112
|
-
|
113
|
-
bmpi.bmiHeader.biHeight = imgHeight;
|
114
|
-
|
115
|
-
bmpi.bmiHeader.biPlanes = 1;
|
116
|
-
|
117
|
-
bmpi.bmiHeader.biBitCount = 32;
|
118
|
-
|
119
|
-
bmpi.bmiHeader.biCompression = BI_RGB;
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
hbitmap = CreateDIBSection(himgdc, &bmpi, DIB_RGB_COLORS, &pv, NULL, 0x0);
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
SelectObject(himgdc, hbitmap);
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//DIBの動的作成
|
132
|
-
|
133
|
-
DWORD **image = (DWORD**)malloc(sizeof(DWORD*)*imgWidth);
|
134
|
-
|
135
|
-
DWORD *image_c = (DWORD*)malloc(sizeof(DWORD)*imgWidth*imgHeight);
|
136
|
-
|
137
|
-
for (int i = 0; i < imgWidth; i++)image[i] = image_c + i * imgHeight;
|
138
|
-
|
139
|
-
for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)image[i][j] = 0x00000000;
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
for (int i = imgWidth/2+1; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)image[i][j] = 0x7f7f0000;
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
DWORD *dib = (DWORD*)calloc(sizeof(DWORD)*imgHeight*imgWidth, sizeof(sizeof(DWORD)*imgHeight*imgWidth));
|
148
|
-
|
149
|
-
for (int i = 0; i < imgWidth; i++)for (int j = 0; j < imgHeight; j++)dib[i + j * imgWidth] = image[i][j];
|
150
|
-
|
151
|
-
//DIBの動的作成ここまで
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
pv = dib;
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
bf.BlendOp = AC_SRC_OVER;
|
160
|
-
|
161
|
-
bf.BlendFlags = 0;
|
162
|
-
|
163
|
-
bf.SourceConstantAlpha = 0xff;
|
164
|
-
|
165
|
-
bf.AlphaFormat = AC_SRC_ALPHA;
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
if (!AlphaBlend(
|
170
|
-
|
171
|
-
hdc, 0, 0, winWidth, winHeight,
|
172
|
-
|
173
|
-
himgdc, 0, 0, imgWidth, imgHeight, bf))
|
174
|
-
|
175
|
-
return 0;
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
DeleteObject(hbitmap);
|
180
|
-
|
181
|
-
DeleteDC(himgdc);
|
182
|
-
|
183
|
-
DeleteDC(hdc);
|
184
|
-
|
185
|
-
free(dib);
|
186
|
-
|
187
|
-
free(image_c);
|
188
|
-
|
189
|
-
free(image);
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
break;
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
```
|
206
|
-
|
207
283
|
|
208
284
|
|
209
285
|
|