New Controls for MFC - Push buttons
(Page 4 of 5 )
However, returning to the push button. As I just presented, you can add an image near the text image. Customize the position of these two so that they're related to each other, or disable some of it.
Add a new button to the resource property page. Modify it to the new extended one: CMFCButton. Make sure to embed the control inside the dialog. During the initialization, you can make the button transparent with the m_bTransparent member:
m_Button.m_bTransparent = TRUE;
If you do not use the systems themes, you can set three types of border: flat, semi-flat or a 3D look:



m_Button.m_nFlatStyle = CMFCButton::BUTTONSTYLE_3D;
If you do not want to display the image, just set a null image pointer to the button:
m_Button.SetImage((HBITMAP) NULL);
Alternatively, if you want an image, you actually need to set two images: one for when the button is pushed (it is hot), and one for the other times. Just add/create the new bitmaps and use their resource ID.





if (afxGlobalData.bIsOSAlphaBlendingSupport)
{
m_Button.SetImage(IDB_BTN1_32, IDB_BTN1_HOT_32);
}
else
{
m_Button.SetImage(IDB_BTN1, IDB_BTN1_HOT);
}
Use the SetWindowText call to set new text, or just set none by passing an empty string:
m_Button.SetWindowText(_T(""));
You can change the position of the image if you modify the values of the two public members: m_bRightImage (left/right alignment as compared with the text) and m_bTopImage (up-down alignment as compared with the text). These are bool values. For example, in our case, we delineated three scenarios:
switch (m_nImageLocation)
{
case 0:
m_Button.m_bRightImage = FALSE;
m_Button.m_bTopImage = FALSE;
break;
case 1:
m_Button.m_bRightImage = TRUE;
m_Button.m_bTopImage = FALSE;
break;
case 2:
m_Button.m_bRightImage = FALSE;
m_Button.m_bTopImage = TRUE;
break;
}
The m_nAlignStyle will accept three enum values: center, left and right. Use this to align the text inside the button.
m_btnRadio1.m_nAlignStyle = CMFCButton::ALIGN_CENTER;
Whenever you make a change, it is a good idea to resize the control and invalidate it. This will ensure that the application will draw the most up-to-date state.
m_Button.SizeToContent();
m_Button.Invalidate();
You can set custom cursors. This is the shape of the mouse while it hovers over the corresponding button. You can use the system default, set a hand cursor or use one from a custom cursor file.
switch (m_iCursor)
{
case 0:
m_Button.SetMouseCursor(NULL); //system cursor
break;
case 1:
m_Button.SetMouseCursorHand(); // Hand cursor
break;
case 2:
m_Button.SetMouseCursor(AfxGetApp()-> LoadCursor(IDC_CURSOR)); // custom
break;
}



You can set some custom tooltips with the SetTooltip method:
m_Button.SetTooltip(m_strToolTip_text);
Here it an image made from integrating all of this inside a group that you can modify on the fly:

Next: Menu buttons >>
More .NET Articles
More By Gabor Bernat