Displaying image

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Is there any way I can have an image displayed on a Form, and as the Form is dragged/resized so is the image ?

Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hello AlexanderBB,

Add the following code to your VBA UserForm. This code will make it resizable. If your UserForm uses any of the events listed in the code shown then add the code from here to your existing event code.

Make UserForm Resizable
Code:
' Written:  September 12, 2017
' Author:   Leith Ross


Dim OldWidth    As Double
Dim OldHeight   As Double


Const ZoomMin   As Long = 10
Const ZoomMax   As Long = 400


Private Declare Function GetForegroundWindow Lib "User32.dll" () As Long


Private Declare Function GetWindowLong _
    Lib "User32.dll" Alias "GetWindowLongA" _
        (ByVal hwnd As Long, _
         ByVal nIndex As Long) _
    As Long
               
Private Declare Function SetWindowLong _
    Lib "User32.dll" Alias "SetWindowLongA" _
        (ByVal hwnd As Long, _
         ByVal nIndex As Long, _
         ByVal dwNewLong As Long) _
    As Long


Private Declare Function ShowWindow _
    Lib "User32.dll" _
        (ByVal hwnd As Long, _
         ByVal nCmdShow As Long) _
    As Long


Private Sub MakeFormResizable()


    Dim lStyle  As Long
    Dim hwnd    As Long
    Dim RetVal  As Long
  
    Const WS_MAXIMIZEBOX     As Long = &H10000
    Const WS_MINIMIZEBOX     As Long = &H20000
    Const WS_THICKFRAME      As Long = &H40000
    Const GWL_STYLE          As Long = -16
        
        hwnd = GetForegroundWindow
  
        ' Get the basic window style
        lStyle = GetWindowLong(hwnd, GWL_STYLE) Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX


        ' Set the basic window styles
        RetVal = SetWindowLong(hwnd, GWL_STYLE, lStyle)


End Sub




Private Sub UserForm_Activate()


        MakeFormResizable
        
End Sub


Private Sub UserForm_Initialize()


        OldWidth = Me.Width
        OldHeight = Me.Height


End Sub


Private Sub UserForm_Resize()


    Dim CurStyle    As Long
    Dim tmpZoom     As Double
    
    Const WS_MAXIMIZE   As Long = &H1000000
        
        CurStyle = GetWindowLong(GetForegroundWindow, GWL_STYLE)
        
        tmpZoom = ((Me.Width / OldWidth) * 100) - 2.5
        If tmpZoom <= ZoomMin Then tmpZoom = ZoomMin
        If tmpZoom >= ZoomMax Then tmpZoom = ZoomMax
        
        If tmpZoom <= ZoomMin Or tmpZoom >= ZoomMax Then
            If (CurStyle And WS_MAXIMIZE) = WS_MAXIMIZE Then
                Me.Width = (tmpZoom * OldWidth) / 100
                Me.Height = (Me.Width * OldHeight) / OldWidth
            End If
        End If
        
        Me.Zoom = tmpZoom


End Sub
 
Upvote 0
Hi Leith,

Wow! That's almost exactly what I was hoping for. Thank you. The only thing is, can it be modelless? If not possibly I could use it's click event to move to any 'next' image?
Would you advise to use, or not use, an image control on the Form. So far i haven't. I've set Pic alignment to 0 and size Mode to Clip. Just playing around now with an initial Height and Width.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top