質問編集履歴
3
Edit title
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
WinAPI BITMAPを読み込んでBITMAPの描画
|
1
|
+
WinAPI BITMAPを読み込んでBITMAPの描画・UI
|
body
CHANGED
File without changes
|
2
IDE's version up
title
CHANGED
File without changes
|
body
CHANGED
@@ -294,4 +294,4 @@
|
|
294
294
|
|
295
295
|
### 補足情報
|
296
296
|
Windows10 Pro
|
297
|
-
|
297
|
+
VisualStudio2019 Community
|
1
コメント追加・本文修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
BMPをウィンドウにドロップするとファイルヘッダ―等を読み取って情報を画像とともに表示するコードを書いています.圧縮の有無判別はまだしていませんからかなり荒いですが画像の表示ができません.また文字の表示が一回上書きされてしまいドロップ時に文字列が表示されません.画像のデータの取得ミスのように思いますがお願いします.
|
2
|
+
BMPをウィンドウにドロップするとファイルヘッダ―等を読み取って情報を画像とともに表示するコードを書いています.圧縮の有無判別はまだしていませんからかなり荒いですが画像の表示ができません.~~また文字の表示が一回上書きされてしまいドロップ時に文字列が表示されません.~~ ~~画像のデータの取得ミスのように思いますがお願いします.~~
|
3
3
|
|
4
4
|
また文字列変数の定義場所や変数の代入方法において改善点がありましたらお願いします.(`wsprintf`が長くなりすぎている点等)
|
5
5
|
|
6
|
+
### 修正および追記
|
7
|
+
WM_PAINT周辺は変更したつもりですが223行目のポインターに代入すると例外が投げられます.引数のbitmapのSizeiImageを再代入してみたりしましたが私のデバッグの知識ではそれ以上把握できませんでした.画像の描画領域の確保まではいけていると思いますが画像が表示できません.
|
8
|
+
GetDCをWM_CREATEで入手していいものかその辺りの変なところもご指摘お願いします.
|
9
|
+
|
6
10
|
### 該当のソースコード
|
7
11
|
|
8
12
|
```C
|
@@ -32,7 +36,7 @@
|
|
32
36
|
RegisterClassEx(&wc);
|
33
37
|
|
34
38
|
HWND hwnd = CreateWindowEx(
|
35
|
-
WS_EX_ACCEPTFILES, CLASS_NAME, L"
|
39
|
+
WS_EX_ACCEPTFILES, CLASS_NAME, L"Template", WS_OVERLAPPEDWINDOW,
|
36
40
|
50, 50, 960, 525,
|
37
41
|
NULL, NULL, hInstance, NULL
|
38
42
|
);
|
@@ -51,207 +55,233 @@
|
|
51
55
|
}
|
52
56
|
|
53
57
|
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
54
|
-
static BOOL
|
58
|
+
static BOOL status_b_open = FALSE;
|
59
|
+
static DWORD status_d_head = 0;
|
60
|
+
|
55
61
|
static wchar_t filename[_MAX_PATH] = { 0 };
|
62
|
+
|
63
|
+
static RECT rc = { 0, 0, 0, 0 };
|
64
|
+
static RECT src = { 5, 5, 0, 0 };
|
65
|
+
static BITMAPFILEHEADER bmfh;
|
66
|
+
static BITMAPV5HEADER* bmih;
|
67
|
+
|
68
|
+
|
69
|
+
static HDC hdc;
|
70
|
+
static HDC hBuffer;
|
71
|
+
static HBITMAP hbmp;
|
56
72
|
static HANDLE hFile;
|
73
|
+
static HBITMAP bm;
|
74
|
+
|
75
|
+
|
57
76
|
static BYTE* filedata;
|
58
|
-
static LPDWORD rfilesize;
|
59
|
-
static RECT rc;
|
60
|
-
static RECT src = { 5,5,0,0 };
|
61
|
-
static BITMAPFILEHEADER input;
|
62
|
-
static BITMAPV5HEADER *bmi = { 0 };
|
63
|
-
static HBITMAP hbmp;
|
64
|
-
static HDC hbuffer;
|
65
|
-
static void
|
77
|
+
static void* pv;
|
66
|
-
static HDC hdc;
|
67
|
-
static BITMAP bm;
|
68
|
-
|
69
78
|
static wchar_t pstring[4096];
|
70
79
|
|
71
80
|
switch (uMsg) {
|
72
|
-
|
73
81
|
case WM_CREATE:
|
82
|
+
hdc = GetDC(hwnd);
|
74
83
|
break;
|
75
84
|
|
76
85
|
case WM_PAINT:
|
77
86
|
{
|
78
|
-
|
87
|
+
if (status_b_open == TRUE) {
|
79
|
-
|
88
|
+
if (StretchBlt(hdc, 250, 50, bmih->bV5Width / 2, bmih->bV5Height / 2, hBuffer, 0, 0, bmih->bV5Width, bmih->bV5Height, SRCCOPY) == FALSE) {
|
80
|
-
|
89
|
+
MessageBox(hwnd, L"Failed to drawtext", L"CAUTION", MB_OK);
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
81
|
-
DrawText(hdc, pstring, -1, &src,
|
93
|
+
DrawText(hdc, pstring, -1, &src, DT_LEFT | DT_TOP);
|
94
|
+
|
95
|
+
ValidateRect(hwnd, NULL);
|
82
96
|
}
|
83
97
|
break;
|
98
|
+
case WM_SIZE:
|
99
|
+
{
|
100
|
+
GetClientRect(hwnd, &rc);
|
101
|
+
src.right = rc.right;
|
102
|
+
src.bottom = rc.bottom;
|
103
|
+
}
|
104
|
+
break;
|
84
105
|
|
106
|
+
|
85
107
|
case WM_DROPFILES:
|
86
108
|
{
|
109
|
+
/* temporary param */
|
110
|
+
LPDWORD rfsize=0;
|
87
|
-
|
111
|
+
wchar_t tbmp[64] = { 0 };
|
88
|
-
if (MessageBox(hwnd, L"Replace this image?", L"CAUTION", MB_YESNO) == IDNO)break;
|
89
|
-
else {
|
90
|
-
|
112
|
+
wchar_t infoheadstatus[32] = { 0 };
|
91
|
-
CloseHandle(hFile);
|
92
|
-
|
113
|
+
wchar_t getStatus[1025] = { 0 };
|
93
|
-
|
114
|
+
/* End temporary param */
|
94
|
-
}
|
95
115
|
|
96
|
-
|
116
|
+
/* Get Droped File */
|
97
|
-
|
117
|
+
wchar_t openedfilename[_MAX_PATH] = { 0 };
|
118
|
+
wcscpy_s(openedfilename, _MAX_PATH, filename);
|
98
119
|
HDROP hDrop = (HDROP)wParam;
|
99
120
|
unsigned cof = DragQueryFile(hDrop, -1, NULL, 0);
|
100
|
-
if (cof
|
121
|
+
if (cof == -1)MessageBox(hwnd, L"This application arrowed only 1 file at the same time", L"CAUTION", MB_OK);//alert when i get plural files -> get the first file
|
101
122
|
DragQueryFile(hDrop, 0, filename, _MAX_PATH);
|
102
|
-
|
123
|
+
/* End Get Droped File */
|
103
124
|
|
104
|
-
size_t fsize = GetFileSize(hFile, NULL);
|
105
|
-
filedata = (BYTE*)malloc(fsize);
|
106
|
-
if (ReadFile(hFile, filedata, fsize, rfilesize, NULL) == FALSE) {
|
107
|
-
MessageBox(hwnd, L"FAILED TO READ", L"CAUTION", MB_OK);
|
108
|
-
return -1;
|
109
|
-
}
|
110
125
|
|
111
|
-
wchar_t tbmp[64] = { 0 };
|
112
|
-
|
126
|
+
if (wcscmp(filename, openedfilename) != 0) {//process when the droped file is different with before one
|
113
|
-
DWORD wsize = 0;
|
114
127
|
|
115
|
-
size_t hotspotx = filedata[6] << 8 | filedata[7];
|
116
|
-
|
128
|
+
/* Copy File */
|
129
|
+
hFile = CreateFile(filename, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
117
130
|
|
118
|
-
|
131
|
+
size_t fsize = GetFileSize(hFile, NULL);
|
119
|
-
|
120
|
-
|
132
|
+
rfsize = 0;
|
121
|
-
case 'BM':
|
122
|
-
{
|
123
|
-
|
133
|
+
filedata = (BYTE*)malloc(fsize);
|
124
|
-
if (
|
134
|
+
if (ReadFile(hFile, filedata, fsize, rfsize, NULL) == FALSE) {
|
125
|
-
|
135
|
+
MessageBox(hwnd, L"FAILED TO READ", L"CAUTION", MB_OK);
|
126
|
-
|
136
|
+
return -1;
|
127
137
|
}
|
128
|
-
else {
|
129
|
-
wsprintf(tbmp, L"BMP file\nBITMAPFILEHEADER : version.2");
|
130
|
-
|
138
|
+
/* End Copy File */
|
131
|
-
}
|
132
139
|
|
140
|
+
/* Get Magic Number */
|
133
|
-
|
141
|
+
DWORD dwHead = filedata[0] << 8 | filedata[1];
|
134
|
-
}
|
135
|
-
break;
|
136
142
|
|
143
|
+
size_t hotspotx = filedata[6] << 8 | filedata[7];
|
144
|
+
size_t hotspoty = filedata[8] << 8 | filedata[9];
|
145
|
+
|
146
|
+
DWORD wsize = 0;
|
147
|
+
switch (dwHead) {
|
137
|
-
|
148
|
+
case 'BM':
|
149
|
+
{
|
138
|
-
|
150
|
+
int* s = (int*)(filedata + 0xe);
|
139
|
-
case 'PT':
|
140
|
-
case 'CP':
|
141
|
-
|
151
|
+
if (hotspotx == 0 && hotspoty == 0 && *s != 12) {
|
152
|
+
wsprintf(tbmp, L"BMP file\nBITMAPFILEHEADER : version.1");
|
153
|
+
status_d_head = 0x100;
|
154
|
+
}
|
155
|
+
else {
|
142
|
-
|
156
|
+
wsprintf(tbmp, L"BMP file\nBITMAPFILEHEADER : version.2");
|
143
|
-
|
157
|
+
status_d_head = 0x200;
|
158
|
+
}
|
159
|
+
|
144
160
|
wsize = filedata[2] | filedata[3] << 8 | filedata[4] << 16 | filedata[5] << 24;
|
145
|
-
break;
|
146
161
|
}
|
162
|
+
break;
|
163
|
+
|
164
|
+
case 'IC':
|
165
|
+
case 'CI':
|
166
|
+
case 'PT':
|
167
|
+
case 'CP':
|
168
|
+
if (hotspotx != 0 && hotspoty != 0) {
|
169
|
+
wsprintf(tbmp, L"BMP file\nBITMAPFILEHEADER : version.2");
|
170
|
+
status_d_head = 0x200;
|
171
|
+
wsize = filedata[2] | filedata[3] << 8 | filedata[4] << 16 | filedata[5] << 24;
|
172
|
+
break;
|
173
|
+
}
|
147
|
-
|
174
|
+
else {
|
148
|
-
|
175
|
+
wsprintf(tbmp, L"BMP file\nBITMAPFILEHEADER : version.2 but not use hotspot");
|
149
|
-
|
176
|
+
status_d_head = 0x200;
|
177
|
+
wsize = 0;
|
178
|
+
break;
|
179
|
+
}
|
180
|
+
default:
|
181
|
+
wsprintf(tbmp, L"not BMP file");
|
150
182
|
wsize = 0;
|
151
|
-
|
183
|
+
status_d_head = 0x0;
|
152
184
|
}
|
153
|
-
default:
|
154
|
-
wsprintf(tbmp, L"not BMP file");
|
155
|
-
wsize = 0;
|
156
|
-
bitmapfileheaderver = 0;
|
157
|
-
}
|
158
|
-
|
185
|
+
/* End Get Magic Number */
|
159
186
|
|
160
|
-
size_t tableoff = filedata[0xa] | filedata[0xb] << 8 | filedata[0xc] << 16 | filedata[0xd] << 24;
|
161
187
|
|
188
|
+
/* Check File version and Fill defined structure */
|
162
|
-
|
189
|
+
/* Remark : If the file is classified as Bitmap for Windows, i read the file data */
|
163
190
|
|
164
|
-
wchar_t infoheadstatus[32] = { 0 };
|
165
|
-
wchar_t getStatus[1025] = { 0 };
|
166
191
|
|
192
|
+
size_t tableoff = filedata[0xa] | filedata[0xb] << 8 | filedata[0xc] << 16 | filedata[0xd] << 24;//the offset of the table lead from the file lead
|
193
|
+
size_t infosize = filedata[0xe] | filedata[0xf] << 8 | filedata[0x10] << 16 | filedata[0x11] << 24;//size of header
|
167
194
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
bitmapinfoheaderver = 1;
|
195
|
+
switch (status_d_head) {
|
196
|
+
case 0x100:
|
197
|
+
{
|
198
|
+
switch (infosize) {
|
199
|
+
case 40:
|
200
|
+
wsprintf(infoheadstatus, L"version.1 for Win");
|
201
|
+
bmih = (BITMAPV5HEADER*)(filedata + 0xe);
|
202
|
+
wsprintf(getStatus, L"biSize : %d\nbiWidth : %d\nbiHeight : %d\nbiPlanes : %d\nbiBitCount : %d\nbiCompression : %d\nbiSizeImage : %d\nbiXPelsPerMeter : %d\nbiYPelsPerMeter : %d\nbiClrUsed : %d\nbiClrImportant : %d\n\0", bmih->bV5Size, bmih->bV5Width, bmih->bV5Height, bmih->bV5Planes, bmih->bV5BitCount, bmih->bV5Compression, bmih->bV5SizeImage, bmih->bV5XPelsPerMeter, bmih->bV5YPelsPerMeter, bmih->bV5ClrUsed, bmih->bV5ClrImportant);
|
203
|
+
status_d_head |= 0x1;
|
178
204
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
205
|
+
break;
|
206
|
+
case 108:
|
207
|
+
wsprintf(infoheadstatus, L"version.4");
|
208
|
+
bmih = (BITMAPV5HEADER*)(filedata + 0xe);
|
209
|
+
wsprintf(getStatus, L"bV4Size : %d\nbV4Width : %d\nbV4Height : %d\nbV4Planes : %d\nbV4bV4tCount : %d\nbV4Compression : %d\nbV4SizeImage : %d\nbV4XPelsPerMeter : %d\nbV4YPelsPerMeter : %d\nbV4ClrUsed : %d\nbV4ClrImportant : %d\nbV4RedMask : %d\nbV4GreenMask : %d\nbV4BlueMask : %d\nbV4AlphaMask : %d\nbV4CSType : %d\nbV4Endpoints : %p\nbV4GammaRed : %d\nbV4GammaGreen : %d\nbV4GammaBlue : %d\n\0", bmih->bV5Size, bmih->bV5Width, bmih->bV5Height, bmih->bV5Planes, bmih->bV5BitCount, bmih->bV5Compression, bmih->bV5SizeImage, bmih->bV5XPelsPerMeter, bmih->bV5YPelsPerMeter, bmih->bV5ClrUsed, bmih->bV5ClrImportant, bmih->bV5RedMask, bmih->bV5GreenMask, bmih->bV5BlueMask, bmih->bV5AlphaMask, bmih->bV5CSType, bmih->bV5Endpoints, bmih->bV5GammaRed, bmih->bV5GammaGreen, bmih->bV5GammaBlue);
|
210
|
+
status_d_head |= 0x2;
|
185
211
|
|
186
|
-
|
212
|
+
break;
|
187
213
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
214
|
+
case 124:
|
215
|
+
wsprintf(infoheadstatus, L"version.5");
|
216
|
+
bmih = (BITMAPV5HEADER*)(filedata + 0xe);
|
217
|
+
wsprintf(getStatus, L"bV5Size : %d\nbV5Width : %d\nbV5Height : %d\nbV5Planes : %d\nbV5BitCount : %d\nbV5Compression : %d\nbV5SizeImage : %d\nbV5XPelsPerMeter : %d\nbV5YPelsPerMeter : %d\nbV5ClrUsed : %d\nbV5ClrImportant : %d\nbV5RedMask : %d\nbV5GreenMask : %d\nbV5BlueMask : %d\nbV5AlphaMask : %d\nbV5CSType : %d\nbV5Endpoints : %p\nbV5GammaRed : %d\nbV5GammaGreen : %d\nbV5GammaBlue : %d\nbV5Intent : %d\nbV5ProfileData : %d\nbV5ProfileSize : %d\nbV5Reserved%d\n\0", bmih->bV5Size, bmih->bV5Width, bmih->bV5Height, bmih->bV5Planes, bmih->bV5BitCount, bmih->bV5Compression, bmih->bV5SizeImage, bmih->bV5XPelsPerMeter, bmih->bV5YPelsPerMeter, bmih->bV5ClrUsed, bmih->bV5ClrImportant, bmih->bV5RedMask, bmih->bV5GreenMask, bmih->bV5BlueMask, bmih->bV5AlphaMask, bmih->bV5CSType, (tagICEXYZTRIPLE)bmih->bV5Endpoints, bmih->bV5GammaRed, bmih->bV5GammaGreen, bmih->bV5GammaBlue, bmih->bV5Intent, bmih->bV5ProfileData, bmih->bV5ProfileSize, bmih->bV5Reserved);
|
218
|
+
status_d_head |= 0x3;
|
193
219
|
|
194
|
-
|
220
|
+
break;
|
195
221
|
|
196
|
-
|
222
|
+
default:
|
197
|
-
|
223
|
+
wsprintf(infoheadstatus, L"BROKEN BMP");
|
198
224
|
|
199
|
-
|
225
|
+
goto FileCheckEnd;
|
200
|
-
|
226
|
+
}
|
201
227
|
|
228
|
+
/* Image Rendering */
|
229
|
+
|
202
|
-
|
230
|
+
hBuffer = CreateCompatibleDC(hdc);
|
203
231
|
|
232
|
+
hbmp = CreateDIBSection(hBuffer, (BITMAPINFO*)bmih, DIB_RGB_COLORS, &pv, NULL, NULL);
|
233
|
+
if (hbmp != NULL) {
|
204
|
-
|
234
|
+
int bmpoffset = bmfh.bfOffBits;
|
235
|
+
for (int i = 0; i < bmih->bV5Height * bmih->bV5Width; i++)((unsigned *)pv)[i] = filedata[bmpoffset+i];
|
205
236
|
|
206
|
-
|
237
|
+
SelectObject(hBuffer, hbmp);
|
238
|
+
GetObject(hBuffer, sizeof(BITMAP), &bm);
|
207
239
|
|
208
|
-
if (hbmp == NULL)MessageBox(hwnd, L"FAILED TO CDIBS", L"CAUTION", MB_OK);
|
209
|
-
hbuffer = CreateCompatibleDC(hdc);
|
210
|
-
|
240
|
+
status_b_open = TRUE;
|
211
|
-
GetObject(hbuffer, sizeof(BITMAP), &bm);
|
212
241
|
|
213
|
-
|
242
|
+
}
|
214
|
-
|
243
|
+
else {
|
244
|
+
MessageBox(hwnd, L"FAILED TO CDIBS", L"CAUTION", MB_OK);
|
245
|
+
status_b_open = FALSE;
|
246
|
+
}
|
215
247
|
|
216
|
-
case 2:
|
217
|
-
if (infosize == 12) {
|
218
|
-
|
248
|
+
/* End Image Rendering */
|
249
|
+
|
250
|
+
|
219
251
|
}
|
220
|
-
else if (infosize >= 16 && infosize <= 64) {
|
221
|
-
wsprintf(infoheadstatus, L"version.2 for OS/2");
|
222
|
-
}
|
223
|
-
else {
|
224
|
-
wsprintf(infoheadstatus, L"BROKEN BMP for OS/2");
|
225
|
-
}
|
226
252
|
break;
|
227
253
|
|
254
|
+
case 0x200:
|
255
|
+
if (infosize == 12) {
|
256
|
+
wsprintf(infoheadstatus, L"core for OS/2");
|
257
|
+
}
|
258
|
+
else if (infosize >= 16 && infosize <= 64) {
|
259
|
+
wsprintf(infoheadstatus, L"version.2 for OS/2");
|
260
|
+
}
|
261
|
+
else {
|
262
|
+
wsprintf(infoheadstatus, L"BROKEN BMP for OS/2");
|
263
|
+
}
|
264
|
+
status_b_open = FALSE;
|
265
|
+
break;
|
266
|
+
|
228
|
-
|
267
|
+
default:
|
229
|
-
|
268
|
+
wsprintf(infoheadstatus, L"NO INFOHEADER");
|
269
|
+
status_b_open = FALSE;
|
270
|
+
}
|
230
271
|
}
|
272
|
+
/* End Check File version and Fill defined structure */
|
231
273
|
|
232
|
-
|
274
|
+
/* Pass the string data to the parameter of parent scope */
|
275
|
+
wsprintf(pstring, L"PATH : %s\n\nFileType : \n%s\n\nInfoHeader : \n%s\n", filename, tbmp, getStatus);
|
276
|
+
/* Pass the string data to the parameter of parent scope */
|
233
277
|
|
234
|
-
|
235
|
-
|
236
|
-
wsprintf(pstring, L"PATH : %s\n\nFileSize : %10dbyte\nMagic Number : %c%c %s\n\nWrittenfileSize : %10dbyte\nBITMAPINFOHEADER : %s\n\0", filename, fsize, filedata[0], filedata[1], tbmp, wsize, infoheadstatus);
|
278
|
+
}
|
279
|
+
FileCheckEnd:
|
237
280
|
|
238
|
-
if ((bitmapinfoheaderver >= 1) && (bitmapinfoheaderver != 0)) {
|
239
|
-
wcscat_s(pstring, 1024 * 2, getStatus);
|
240
|
-
}
|
241
|
-
|
281
|
+
InvalidateRect(hwnd, NULL, TRUE);
|
242
|
-
UpdateWindow(hwnd);
|
243
|
-
}
|
244
282
|
break;
|
245
283
|
|
246
|
-
case WM_SIZE:
|
247
|
-
GetClientRect(hwnd, &rc);
|
248
|
-
src.right = rc.right - 5;
|
249
|
-
src.bottom = rc.bottom - 5;
|
250
|
-
break;
|
251
|
-
|
252
284
|
case WM_DESTROY:
|
253
|
-
CloseHandle(hFile);
|
254
|
-
free(filedata);
|
255
285
|
PostQuitMessage(0);
|
256
286
|
return 0;
|
257
287
|
}
|