INFO: Windows 95 Styles Make Attaching Bitmap to Button Easier

Last reviewed: January 5, 1998
Article ID: Q125673
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows 95

SUMMARY

Under Windows 95, there are two new button styles (BS_BITMAP and BS_ICON). Using these styles makes attaching a bitmap or an icon to a button in Windows 95 easier than it was in Windows version 3.1.

MORE INFORMATION

Under Windows version 3.1 you create the button by using the CreateWindow() function with the BS_OWNERDRAW style to attach a bitmap or an icon to a button. Each time the parent window receives the WM_DRAWITEM message the bitmap or the icon must be loaded and drawn on the button.

Under Windows 95 you can create a button with style BS_BITMAP to display a bitmap instead of text on the button. After you create the button by using CreateWindow() function, assign the bitmap to the button by sending a BM_SETIMAGE message to the button with the wparam as IMAGE_BITMAP and lparam as a handle to the bitmap. Windows displays the specified bitmap on the button. The attached bitmap should not be deleted until the button is destroyed, or another bitmap is assigned to the button.

Sample Code

   hwndButton = CreateWindow(
                 "BUTTON",   // predefined class
                 "OK",       // button text
                 WS_VISIBLE| WS_CHILD | BS_DEFPUSHBUTTON |BS_BITMAP,
                 //styles
                 // Size and position values are given explicitly, because
                 // the CW_USEDEFAULT constant gives zero values for
                 // buttons.
                 5, // starting x position
                 5, // starting y position
                 30,// button width
                 18, // button  height
                 hWnd,// parent window
                 NULL,// No menu
                 (HINSTANCE) GetWindowLong(hWnd,
                             GWL_HINSTANCE), NULL); // pointer not needed

   // load the bitmap, NOTE: delete it only when it's no longer being used.
   hBitmap   = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));
   // associate the bitmap with the button.
   SendMessage(hwndButton,BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,
              (LPARAM)(HANDLE)hBitmap);

Under Windows 95, you can also create a button with style BS_ICON to display a icon instead of text on the button. After creating the button by using CreateWindow() function, assign the icon to the button by sending a BM_SETIMAGE message to the button with the wparam as IMAGE_ICON and the lparam as the handle to the icon. Windows displays the specified icon on the button.

NOTE: The system handles cleanup of icons or cursors loaded from resources. Therefore, the icon should not be deleted unless the icon was created at run time by using CreateIcon() function.

Sample Code

 hwndButton = CreateWindow(
                  "BUTTON",   // predefined class
                  "OK",       // button text
                  WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON|BS_ICON,//styles
               // Size and position values are given explicitly, because
               // the CW_USEDEFAULT constant gives zero values for buttons.

                   5, // starting x position
                   5, // starting y position
                  30, // button width
                  18, // button height
                  hWnd,   // parent window
                  NULL,   // No menu
                  (HINSTANCE) GetWindowLong(hWnd,GWL_HINSTANCE),
                  NULL);      // pointer not needed

 // load the icon, NOTE: you should delete it after assigning it.
 hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON1));
 // associate the icon with the button.
 SendMessage(hwndButton,BM_SETIMAGE,(WPARAM)IMAGE_ICON,
            (LPARAM)(HANDLE)hIcon);

If a bitmap or icon is not specified via the BM_SETIMAGE message and the BS_BITMAP or BS_ICON style is specified, a blank button with no text is displayed.
Keywords          : UsrWndw
Platform          : Win95
Issue type        : kbinfo


================================================================================


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: January 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.