PRB: CButton SetIcon and SetBitmap Work Only Under Windows 95

Last reviewed: July 10, 1997
Article ID: Q142226
4.00 WINDOWS NT kbprg kbcode kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, version 4.0

SYMPTOMS

When a button is displayed on Windows NT, which uses these functions and styles, the button will not appear. If you click in the area that the button should be, you will get an outline of a check-box button.

CAUSE

Windows NT does not support the BS_ICON and BS_BITMAP styles.

RESOLUTION

Only use these features of CButton when the Windows version is greater than or equal to 4.

Remove the BS_ICON or BS_BITMAP style from the dialog template. Then use GetVersion to determine the Windows version. If the version is greater than or equal to 4, use ModifyStyle to add the BS_ICON or BS_BITMAP style and call SetIcon or SetBitmap. See the "Sample Code" section of this article.

Using this method, under Windows NT the user will see the text caption version of the button, but under Windows 95 the desired icon or bitmap will be seen.

MORE INFORMATION

In the following code, m_ok is a CButton member variable, m_hIcon is a HICON member variable, and IDI_ICON1 is the ID of some icon that will be displayed under Windows 95.

Sample Code

/* compile options needed: default
*/

BOOL CAboutDlg::OnInitDialog() {
    CDialog::OnInitDialog();

    if ( (BYTE)GetVersion() >=4 )
    {
        m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
        if (NULL != m_hIcon)
        {
            m_ok.ModifyStyle(0, BS_ICON);
            m_ok.SetIcon(m_hIcon);
        }
    }
    else
        m_hIcon = NULL;

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

void CAboutDlg::OnDestroy()
{
    CDialog::OnDestroy();

    // destroy the icon if it was loaded
    if (NULL != m_hIcon)
        DestroyIcon(m_hIcon);
}

REFERENCES

Q125673 "New Windows 95 Styles make attaching bitmap to button easier"

Win32 SDK BM_SETIMAGE documentation


Additional reference words: 4.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: MfcUI
Keywords : MfcUI kbcode kbprb kbprg
Technology : kbMfc
Version : 4.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.