MFC uses the bitmap button, and sets the mouse hover effect of the button, the mfc bitmap
System Environment: Windows 10
Software environment: Visual C ++ 2013 SP1
Purpose: To use the bitmap button and set the mouse hover effect of the button
When using MFC for development, the interface is relatively difficult to develop. The CBitmapButton class is self-contained in VC, but it is not used much. Many buttons Derived classes on the Internet are quite practical. However, if the simple bitmap Button is implemented with the built-in CBitmapButton, the following is:
Step 1: Create a project based on the dialog box and place a button to adjust the size, set its Owner Draw attribute to TRUE, right-click the button to add a variable, and change its variable type to CBitmapButton, completed by confirmation;
Step 2: Import button bitmap. Here, the four images must be of the same size. Below are the buttons for the four types of bodies. If you want to experiment with this design, you can right-click and save it, then save the png format as bmp using "Drawing. In the resource view, right-click "Add Resource", and "import" to slice:
Step 3: Initialize the task by adding the following code in the OnInitDialog () function of the dialog box:
M_btButton.LoadBitmaps (IDB_BITMAP1, IDB_BITMAP2, IDB_BITMAP3, IDB_BITMAP4); // load m_btButton.SizeToContent (); // adjust the button to fit the image size
Step 4: Add code to control the available and unavailable Bitmap Buttons. This is the button to verify the fourth status:
void CTestDlg::OnBnClickedButton2() { m_btButton.EnableWindow(0); } void CTestDlg::OnBnClickedButton3() { m_btButton.EnableWindow(); }
Step 5: The bitmap button has been implemented here. You can see the result after compiling and running. However, when the mouse slides over, the button does not sense the existence of the focus. Here, the timer is used to capture the focus on the button, and the button body at this time is changed to the third image. First, add an OnTimer () event to the form, and fill in the following code in the OnTimer () function:
Void CTestDlg: OnTimer (UINT_PTR nIDEvent) {// TODO: add the message processing program code and/or call the default value if (1 = nIDEvent) {POINT p; getCursorPos (& p); CWnd * hwnd; hwnd = WindowFromPoint (p); // the retrieval window contains the specified vertex; the vertex must specify a vertex of the screen coordinate on the screen. CWnd * hwnd2 = GetDlgItem (IDC_BUTTON1); if (hwnd2 = hwnd) // if you mouse over the button {if (GetFocus ()! = Hwnd2) // if the button has no focus, set the focus hwnd2-> SetFocus ();} else // if the mouse is not on the button; {if (GetFocus () = hwnd2) // If the button has a focus, go to the focus; GetDlgItem (IDC_BUTTON2)-> SetFocus (); // The IDC_BUTTON2 Control ID you want to jump to} CDialog :: onTimer (nIDEvent );}
Add the Enable Timer In the OnInitDialog () function of the dialog box. The interval is ms.
SetTimer(1,200,NULL);
After the operation is complete, you can achieve the same effect as the image shown at the beginning of the article.