Drag Picture on Userform

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,591
Is it possible to have a Picture on a UserForm and have its size change with the Form as you drag the bottom right corner ?
I have this working on some other platfoms (Access, VB6) but can't seem to manage it in Excel - or the Picture flashes
white bars while moving.
Any help much appreciated.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,245
Something quickly cobbled together worked first time for me. This is what I did, which I suggest you simply replicate for your first test:

1. Create a userform in a new workbook
2. Manuallly amend userform's Picture Size Mode property to 1-fmPictureSizeModeStretch
3. Insert picture directly onto the userform ( click on Picture property \ click box with 3 dots to browse to folder \ select a file )
4. Insert code as instructed below
5. Run the userform and drag right corner

Caveat
Leith's code may need updating if you are running 64-bit version of Excel
- if unable to do that yourself, then I suggest you start a new thread specifically asking for the code to be modified to handle both 32 & 64 bit

Credit for the code @Leith Ross
Here is the link https://www.mrexcel.com/forum/excel-questions/485489-resize-userform.html


In a Module
Code:
[COLOR=#006400]'Written: August 02, 2010                   Author:  Leith Ross
'Summary: Makes the UserForm resizable by dragging one of the sides. Place a call
'         to the macro MakeFormResizable in the UserForm's Activate event.[/COLOR]

Private Declare Function SetLastError _
   Lib "kernel32.dll" (ByVal dwErrCode As Long) As Long
   
Public Declare Function GetActiveWindow 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

Public Sub MakeFormResizable()
    Dim lStyle As Long, hWnd As Long, RetVal
    Const WS_THICKFRAME = &H40000, GWL_STYLE As Long = (-16)
  
    hWnd = GetActiveWindow
'Get the basic window style
    lStyle = GetWindowLong(hWnd, GWL_STYLE) Or WS_THICKFRAME
'Set the basic window styles
    RetVal = SetWindowLong(hWnd, GWL_STYLE, lStyle)
'Clear any previous API error codes
    SetLastError 0
'Did the style change?
    If RetVal = 0 Then MsgBox "Unable to make UserForm Resizable."
End Sub
In Userform code
Code:
Private Sub UserForm_Activate()
    MakeFormResizable
End Sub
 
Last edited:

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,591
Thanks very much Yongle. I like that code, but like I had, the image falls apart with nasty white flashes when moved.
Is this expected/normal, or something weird happening here ?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,245
:confused: On my screen the image is perfect when dragged
- the image does not fall apart
- there are no nasty white flashes when moved
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,410
Office Version
2016
Platform
Windows
Thanks very much Yongle. I like that code, but like I had, the image falls apart with nasty white flashes when moved.
Is this expected/normal, or something weird happening here ?
Can you show us the code you are using ? Maybe if you could upload here a simplified test userform workbook so we can reproduce the issue you are describing
 
Last edited:

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,591
The code is exactly as posted by Yongle. But he doesn't have the problem.. I do but on XP using Excel 2010. Maybe that's part of it?
I do have a win10/Excel 2019 machine but there's a change there and no Windows drag anymore, just the outline and the image / picture
goes away until mouse up. I do not have the problem when that happens.
I have made a recording of the effect and would you like you and Yongle to see it, but AFAIK you can't upload a file here?
 

Forum statistics

Threads
1,077,827
Messages
5,336,614
Members
399,093
Latest member
chado4250

Some videos you may like

This Week's Hot Topics

Top