Microsoft Knowledge Base Email Alertz

Command buttons in Visual Basic for Windows are limited to a single line of text and one background color (gray). The 3D command button shipped in the Professional Editions of Visual Basic version 2.0, 3.0 and 4.0 for Windows does have the capability...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 140884 - Last Review: January 8, 2003 - Revision: 1.1

How to Make a Push Button with a Bitmap in Visual Basic

This article was previously published under Q140884

SUMMARY

Command buttons in Visual Basic for Windows are limited to a single line of text and one background color (gray). The 3D command button shipped in the Professional Editions of Visual Basic version 2.0, 3.0 and 4.0 for Windows does have the capability of displaying bitmaps within a command button in Visual Basic for Windows. However, there is no way to alter the background or border colors to change its appearance. You can create the look and feel of a command button by using a picture control and manipulating the DrawMode in conjunction with the Line method. Using a picture control also allows you to display the "command button" in any color with multiple lines of caption text.

MORE INFORMATION

The technique (demonstrated further below) simulates the effect of pressing a command button by using the Line method with the BF option (Box Fill) in invert mode each time a MouseUp or MouseDown event occurs for the picture control. To add multiline text to the "button," either print to the picture box or add the text permanently to the bitmap.

The steps to create a customized "command button" are as follows:

  1. Start Visual Basic for Windows, or choose New Project from the File menu (press ALT, F, N) if Visual Basic for Windows is already running. Form1 will be created by default.
  2. Put a picture control (Picture1) on Form1.
  3. Set the properties for Picture1 as given in the chart below:
        Property         Value
        --------         -----
        AutoRedraw       True
        AutoSize         True
        BorderStyle      0-None
        DrawMode         6-Invert
    						
  4. Assign the Picture property of Picture1 to the bitmap of your choice. For example, choose ARW01DN.ICO from the ARROWS subdirectory of the ICONS directory shipped with Visual Basic for Windows. This is a good example of a bitmap with a three dimensional appearance.
  5. Enter the following code in the Picture1_DblClick event procedure of Picture1:
        Private Sub Picture1_DblClick ()
            Picture1.Line (0, 0)-(Picture1.width, Picture1.height), , BF
        End Sub
    						
    NOTE: This code is necessary to avoid getting the bitmap stuck in an inverted state because of Mouse messages being processed out of order or from piling up due to fast clicking.
  6. Enter the following code in the Picture1_MouseDown event procedure of Picture1:
        Private Sub Picture1_MouseDown (Button As Integer, Shift As Integer,
                              X As Single, Y As Single)  ' Append to above line
            Picture1.Line (0, 0)-(Picture1.width, Picture1.height), , BF
        End Sub
    						
  7. Enter the following code in the Picture1_MouseUp event procedure of Picture1:
        Private Sub Picture1_MouseUp (Button As Integer, Shift As Integer,
                              X As Single, Y As Single) ' Append to above line.
            Picture1.Line (0, 0)-(Picture1.width, Picture1.height), , BF
        End Sub
    						
  8. Add the following code to the Picture1_KeyUp event procedure for Picture1:
        Private Sub Picture1_KeyUp (KeyCode As Integer, Shift As Integer)
            '* Check to see if the ENTER key was pressed.  If so, restore
            '* the picture image.
            If KeyCode = 13 Then
              Picture1.Line (0, 0)-(Picture1.width, Picture1.height), , BF
            End If
        End Sub
    						
  9. Add the following code to the Picture1_KeyDown event procedure for Picture1:
        Private Sub Picture1_KeyDown (KeyCode As Integer, Shift As Integer)
            '* Check to see if the ENTER key was pressed.  If so, invert
            '* the picture image.
            If KeyCode = 13 Then
              Picture1.Line (0, 0)-(Picture1.width, Picture1.height), , BF
            End If
        End Sub
    						
  10. From the Run menu, choose Start. Click the picture box. The image of the picture should be inverted while the mouse button is down, giving the visual effect of a button press.

APPLIES TO
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 16-bit Enterprise Edition
Keywords: 
KB140884
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate