Finance

Charts

Statistics

Macros

Search

Creating a Custom Form or UserForm with a Changeable Background, Excel VBA

A background image can be incorporated into a form using the Picture property. This property displays the image at its original dimensions. If you want the image to fill the entire client area of the form or to stretch across its full width or height, use the PictureSizeMode property. The PictureAlignment property places the image within the form’s client area—for example, centered or aligned to specific sides of the form.

Let’s build a project with a form where an image is displayed as the background. When you click the form, two images will alternate.

To implement the project, you need two images. In this case:
C:\image1.jpg and C:\image2.jpg.

Now, create a form and use the Properties Window to set its property values as shown in the table below:

Table: Property Values Set in the Properties Window

Object Property Value
Form/UserForm Picture Link to bitmap file C:\image1.jpg
PictureSizeMode FmPictureSizeModeStretch
Caption Changeable Backgrounds

Then, double-click on the left mouse button inside the form, and in the opened UserForm module, type the following code:

Private Sub UserForm_Click()
    Static flag As Boolean
    Dim filename As String
    If Not flag Then
        filename = "C:\image1.jpg"
        Me.Picture = LoadPicture(filename)
        Me.PictureSizeMode = fmPictureSizeModeStretch
        Me.Caption = "Changeable Background Images " & filename
    Else
        filename = "C:\image2.jpg"
        Me.Picture = LoadPicture(filename)
        Me.PictureSizeMode = fmPictureSizeModeZoom
        Me.PictureAlignment = fmPictureAlignmentTopLeft
        Me.Caption = "Changeable Background Images " & filename
    End If
    Me.Repaint
    flag = Not flag
End Sub

Remarks

Now, when you click the form, the images C:\image1.jpg and C:\image2.jpg will alternate as background images.

  • The image is loaded from a file using the LoadPicture function, whose parameter is the source filename.
  • Since the value of the PictureSizeMode property for image C:\image1.jpg is fmPictureSizeModeStretch, it will stretch or shrink without keeping proportions to fill the form’s client area.
  • The PictureSizeMode value for image C:\image2.jpg is fmPictureSizeModeZoom, so it will stretch or shrink proportionally to fit either the width or height of the client area.
  • The PictureAlignment property set to fmPictureAlignmentTopLeft aligns the top-left corner of the image with the top-left corner of the form’s client area.
  • In the Properties window, you can remove the image by placing the cursor in the Picture field and pressing the Delete key.
  • In code, the image is removed by assigning an empty value to the Picture property, like this:
Me.Picture = LoadPicture("")
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx