How to change the background color of the MFC button

Source: Internet
Author: User

Today, I plan to color my project. However, a problem occurred while coloring the buttons. The methods used for searching on the Internet are basically useless. Just like the truth, the various methods of OnCtrlColor () and OnEraseBkground () seem amazing, but they do not work.

The really useful way is to customize the button to realize the button color. However, this method is a little complicated. I am so lazy and don't want to rewrite a class for such a small function or use a third-party button class, so I have to find another way.

Fortunately, I found another tutorial on codeproject. You can use CMFCButton to implement this function.

Next, we will briefly introduce the usage of CMFCButton. Create a dialog box-based project MFCButtonTest. In the dialog box editing interface, drag a button control and change the resource ID to IDC_BUTTON_TEST.

Next, add the following code in CMFCButtonTestDlg: OnInitDialog:

 
 
  1. CMFCButton * button = new CMFCButton; 
  2. button->Create(_T("Test Button"), 
  3. WS_VISIBLE, 
  4. CRect(5, 5, 80, 50), 
  5. this, 
  6. IDC_BUTTON_TEST); 

Note !! This behavior is wrong, and memory leakage occurs. It is only for example writing. This writing method is not allowed in real projects.

Okay. Run it. Two buttons are displayed on the screen.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A34235b-0.png "border =" 0 "alt =" "/>

But don't worry, TestButton is Button1, Button1 is TestButton, they share the same resource number. It can also be said that TestButton is actually a "shadow separation" of Button1, which is invisible in the dialog box editing interface. The solution is simple. Set the Visible attribute of Button1 to False in the dialog box editing interface.

Tips: in this way, you can create multiple CMFCButton and use the same ID. Then they can be scattered all over the world and run the same piece of code. This is really handsome!

Run again.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A3426300-1.png "border =" 0 "alt =" "/>

Okay. Next, let's go to the main scene. CMFCButton is highly customizable, including the background color, image, button font, font color, font highlight color, and mouse pointer... And so on.

Next let's take a test. 650) this. width = 650; "src ="/neweditor/editor/images/smiley/17.gif" alt = ""/>

Or CMFCButtonTestDlg: OnInitDialog (), continue to add some methods. Add a button to display the image background. If you have images, import a BMP file. Set the ID number to IDB_BITMAP1. Adjust the window size to display the buttons completely.

 
 
  1. CMFCButton * button = new CMFCButton;
  2. Button-> Create (_ T ("Test Button "),
  3. WS_VISIBLE,
  4. CRect (5, 5, 80, 50 ),
  5. This,
  6. IDC_BUTTON_TEST );
  7. // Modify text
  8. Button-> SetWindowTextW (_ T ("force change "));
  9. // Change the background color
  10. Button-> SetFaceColor (RGB (153,217,234 ));
  11. // Change the font color
  12. Button-> SetTextColor (RGB (255,255,255 ));
  13. // Change the highlighted color
  14. Button-> SetTextHotColor (RGB (63, 72,204 ));
  15. // Change the location
  16. Button-> MoveWindow (50, 50,100, 70 );
  17. // Changes to a gesture when the mouse passes
  18. Button-> SetMouseCursorHand ();
  19. // Tooltip
  20. Button-> SetTooltip (_ T (" "));
  21.  
  22. CMFCButton * button1 = new CMFCButton;
  23. Button1-> Create (_ T ("Test Button "),
  24. WS_VISIBLE,
  25. CRect (160, 15,280,150 ),
  26. This,
  27. IDC_BUTTON_TEST );
  28. // Load the image
  29. Button1-> SetImage (IDB_BITMAP1 );

So an ugly button and a more ugly button were born ~

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A342O17-2.png "border =" 0 "alt =" "/>

When the mouse passes, it turns into a hand shape, and it is usually white. When the mouse passes, it turns into blue in the figure.

Simple? In vs2010, there should be many classes that can benefit programmers, and more mining will be required in the future.

 

This article from the "front of the tradeford reverse tairuibao blog, please be sure to keep this source http://serious.blog.51cto.com/242085/866172

Related Article

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.