By default, the MFC button displays the normal cursor when the mouse is positioned on it. The MFC button gives you the option to use a different cursor. At design time, to specify that the button will use a hand cursor, in the Properties window, click Cursor Type and select Hand. To programmatically set the cursor to a hand, you can call the SetMouseCursorHand() member function. Its syntax is: void SetMouseCursorHand(); To let you programmatically specify the cursor, the CMFCButton class provides the SetMouseCursor() member function whose syntax is: void SetMouseCursor(HCURSOR hcursor); Before calling this member function, you can first import a cursor file to your project. Call the LoadCursor() function and pass its returned value to the CMFCButton::SetMouseCursor() member function. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetMouseCursor(LoadCursor(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_CURSOR1)));
return TRUE;
}
To provide more information about a button, you can display a tool tip when the mouse is positioned on top of it. To do this visually, in the Properties window, click Tooltip and type the desired string. To let you programmatically create a tool tip, the CMFCButton class is equipped with the SetTooltip() member function. Its syntax is: void SetTooltip(LPCTSTR lpszToolTipText); Here is an example of calling this function: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetTooltip(_T("Click this button to perform the calculation"));
return TRUE; // return TRUE unless you set the focus to a control
}
To enhance the appearance of an MFC button, you can display an icon on its face. To support this, the class is equipped with a member function named SetImage and that is overloaded with three versions. The version for an icon uses the following syntax: void SetImage(HICON hIcon, BOOL bAutoDestroy=TRUE, HICON hIconHot=NULL, HICON hIconDisabled=NULL, BOOL bAlphaBlend=FALSE ); Only the first argument is required. Before using it, you can first import an icon file to your project and give that icon an ID. Call the LoadIcon() function and pass its returned value to the CMFCButton::SetImage() member function. Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetImage(LoadIcon(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ARROW)));
return TRUE; // return TRUE unless you set the focus to a control
}
It is important to know that icons usually have a 16x16 or a 32x32 pixels size. If you call the SetImage() member function, the 32x32 pixel size would be used. If you'd rather use the 16x16 size, you may have to call the LoadIcon() member function of the CButton class.
If you don't want to deal with the size limits of an icon, you can call one of the other two versions of the CMFCButton::SetItmage() member functions. Their syntaxes are: void SetImage(HBITMAP hBitmap, BOOL bAutoDestroy = TRUE, HBITMAP hBitmapHot = NULL, BOOL bMap3dColors = TRUE, HBITMAP hBitmapDisabled = NULL); void SetImage(UINT uiBmpResId, UINT uiBmpHotResId = 0, UINT uiBmpDsblResID = 0); Before using one of these versions, you should first import or design 1 to 4 bitmaps and give them IDs. When you call this member function, you can pass the ID(s) of the bitmap(s) as argument(s):
To paint an MFC button with a color of your choice, you can call the SetFaceColor() member function. Its syntax is: void SetFaceColor(COLORREF crFace, BOOL bRedraw=TRUE); Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetFaceColor(2252835);
return TRUE; // return TRUE unless you set the focus to a control
}
To specify the color used on the caption of an MFC button, you can call the SetTextColor() member function whose syntax is: void SetTextColor(COLORREF clrText); Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetFaceColor(2252835);
m_Calculate.SetTextColor(RGB(255, 255, 205));
return TRUE; // return TRUE unless you set the focus to a control
}
You can also specify what color the caption should use when the mouse is positioned on the button. This is done by calling the SetTextHotColor() member function. Its syntax is: void SetTextHotColor(COLORREF clrTextHot); Here is an example: BOOL CExerciseDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_Calculate.SetTextColor(RGB(255, 255, 205));
m_Calculate.SetTextHotColor(8468360);
return TRUE; // return TRUE unless you set the focus to a control
}
|
|
|||||||||||||
|