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:
- CMFCButton * button = new CMFCButton;
- button->Create(_T("Test Button"),
- WS_VISIBLE,
- CRect(5, 5, 80, 50),
- this,
- 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.
- CMFCButton * button = new CMFCButton;
- Button-> Create (_ T ("Test Button "),
- WS_VISIBLE,
- CRect (5, 5, 80, 50 ),
- This,
- IDC_BUTTON_TEST );
- // Modify text
- Button-> SetWindowTextW (_ T ("force change "));
- // Change the background color
- Button-> SetFaceColor (RGB (153,217,234 ));
- // Change the font color
- Button-> SetTextColor (RGB (255,255,255 ));
- // Change the highlighted color
- Button-> SetTextHotColor (RGB (63, 72,204 ));
- // Change the location
- Button-> MoveWindow (50, 50,100, 70 );
- // Changes to a gesture when the mouse passes
- Button-> SetMouseCursorHand ();
- // Tooltip
- Button-> SetTooltip (_ T (" "));
-
- CMFCButton * button1 = new CMFCButton;
- Button1-> Create (_ T ("Test Button "),
- WS_VISIBLE,
- CRect (160, 15,280,150 ),
- This,
- IDC_BUTTON_TEST );
- // Load the image
- 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