Bonjour
Je dois créer pour un projet des curseurs et les utiliser dans un programme. A terme, j’inclurai ces curseurs dans la partie ressource de ma DLL. Je commence par des tests.
1) Création du fichier .cur
J'utilise le curseur Yin de MSDN et la spec des fichiers .cur que j'ai trouvé ici et ici
Me basant sur ces spec, j'ai écrit un programme créant le fichier yin.cur :
2) test du fichier .cur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdlib.h> #include <string.h> #include <windows.h> static int _write() { typedef struct { BYTE bWidth; // Width, in pixels, of the image BYTE bHeight; // Height, in pixels, of the image BYTE bColorCount; // Number of colors in image (0 if >=8bpp) BYTE bReserved; // Reserved ( must be 0) WORD wPlanes; // Color Planes WORD wBitCount; // Bits per pixel DWORD dwBytesInRes; // How many bytes in this resource? DWORD dwImageOffset; // Where in the file is this image? } ICONDIRENTRY; typedef struct { WORD idReserved; // Reserved (must be 0) WORD idType; // Resource Type (1 for icons) WORD idCount; // How many images? } ICONDIR; HANDLE file; ICONDIR header; ICONDIRENTRY entries[2]; file = CreateFile("yin.cur", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (file == INVALID_HANDLE_VALUE) { return 0; } int hotspot_x = 19; int hotspot_y = 2; // header header.idReserved = 0; header.idType = 2; header.idCount = 2; entries[0].bWidth = 32; entries[0].bHeight = 32; entries[0].bColorCount = 0; entries[0].bReserved = 0; entries[0].wPlanes = hotspot_x; entries[0].wBitCount = hotspot_y; entries[0].dwBytesInRes = 32 * 4; entries[0].dwImageOffset = 38; entries[1].bWidth = 32; entries[1].bHeight = 32; entries[1].bColorCount = 0; entries[1].bReserved = 0; entries[1].wPlanes = hotspot_x; entries[1].wBitCount = hotspot_y; entries[1].dwBytesInRes = 32 * 4; entries[1].dwImageOffset = 166; DWORD size; if (!WriteFile(file, &header, sizeof(header), &size, NULL)) { goto close_file; } if (size != sizeof(header)) { goto close_file; } if (!WriteFile(file, entries, sizeof(entries), &size, NULL)) { goto close_file; } if (size != sizeof(entries)) { goto close_file; } BYTE ANDmaskCursor[] = { 0xFF, 0xFC, 0x3F, 0xFF, // line 1 0xFF, 0xC0, 0x1F, 0xFF, // line 2 0xFF, 0x00, 0x3F, 0xFF, // line 3 0xFE, 0x00, 0xFF, 0xFF, // line 4 0xF7, 0x01, 0xFF, 0xFF, // line 5 0xF0, 0x03, 0xFF, 0xFF, // line 6 0xF0, 0x03, 0xFF, 0xFF, // line 7 0xE0, 0x07, 0xFF, 0xFF, // line 8 0xC0, 0x07, 0xFF, 0xFF, // line 9 0xC0, 0x0F, 0xFF, 0xFF, // line 10 0x80, 0x0F, 0xFF, 0xFF, // line 11 0x80, 0x0F, 0xFF, 0xFF, // line 12 0x80, 0x07, 0xFF, 0xFF, // line 13 0x00, 0x07, 0xFF, 0xFF, // line 14 0x00, 0x03, 0xFF, 0xFF, // line 15 0x00, 0x00, 0xFF, 0xFF, // line 16 0x00, 0x00, 0x7F, 0xFF, // line 17 0x00, 0x00, 0x1F, 0xFF, // line 18 0x00, 0x00, 0x0F, 0xFF, // line 19 0x80, 0x00, 0x0F, 0xFF, // line 20 0x80, 0x00, 0x07, 0xFF, // line 21 0x80, 0x00, 0x07, 0xFF, // line 22 0xC0, 0x00, 0x07, 0xFF, // line 23 0xC0, 0x00, 0x0F, 0xFF, // line 24 0xE0, 0x00, 0x0F, 0xFF, // line 25 0xF0, 0x00, 0x1F, 0xFF, // line 26 0xF0, 0x00, 0x1F, 0xFF, // line 27 0xF8, 0x00, 0x3F, 0xFF, // line 28 0xFE, 0x00, 0x7F, 0xFF, // line 29 0xFF, 0x00, 0xFF, 0xFF, // line 30 0xFF, 0xC3, 0xFF, 0xFF, // line 31 0xFF, 0xFF, 0xFF, 0xFF // line 32 }; // Yin-shaped cursor XOR mask BYTE XORmaskCursor[] = { 0x00, 0x00, 0x00, 0x00, // line 1 0x00, 0x03, 0xC0, 0x00, // line 2 0x00, 0x3F, 0x00, 0x00, // line 3 0x00, 0xFE, 0x00, 0x00, // line 4 0x0E, 0xFC, 0x00, 0x00, // line 5 0x07, 0xF8, 0x00, 0x00, // line 6 0x07, 0xF8, 0x00, 0x00, // line 7 0x0F, 0xF0, 0x00, 0x00, // line 8 0x1F, 0xF0, 0x00, 0x00, // line 9 0x1F, 0xE0, 0x00, 0x00, // line 10 0x3F, 0xE0, 0x00, 0x00, // line 11 0x3F, 0xE0, 0x00, 0x00, // line 12 0x3F, 0xF0, 0x00, 0x00, // line 13 0x7F, 0xF0, 0x00, 0x00, // line 14 0x7F, 0xF8, 0x00, 0x00, // line 15 0x7F, 0xFC, 0x00, 0x00, // line 16 0x7F, 0xFF, 0x00, 0x00, // line 17 0x7F, 0xFF, 0x80, 0x00, // line 18 0x7F, 0xFF, 0xE0, 0x00, // line 19 0x3F, 0xFF, 0xE0, 0x00, // line 20 0x3F, 0xC7, 0xF0, 0x00, // line 21 0x3F, 0x83, 0xF0, 0x00, // line 22 0x1F, 0x83, 0xF0, 0x00, // line 23 0x1F, 0x83, 0xE0, 0x00, // line 24 0x0F, 0xC7, 0xE0, 0x00, // line 25 0x07, 0xFF, 0xC0, 0x00, // line 26 0x07, 0xFF, 0xC0, 0x00, // line 27 0x01, 0xFF, 0x80, 0x00, // line 28 0x00, 0xFF, 0x00, 0x00, // line 29 0x00, 0x3C, 0x00, 0x00, // line 30 0x00, 0x00, 0x00, 0x00, // line 31 0x00, 0x00, 0x00, 0x00 // line 32 }; if (!WriteFile(file, XORmaskCursor, 32 * 4, &size, NULL)) { goto close_file; } if (size != 32 * 4) { goto close_file; } // AND mask then if (!WriteFile(file, ANDmaskCursor, 32 * 4, &size, NULL)) { goto close_file; } if (size != 32 * 4) { goto close_file; } CloseHandle(file); return 1; close_file: CloseHandle(file); return 0; } int main() { _write(); return 0; }
J'utilise la fonction LoadCursorFromFile() pour charger le fichier yin.cur. Le programme suivant crée une fenêtre. En appuyant sur la touche 'b', la fenêtre devient sans bord et j'utilise le curseur de "yin.cur". En re-appuyant sur 'b', la fenêtre redevient avec bord et j'utilise le curseur HELP.
Le code :
les lignes 296 et 305 se chargent de modifier le curseur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 #define _WIN32_WINNT 0x0502 #include <stdlib.h> #include <stdio.h> #include <windows.h> typedef struct BITMAPINFO_GDI BITMAPINFO_GDI; struct BITMAPINFO_GDI { BITMAPINFOHEADER bih; DWORD masks[3]; }; typedef struct { HWND window; HDC dc; HBITMAP bitmap; void *data; unsigned int borderless : 1; } Window; HINSTANCE instance; static LRESULT CALLBACK _window_procedure(HWND window, UINT message, WPARAM window_param, LPARAM data_param); static int layer_init(void) { WNDCLASS wc; instance = GetModuleHandle(NULL); if (!instance) return 0; memset (&wc, 0, sizeof (WNDCLASS)); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = _window_procedure; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instance; wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE); wc.lpszMenuName = NULL; wc.lpszClassName = "layer"; if(!RegisterClass(&wc)) { FreeLibrary(instance); return 0; } return 1; } static void layer_shutdown(void) { FreeLibrary(instance); } static Window * layer_window_new(int x, int y, int width, int height) { RECT rect; Window *win; DWORD style; win = (Window *)calloc(1, sizeof(Window)); if (!win) return NULL; style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX; rect.left = 0; rect.top = 0; rect.right = width; rect.bottom = height; if (!AdjustWindowRect(&rect, style, FALSE)) goto free_window; win->window = CreateWindowEx(0, "layer", "Test", style, x, y, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, instance, NULL); if (!win->window) goto free_window; SetLastError(0); if (!SetWindowLongPtr(win->window, GWLP_USERDATA, (LONG_PTR)win) && (GetLastError() != 0)) { printf("SetWindowLongPtr() failed\n"); goto destroy_window; } win->dc = GetDC(win->window); if (!win->dc) goto destroy_window; win->borderless = 0; return win; destroy_window: DestroyWindow(win->window); free_window: free(win); return NULL; } static void layer_window_free(Window *window) { ReleaseDC(window->window, window->dc); DestroyWindow(window->window); free(window); } static void layer_window_show(Window *window) { ShowWindow(window->window, SW_SHOWNORMAL); UpdateWindow(window->window); } void layer_window_bitmap_new(Window *win) { BITMAPINFO_GDI bitmap_info; RECT r; int size; if (!GetClientRect(win->window, &r)) { printf("GetClientRect() failed\n"); return; } size = 4 * r.right * r.bottom; ZeroMemory(&bitmap_info, sizeof(BITMAPINFOHEADER)); bitmap_info.bih.biSize = sizeof(BITMAPINFOHEADER); bitmap_info.bih.biWidth = r.right; bitmap_info.bih.biHeight = -r.bottom; bitmap_info.bih.biPlanes = 1; bitmap_info.bih.biSizeImage = size; bitmap_info.bih.biBitCount = 32; bitmap_info.bih.biCompression = BI_BITFIELDS; bitmap_info.masks[0] = 0x00ff0000; bitmap_info.masks[1] = 0x0000ff00; bitmap_info.masks[2] = 0x000000ff; win->bitmap = CreateDIBSection(win->dc, (const BITMAPINFO *)&bitmap_info, DIB_RGB_COLORS, (void **)(&win->data), NULL, 0); } static void layer_window_update(Window *win) { DWORD style; RECT rect_win; style = GetWindowLong(win->window, GWL_EXSTYLE); style &= ~WS_EX_LAYERED; SetLastError(0); if (!SetWindowLongPtr(win->window, GWL_EXSTYLE, style) && (GetLastError() != 0)) { printf("SetWindowLongPtr() failed\n"); return; } style = GetWindowLong(win->window, GWL_STYLE); if (win->borderless) { if (!GetClientRect(win->window, &rect_win)) { printf("GetClientRect() failed\n"); return; } SetLastError(0); if (!SetWindowLongPtr(win->window, GWL_STYLE, style & ~(WS_CAPTION | WS_THICKFRAME)) && (GetLastError() != 0)) { printf("SetWindowLongPtr() failed\n"); return; } } else { if (!GetWindowRect(win->window, &rect_win)) { printf("GetWindowRect() failed\n"); return; } style |= WS_CAPTION | WS_THICKFRAME; if (!AdjustWindowRect (&rect_win, style, FALSE)) { printf("AdjustWindowRect() failed\n"); return; } SetLastError(0); if (!SetWindowLongPtr(win->window, GWL_STYLE, style) && (GetLastError() != 0)) { printf("SetWindowLongPtr() failed\n"); return; } } if (!SetWindowPos(win->window, HWND_TOPMOST, rect_win.left, rect_win.top, rect_win.right - rect_win.left, rect_win.bottom - rect_win.top, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_FRAMECHANGED)) { printf("SetWindowPos() failed\n"); return; } } void layer_paint(Window *win) { RECT r; HDC dc; int *tmp; int i; int j; if (!win->data) return; if (!GetClientRect(win->window, &r)) return; tmp = win->data; /* loop over height */ for (j = 0; j < r.bottom; j++) /* loop over width */ for (i = 0; i < r.right; i++, tmp++) { if ((i > ((r.right * 3) / 4)) && (j > ((r.bottom * 3) / 4))) *tmp = 0x000000ff; else *tmp = 0xffff0000; } dc = CreateCompatibleDC(win->dc); if (!dc) return; SelectObject(dc, win->bitmap); BitBlt(win->dc, 0, 0, r.right, r.bottom, dc, 0, 0, SRCCOPY); DeleteDC(dc); } static LRESULT CALLBACK _window_procedure(HWND window, UINT message, WPARAM window_param, LPARAM data_param) { Window *win; win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA); switch (message) { case WM_KEYUP: { if (window_param == 'B') { win->borderless = !win->borderless; layer_window_update(win); if (win->borderless) { HCURSOR cursor = LoadCursorFromFile("yin.cur"); SetLastError(0); printf(" ** %p %ld\n", cursor, GetLastError()); SetClassLongPtr(win->window, GCLP_HCURSOR, (LONG_PTR)cursor); /* SetCursor(cursor); */ } else { HCURSOR cursor = LoadCursor(NULL, IDC_HELP); SetClassLongPtr(win->window, GCLP_HCURSOR, (LONG_PTR)cursor); /* SetCursor(cursor); */ } } if (window_param == 'Q') { PostQuitMessage(0); } return 0; } /* window notifications */ case WM_CLOSE: { layer_window_free(win); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } case WM_SIZE: { printf("sized\n"); layer_window_bitmap_new(win); return 0; } /* GDI notifications */ case WM_PAINT: { RECT rect; printf("paint\n"); if (GetUpdateRect(window, &rect, FALSE)) { PAINTSTRUCT ps; HDC hdc; hdc = BeginPaint(window, &ps); layer_paint(win); EndPaint(window, &ps); } return 0; } default: return DefWindowProc(window, message, window_param, data_param); } } int main() { Window *win; if (!layer_init()) return EXIT_FAILURE; win = layer_window_new(10, 10, 200, 200); if (!win) { printf("zut\n"); layer_shutdown(); return EXIT_FAILURE; } layer_window_show(win); /* msg loop */ while(1) { MSG msg; BOOL ret; ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); if (ret) { do { if (msg.message == WM_QUIT) goto beach; TranslateMessage(&msg); DispatchMessageW(&msg); } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); } } beach: layer_window_free(win); layer_shutdown(); return EXIT_SUCCESS; }
Problème : la fonction LoadCursorFromFile() échoue et GetLastError() renvoie 0, donc pas d'erreur.
Question : où se trouve l'erreur ? Dans la création du fichier .cur, ou bien son utilisation avec LoadCursorFromFile() ?
Merci
Partager