Bonjour
Je voudrais savoir comment changer, via une boite de dialogue commune, la couleur de dessins et de textes créés avec WM_PAINT dans une fenêtre.
Merci d'avance
Bonjour
Je voudrais savoir comment changer, via une boite de dialogue commune, la couleur de dessins et de textes créés avec WM_PAINT dans une fenêtre.
Merci d'avance
Salut,
Si j'ai bien compris ce que tu veux, tu dois mémoriser une variable de type COLORREF puis l'utiliser lorsque tu traites le message WM_PAINT avec des fonctions comme SetTextColor(), SetBkColor(), SetDCPenColor(), etc.
Salut
Oui, c'est bien ça. Mais j'ai en plus un souci avec SetDCPenColor(): j'ai à la compilation "[Link error] undefined reference to 'SetDCPenColor'".
SetDCPenColor() est une API disponible à partir de Windows 2000.
Assures-toi que tu lies bien la librairie GDI32.LIB et que tu as ajouté #define _WIN32_WINNT 0x0500 avant l'inclusion de <windows.h>.
salut
En rajoutant #define _WIN32_WINNT 0x0500, SetDCPenColor() ne me pose plus de problèmes. Par contre, la couleur n'est changé que si la fenêtre est redessinée. J'ai un autre problème, cette fois-ci pour changer la fonte d'un texte, ça ne marche pas du tout.
salut
J'ai réussi à tégler le problème du changement de couleur. Il suffisait de rajouter la ligne InvalidateRect(hwnd, NULL, TRUE). Par contre, pour changer la fonte, j'ai toujours des soucis.
visiblement, avec InvalidateRect(), ça ne marche pas.
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 else if (LOWORD(wParam) == IDG_FONT) { CHOOSEFONT cg; ZeroMemory(&cg, sizeof(CHOOSEFONT)); cg.lStructSize = sizeof (CHOOSEFONT); cg.hwndOwner = hGraph; cg.lpLogFont = &lfg; cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; if (ChooseFont(&cg)) { DeleteObject(NewFont); NewFont = CreateFontIndirect(&lfg); SendMessage(hwnd,WM_PAINT,(UINT)NewFont,TRUE); InvalidateRect(hwnd,NULL,TRUE); } }
Pour la font as-tu essayé SetTextColor() au lieu de SetDCPenColor() ? Je crois que la première fonction est plus adaptée...
salut
Pour la font, j'utilise bien SetTextColor et ça change bien la couleur. Mais pour changer la police, le style ou la taille, ça ne marche pas.
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 PAINTSTRUCT pg; HDC hdg; static BOOL GraphNotChg = TRUE; static COLORREF GraphColor; LOGFONT lfg; HPEN hpNew, hpOld; HFONT NewFont, OldFont; int m = 0; char *szBuffer1; szBuffer1 = (char*)malloc(256*sizeof(char)); ZeroMemory(&lfg, sizeof(LOGFONT)); lstrcpy(lfg.lfFaceName, "Times New Roman"); lfg.lfHeight = 20; lfg.lfItalic = TRUE; lfg.lfUnderline = FALSE; lfg.lfWeight = FW_NORMAL; NewFont = CreateFontIndirect(&lfg); switch(uMsg) { case WM_CREATE: GraphColor = RGB(0,0,0); case WM_PAINT: { char *buffer; buffer = (char*)malloc(256*sizeof(char)); hdg = BeginPaint(hGraph, &pg); SetBkMode(hdg, TRANSPARENT); OldFont = SelectObject(hdg,NewFont); SetTextColor(hdg, GraphColor); sprintf(buffer, "p1(x) = %s", afficher(&p[0], szBuffer1)); TextOut(hdg, 5, 5, buffer, lstrlen(buffer)); free(buffer); SelectObject(hdg,OldFont); EndPaint(hGraph, &pg); return 0L; } case WM_COMMAND: { if (LOWORD(wParam) == IDG_FONT) { CHOOSEFONT cg; ZeroMemory(&cg, sizeof(CHOOSEFONT)); cg.lStructSize = sizeof (CHOOSEFONT); cg.hwndOwner = hGraph; cg.lpLogFont = &lfg; cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; if (ChooseFont(&cg)) { DeleteObject(NewFont); NewFont = CreateFontIndirect(&lfg); SendMessage(hGraph,WM_PAINT,(UINT)NewFont,TRUE); InvalidateRect(hGraph, NULL, TRUE); } } else if (LOWORD(wParam) == IDG_TEXTCOLOR) { CHOOSECOLOR ccg; static COLORREF acrCustClrg[16]; ZeroMemory(&ccg, sizeof(CHOOSECOLOR)); ccg.lStructSize = sizeof(CHOOSECOLOR); ccg.hwndOwner = hGraph; ccg.lpCustColors = NULL; ccg.lpCustColors = (LPDWORD) acrCustClrg; ccg.rgbResult = GraphColor; ccg.Flags = CC_FULLOPEN | CC_RGBINIT; if (ChooseColor(&ccg)==TRUE) { GraphColor = ccg.rgbResult; PostMessage(hGraph, WM_PAINT, TRUE, 0); InvalidateRect(hGraph, NULL, TRUE); } } else if (HIWORD(wParam) == EN_CHANGE) GraphNotChg = FALSE; return 0; } case WM_DESTROY: { free(szBuffer1); free(p); DeleteObject(NewFont); ShowWindow(hGraph, SW_HIDE); return 0; }
Il y a plusieurs choses bizarres dans ton code:
- tu envoies le message WM_PAINT avec WPARAM renseigné, mais dans le traitement du dit message tu ne récupères ce paramètre
- tu crées une font à chaque fois que tu reçois un message (le code avant le switch(uMsg) ), de même pour le malloc(), c'est inutile et tu risques d'épuiser tes ressources.
- il ne vaut mieux pas demander le dessin de la fenêtre par SendMessage(Hwnd,WM_PAINT,0,0) mais plutot avec UpdateWindow(Hwnd)
Je te propose:
- de mettre HFONT NewFont, OldFont en static et de créer ta font de départ dans WM_CREATE
- d'appeler SelectObject(hdc,NewFont) après CreateFontIndirect() dans WM_COMMAND
- d'utiliser UpdateWindow(Hwnd)
En gros ça pourrait donner le code suivant:
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 static BOOL GraphNotChg = TRUE; static COLORREF GraphColor; static HFONT NewFont, OldFont; static char *szBuffer1; PAINTSTRUCT pg; HDC hdg; LOGFONT lfg; HPEN hpNew, hpOld; int m = 0; switch(uMsg) { case WM_CREATE: szBuffer1 = (char*)malloc(256*sizeof(char)); GraphColor = RGB(0,0,0); ZeroMemory(&lfg, sizeof(LOGFONT)); lstrcpy(lfg.lfFaceName, "Times New Roman"); lfg.lfHeight = 20; lfg.lfItalic = TRUE; lfg.lfUnderline = FALSE; lfg.lfWeight = FW_NORMAL; hdg = GetDC(hwnd); NewFont = CreateFontIndirect(&lfg); OldFont = SelectObject(hdg,NewFont); ReleaseDC(hwnd, hdg); break; case WM_PAINT: { char *buffer; buffer = (char*)malloc(256*sizeof(char)); hdg = BeginPaint(hGraph, &pg); SetBkMode(hdg, TRANSPARENT); SetTextColor(hdg, GraphColor); sprintf(buffer, "p1(x) = %s", afficher(&p[0], szBuffer1)); TextOut(hdg, 5, 5, buffer, lstrlen(buffer)); free(buffer); EndPaint(hGraph, &pg); return 0L; } case WM_COMMAND: { if (LOWORD(wParam) == IDG_FONT) { CHOOSEFONT cg; ZeroMemory(&cg, sizeof(CHOOSEFONT)); cg.lStructSize = sizeof (CHOOSEFONT); cg.hwndOwner = hGraph; cg.lpLogFont = &lfg; cg.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; if (ChooseFont(&cg)) { hdg = GetDC(hwnd); SelectObject(hdg,OldFont); DeleteObject(NewFont); NewFont = CreateFontIndirect(&lfg); OldFont = SelectObject(hdg,NewFont); ReleaseDC(hwnd, hdg); InvalidateRect(hGraph, NULL, TRUE); UpdateWindow(Hwnd); } } else if (LOWORD(wParam) == IDG_TEXTCOLOR) { CHOOSECOLOR ccg; static COLORREF acrCustClrg[16]; ZeroMemory(&ccg, sizeof(CHOOSECOLOR)); ccg.lStructSize = sizeof(CHOOSECOLOR); ccg.hwndOwner = hGraph; ccg.lpCustColors = NULL; ccg.lpCustColors = (LPDWORD) acrCustClrg; ccg.rgbResult = GraphColor; ccg.Flags = CC_FULLOPEN | CC_RGBINIT; if (ChooseColor(&ccg)==TRUE) { GraphColor = ccg.rgbResult; InvalidateRect(hGraph, NULL, TRUE); UpdateWindow(Hwnd); } } else if (HIWORD(wParam) == EN_CHANGE) GraphNotChg = FALSE; return 0; } case WM_DESTROY: { free(szBuffer1); free(p); SelectObject(hdg,OldFont); DeleteObject(NewFont); ShowWindow(hGraph, SW_HIDE); return 0; } }
Ca marche très bien.
J'ai juste rajouté "GetObject(NewFont, sizeof(LOGFONT), &lfg);" après "ZeroMemory(&cg, sizeof(CHOOSEFONT));".
Je te remercie beaucoup pour ton aide et ta patience.
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager