[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

runtime resizing of picturebox

Does anybody know if it's possible to allow a user to resize a picturebox at runtime? I'd like the user to click the corner and drag a band to the required size.
1 Solution
Sure, here's a rough sample of how to do it.  You can spice it up by changing the mouse pointer and/or limiting the minimum size, maximum size, etc...

Create a new project and place a single pictruebox (Picture1) on Form1 and paste the following code:

Dim Drag As Boolean
Dim DragX As Single, DragY As Single
Dim DragW As Single, DragH As Single

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Drag = True
DragX = X
DragY = Y
DragW = Picture1.Width
DragH = Picture1.Height
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Drag Then
Picture1.Width = DragW + (X - DragX)
Picture1.Height = DragH + (Y - DragY)
End If

End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Drag = False
End Sub

Almost all controls used in vb can be resized using windows API Call, Which I am Adding below
 This is coded in VB3.0 but can be updated for VB4.0 and VB5.0.

Global Const GWL_STYLE = (-16)
Global Const WS_THICKFRAME = &H40000
Global Const SWP_DRAWFRAME = &H20
Global Const SWP_NOMOVE = &H2
Global Const SWP_NOSIZE = &H1
Global Const SWP_NOZORDER = &H4

Declare Function GetWindowLong Lib "User" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Long
Declare Function SetWindowLong Lib "User" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
Declare Function SetWindowPOs Lib "User" (ByVal hwnd%, ByVal hWndInsertAfter%, ByVal x%, ByVal y%, ByVal cX%, ByVal cY%, ByVal wFlags%) As Integer

Sub Set_Sizable_Border (F_Controls As Control, F_Form As Form)
    Dim Style As Long, Ret As Integer
    Style = GetWindowLong(F_Controls.hWnd, GWL_STYLE)
    Style = Style Or WS_THICKFRAME
    Style = SetWindowLong(F_Controls.hWnd, GWL_STYLE, Style)
    Ret = SetWindowPOs(F_Controls.hWnd, F_Form.hWnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME)
End Sub

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now