Bonjour,
je travaille sur un projet avec des MFC.
J'aimerais savoir coment on change la couleur de fond d'une boîte de dialogue.
J'ai été voir dans la FAQ mais je ne comprends pas très bien ce qui est proposé.
Merci.
Atomikx
Version imprimable
Bonjour,
je travaille sur un projet avec des MFC.
J'aimerais savoir coment on change la couleur de fond d'une boîte de dialogue.
J'ai été voir dans la FAQ mais je ne comprends pas très bien ce qui est proposé.
Merci.
Atomikx
salut,
recuperes:
http://farscape.developpez.com/Samples/SampleSDI.zip
issu de mon tuto sur les MFC,il y a un exemple de boite de dialogue completement colorée.
:D
ca marche pas j'ai fais les modif dans le .h et j'ai ajouté la fonction
ensuite je l'appelle de cette façon :Code:
1
2
3
4
5
6
7
8
9
10 void SearchDialogBox::SetDialogBkColor(COLORREF clrCtlBk /*= RGB(192, 192, 192)*/, COLORREF clrCtlText /*= RGB(0, 0, 0) */) { //m_HbrClrCtlBk est à null dans le constructeur if(m_HbrClrCtlBk) ::DeleteObject(m_HbrClrCtlBk); m_HbrClrCtlBk = ::CreateSolidBrush(clrCtlBk); m_ClrCtlText = clrCtlText; m_crBackColor = clrCtlBk; if(m_hWnd) Invalidate(); }
tout tompile bien mais je n'ai toujours pas de couleur dans ma boîte de dialogue......Code:
1
2
3
4
5
6 BOOL searchDialogBox::OnInitDialog() { CDHtmlDialog::OnInitDialog(); SetDialogBkColor(RGB(187,221,255)); return TRUE; // retourne TRUE sauf si vous avez défini le focus sur un contrôle }
au secours :roll:
salut,
tu n'as pas bien regardé !
il te manque la fonction :
à générer avec class Wizard message :WM_CTLCOLORCode:
1
2 HBRUSH CDlgAdvanced::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
:D
ok, mais en fait ça me cause toujours un problème, en effet il ne met pas le fond de la fenêtre en bleu mais juste le fond ou il y a du texte.... je pense que je dois mal faire l'appel enfin je vois pas trop...
tu as une idée?
en fait je procède comme ceci.....mais je vois pas ou est l'erreur !!
Code:
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 searchDialogBox::searchDialogBox(CWnd* pParent) : CDHtmlDialog(searchDialogBox::IDD, searchDialogBox::IDH, pParent) { // HBRUSH m_HbrClrCtlBk=NULL ; // COLOR m_ClrCtlText= RGB(0, 0, 0) ; } searchDialogBox::~searchDialogBox() { if(m_HbrClrCtlBk) ::DeleteObject(m_HbrClrCtlBk); } void searchDialogBox::SetDialogBkColor(COLORREF clrCtlBk /*= RGB(192, 192, 192)*/, COLORREF clrCtlText /*= RGB(0, 0, 0) */) { //m_HbrClrCtlBk est à null dans le constructeur if(m_HbrClrCtlBk) ::DeleteObject(m_HbrClrCtlBk); m_HbrClrCtlBk = ::CreateSolidBrush(clrCtlBk); m_ClrCtlText = clrCtlText; m_crBackColor = clrCtlBk; if(m_hWnd) Invalidate(); } void searchDialogBox::DoDataExchange(CDataExchange* pDX) { CDHtmlDialog::DoDataExchange(pDX); } BOOL searchDialogBox::OnInitDialog() { CMainFrame* main_frm2 = (CMainFrame*) this->GetParent(); if (main_frm2->_bGoogle==true) { CComboBox* pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); pComboBox->AddString("le navigateur Google"); } SetDialogBkColor(RGB(187,221,255)); CDHtmlDialog::OnInitDialog(); return TRUE; // retourne TRUE sauf si vous avez défini le focus sur un contrôle } BEGIN_MESSAGE_MAP(searchDialogBox, CDHtmlDialog) ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1) ON_CBN_SELCHANGE(IDC_COMBO1, OnCbnSelchangeCombo1) ON_WM_CTLCOLOR() END_MESSAGE_MAP() BEGIN_DHTML_EVENT_MAP(searchDialogBox) DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK) DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel) END_DHTML_EVENT_MAP() HBRUSH searchDialogBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); /* CTLCOLOR_BTN button control CTLCOLOR_DLG dialog box CTLCOLOR_EDIT edit control CTLCOLOR_LISTBOX list box CTLCOLOR_MSGBOX message box CTLCOLOR_SCROLLBAR scroll bar CTLCOLOR_STATIC static text, frame, or rectangle */ // TODO: Change any attributes of the DC here // par exemple en fonction de nCtlColor voir doc. switch(nCtlColor) { // Intercepte le message pour es statics //case CTLCOLOR_DLG: deja traite dans OnEraseBkgnd case CTLCOLOR_STATIC : // Fixe la couleur d'ecriture du texte pDC->SetTextColor(m_ClrCtlText); // enventuellement suivant les cas // pDC->SetBkColor(m_ClrCtlBk); // Fixe le fond en transparent pour le texte // à ne pas faire pour un edit. pDC->SetBkMode(TRANSPARENT); // retourne le handle de la brush pour le fond si il existe. if(m_HbrClrCtlBk ) hbr = m_HbrClrCtlBk; break; } // TODO: Return a different brush if the default is not desired return hbr; }
exact :
deux solutions soit tu reactives dans OnCtlColor la ligne :
soit tu implementes comme dans mon exemple OnEraseBknd (WM_ERASEBKGND):Code:
1
2//case CTLCOLOR_DLG: deja traite dans OnEraseBkgnd
:DCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 //---------------------------------------------------- BOOL CDlgAdvanced::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default CBrush backBrush(m_crBackColor);//COLORREF CBrush *pOldBrush=pDC->SelectObject(&backBrush); CRect rect; pDC->GetClipBox(&rect); pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY); pDC->SelectObject(pOldBrush); return TRUE; }
OK, je choisi la première solution, mais dans le case, je fixe la couleur comme ceci :
ou comme ceciCode:pDC->SetBkColor(RGB(192,0,0));
dans le premier ca compile mais ca ne marche pas et dans le 2ème il connait pas m_ClrCtlBk.....Code:pDC->SetBkColor(m_ClrCtlBk);
:evil: :evil: :evil:
pour la 2nde solution j'ai mis
dans le message mapCode:ON_WM_ERASEBKGND()
ensuite j'ai mis la fonction :
dans searchDialogBox.cpp et sa déclaration dans le .hCode:
1
2
3
4
5
6
7
8
9
10
11
12 BOOL searchDialogBox::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default CBrush backBrush(m_crBackColor);//COLORREF CBrush *pOldBrush=pDC->SelectObject(&backBrush); CRect rect; pDC->GetClipBox(&rect); pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY); pDC->SelectObject(pOldBrush); return TRUE; }
mais ca ne marche pas....... :roll: :roll: :roll: :roll: :roll:
le message est a rajouter avec classwizard : a la main on se trompe.
:D
j'utilise jamais le classwizard, comment fait on pour ajouter un message avec ??
en quoi vc6 ? 2003 ?
voir post :
http://www.developpez.net/forums/vie...255609#2255609
sous vc6 : CTRL+W.
sinon pour le post precedent il faut etre plus précis que "ça ne marche pas" .
tu as mis un point d'arret dans la fonction ? tu y passes en trace debug?
m_crBackColor est initialisé ? etc..
:D