Wm_ctlcolor message.

Source: Internet
Author: User

ReferenceArticle: Bai Qiao Original: Beautification interface of VC

Before each control is drawn, a wm_ctlcolor notification message is sent to its parent window. In the message processing function, you can set the foreground color, background color, and font of the control to display the text. The message processing function also requires that a paint brush handle be returned to erase the customer zone before the control is drawn.

The message processing function of wm_ctlcolor ing is afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor ).
CommonCodeIs:

[CPP] View plaincopyprint?
    1. PDC-> settextcolor (RGB (255, 0, 0 ));// Set the text foreground
    2. PDC-> setbkcolor (RGB (255,255,255 ));// Set the text background color
    3. PDC-> setbkmode (transparent );// Transparent or opaque
    4. PDC-> SelectObject (...)
 
PDC-> settextcolor (RGB (255, 0, 0); // sets the text foreground color PDC-> setbkcolor (RGB (255,255,255 )); // set the text background color PDC-> setbkmode (transparent); // transparent or opaquepdc-> SelectObject (...)

A simple example is as follows:

[CPP] View plaincopyprint?
  1. //
  2. // M_font1 and m_font2 are members of the ctestdlg type cfont
  3. //
  4. BoolCtestdlg: oninitdialog ()
  5. {
  6. ......
  7. // Todo: add extra initialization here
  8. M_font1.createpointfont (120, text ("Impact"));
  9. M_font2.createpointfont (120, text ("Arial"));
  10. ......
  11. }
  12. HbrushCtestdlg: onctlcolor (CDC * PDC, cwnd * pwnd,UintNctlcolor)
  13. {
  14. HbrushHBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
  15. // Todo: change any attributes of the DC here
  16. If(Nctlcolor = ctlcolor_static)
  17. {
  18. Switch(Pwnd-> getdlgctrlid ())
  19. {
  20. CaseIdc_static_1:
  21. PDC-> settextcolor (RGB (255, 0, 0 ));
  22. PDC-> setbkcolor (RGB (255,255,255 ));
  23. PDC-> setbkmode (transparent );
  24. PDC-> SelectObject (& m_font1 );
  25. Return(Hbrush): Getstockobject (black_brush );
  26. Break;
  27. CaseIdc_static_2:
  28. PDC-> settextcolor (RGB (255,255, 0 ));
  29. PDC-> setbkcolor (RGB (255,255,255 ));
  30. PDC-> SelectObject (& m_font2 );
  31. Return(Hbrush): Getstockobject (black_brush );
  32. Break;
  33. Default:
  34. Break;
  35. }
  36. }
  37. // Todo: return a different brush if the default is not desired
  38. ReturnHBr;
  39. }
//// M_font1 and m_font2 are members of ctestdlg. The type is cfont // bool ctestdlg: oninitdialog (){...... // todo: add extra initialization here m_font1.createpointfont (120, text ("impact"); m_font2.createpointfont (120, text ("Arial "));......} hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any attributes of the DC hereif (nctlcolor = ctlcolor_static) {Switch (pwnd-> getdlgctrlid () {Case idc_static_1: PDC-> settextcolor (RGB (255, 0, 0); PDC-> setbkcolor (RGB (255,255,255); PDC-> setbkmode (transparent); PDC-> SelectObject (& m_font1); Return (hbrush ):: getstockobject (black_brush); break; Case idc_static_2: PDC-> settextcolor (RGB (255,255, 0); PDC-> setbkcolor (RGB (255,255,255 )); PDC-> SelectObject (& m_font2); Return (hbrush): getstockobject (black_brush); break; default: break; }}// todo: return a different brush if the default is not desiredreturn HBr ;}

Description 1

 
The nctlcolor in onctlcolor can be: ctlcolor_btn button control ctlcolor_dlg dialog box ctlcolor_edit Edit Control ctlcolor_listbox list-box control comment message box ctlcolor_scrollbar scroll-bar control ctlcolor_static Static Control

It can be seen that wm_ctlcolor can be applied to the button control cbutton, edit box control cedit, ListBox control, static control, and scroll bar control, or to the dialog box itself.
Note: in the previous section, wm_ctlcolor is the notification message, that is, the subcontrol is sent to the parent window. However, for the dialog box itself, it can receive the wm_ctlcolor message that nctlcolor is ctlcolor_dlg, which is sent to itself, obviously, this is not a notification.

Example:

[CPP] View plaincopyprint?
  1. HbrushCtestdlg: onctlcolor (CDC * PDC, cwnd * pwnd,UintNctlcolor)
  2. {
  3. HbrushHBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
  4. // Todo: change any attributes of the DC here
  5. If(Nctlcolor = ctlcolor_dlg)
  6. Return(Hbrush): Getstockobject (black_brush );
  7. Else
  8. PDC-> settextcolor (RGB (255, 0, 0 ));
  9. // Todo: return a different brush if the default is not desired
  10. ReturnHBr;
  11. }
Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any attributes of the DC hereif (nctlcolor = ctlcolor_dlg) Return (hbrush): getstockobject (black_brush); elsepdc-> settextcolor (RGB (255, 0, 0 )); // todo: return a different brush if the default is not desiredreturn HBr ;}

 
Description 2
The processing in the onctlcolor message does not work for Pushbutton. It can be seen from the example in description 1, while it is OK for checkbox and radiobutton. Attached the explanation given by csdn: buttons with the bs_pushbutton, bs_defpushbutton, or bs_pushlike styles do not use the returned brush. buttons with these styles are always drawn with the default system colors. drawing push buttons requires several different brushes-face, highlight, and shadow-but the wm_ctlcolorbtn message allows only one brush to be returned. to provide a custom appearance for push buttons, use an o Wner-drawn button. therefore, Pushbutton can only be set to owner-drawn button, and then the Response Control notifies the message wm_drawitem for processing. The response function prototype of the message is afx_msg void ondrawitem (INT nidctl, lpdrawitemstruct ).
 
Description 3: Application of the ComboBox control
The ComboBox control contains an editbox. When you click expand, A ListBox is displayed to list all the items. Note that the parent window of The ListBox control is not the ComboBox, but the parent window of the ComboBox. Therefore, if you want to set the font color of the text displayed in a ComboBox to red (editbox and text in the drop-down ListBox) in the code, assume that the ComboBox ID is idc_combo, the following code does not work.
[CPP] View plaincopyprint?
    1. If(Pwnd-> getdlgctrlid () = idc_combo)
    2. {
    3. PDC-> settextcolor (RGB (255, 0, 0 ));
    4. }
 
If (pwnd-> getdlgctrlid () = idc_combo) {PDC-> settextcolor (RGB (255, 0, 0 ));}
 
The text color in editbox and the drop-down ListBox is not changed.
 
The following dialog box contains two ComboBox controls: m_combo1 and m_combo2. The text color displayed in m_combo1 is red, while m_combo2 is default. The Code is as follows:
[CPP] View plaincopyprint?
  1. HbrushCtestdlg: onctlcolor (CDC * PDC, cwnd * pwnd,UintNctlcolor)
  2. {
  3. HbrushHBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
  4. // Todo: change any attributes of the DC here
  5. // Editbox for m_combo1 (the parent window of the editbox is m_combo1)
  6. If(Nctlcolor = ctlcolor_edit
  7. & Pwnd-> getparent ()-> getdlgctrlid () = m_combo1.getdlgctrlid ())
  8. {
  9. PDC-> settextcolor (RGB (255, 0, 0 ));
  10. }
  11. // ListBox for m_combo1 drop-down
  12. If(Nctlcolor = ctlcolor_listbox
  13. & M_combo1.getparent ()-> getdlgctrlid () = pwnd-> getparent ()-> getdlgctrlid ())
  14. {
  15. // Obtain the screen coordinate ranges of ListBox and m_combo1
  16. Rect rectlistbox;
  17. Rect rectcombobox;
  18. Pwnd-> getwindowrect (& rectlistbox );
  19. M_combo1.getwindowrect (& rectcombobox );
  20. // If The ListBox is under m_combo1, click the drop-down ListBox generated by m_combo1.
  21. If(Rectlistbox. Left = rectcombobox. Left
  22. & Rectlistbox. Top = rectcombobox. Bottom)
  23. {
  24. PDC-> settextcolor (RGB (255, 0, 0 ));
  25. }
  26. }
  27. // Todo: return a different brush if the default is not desired
  28. ReturnHBr;
  29. }
Hbrush ctestdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any attributes of the DC here // For the editbox m_combo1 (the parent window of the editbox is m_combo1) if (nctlcolor = ctlcolor_edit & pwnd-> getparent () -> getdlgctrlid () = m_combo1.getdlgctrlid () {PDC-> settextcolor (RGB (255, 0, 0 ));} // listboxif (nctlcolor = ctlcolor_listbox & m_combo1.getparent ()-> getdlgctrlid () = pwnd-> getparent ()-> getdlgctrlid ()) {// obtain the screen coordinate ranges of ListBox and m_combo1: rect rectlistbox; rect rectcombobox; pwnd-> getwindowrect (& rectlistbox); m_combo1.getwindowrect (& rectcombobox ); // If The ListBox is under m_combo1, click the drop-down listboxif (rectlistbox. left = rectcombobox. left & rectlistbox. top = rectcombobox. bottom) {PDC-> settextcolor (RGB (255, 0, 0) ;}}// todo: return a different brush if the default is not desiredreturn HBr ;}

The effect is as follows:

The simpler method is to use the Wizard to add the cmycombobox: ccombobox class for the MFC class, and then add the response function for the wm_ctlcolor message. (Note: Both editbox and ListBox in ComboBox send the wm_ctlcolor message to the ComboBox window. If the corresponding processing function is not found in the message ing table corresponding to ComboBox, send the wm_ctlcolor message to the parent window of ccombobox. For details, refer to the wm_notify message process instance analysis)
[CPP] View plaincopyprint?
  1. Begin_message_map (cmycombobox, ccombobox)
  2. On_wm_ctlcolor ()
  3. End_message_map ()
  4. HbrushCmycombobox: onctlcolor (CDC * PDC, cwnd * pwnd,UintNctlcolor)
  5. {
  6. HbrushHBr = ccombobox: onctlcolor (PDC, pwnd, nctlcolor );
  7. // Todo: change any attributes of the DC here
  8. PDC-> settextcolor (RGB (255,255, 0 ));
  9. // Todo: return a different brush if the default is not desired
  10. ReturnHBr;
  11. }
 
Hour (hour, ccombobox) hour () hbrush cmycombobox: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = ccombobox: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any attributes of the DC herepdc-> settextcolor (RGB (255,255, 0); // todo: return a different brush if the default is not desiredreturn HBr ;}
 
PDC-> settextcolor (RGB (255,255, 0); a single sentence of code can implement the above functions.
 
Note 4: wm_ctlcolor is a notification message, so it can be processed in the reflected message.On_wm_ctlcolor_reflect ()

From: http://blog.csdn.net/wangyao1052/article/details/8070393

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.