Visual Basic Window/Form Routines
GetSystemMetrics: Resizing a Form to Fit an Image
     
Posted:   Friday January 03, 2003
Updated:   Tuesday August 12, 2008
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   msnews, VBnet - Randy Birch
     
 Prerequisites
None.

This routine was posted in response to a newsgroup request for a method to resize a child window precisely to the size of a randomly loaded image. I regret I did not keep the name of the original poster of this code.

The demo uses one form to obtain the filename of the image to load via a common dialog, then loads the selected file into a picture box on a second form, invoking that form's AutoSizeToPicture routine via the picture box Change event.

GetSystemMetrics retrieves the sizes of various windows bits and pieces, calculating the required size to tightly fit the form around the loaded image. In order to display small images, its is necessary to remove the Control menu of the image-display form.

Form 1 - the main form - needs only a command button and a common dialog control.

Form 2 - the picture form - only requires a borderless picture box.

 BAS Module Code
None.

 Form1 Code - Main Form
To the form, add the following code:

Option Explicit
         

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2008 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Form_Load()

   Command1.Caption = "Load Image..."
  
End Sub


Private Sub Form_Unload(Cancel As Integer)
  
   Unload Form2
  
End Sub


Private Sub Command1_Click()

   With CommonDialog1
  
      .Filter = "Image files (gif, jpg, bmp)|*.gif;*.jpg;*.bmp|"
      .FilterIndex = 0
      .InitDir = "c:\"
      .ShowOpen

      If Len(.FileName) > 0 Then
     
         Load Form2
         Form2.Picture1.Picture = LoadPicture(.FileName)
         Form2.Caption = .FileName
         Form2.Show
     
      End If
  
   End With
  
End Sub
 Form2 Code - Picture form
To the form, add the following code:

Option Explicit
Private Declare Function GetSystemMetrics Lib "user32" _
   (ByVal nIndex As Long) As Long

Private Declare Function GetMenu Lib "user32" _
   (ByVal hwnd As Long) As Long

Private Const SM_CXBORDER = 5
Private Const SM_CYCAPTION = 4
Private Const SM_CYBORDER = 6
Private Const SM_CYMENU = 15

Private twipsx As Long
Private twipsy As Long


Private Sub Form_Load()

   twipsx = Screen.TwipsPerPixelX
   twipsy = Screen.TwipsPerPixelY
           
End Sub


Private Sub Picture1_Change()

   Picture1.AutoSize = True
   Call AutoSizeToPicture(Picture1)
           
End Sub


Private Sub AutoSizeToPicture(pbox As PictureBox)

   Dim vOffset As Long
   Dim hOffset As Long
                     
    hOffset = ((GetSystemMetrics(SM_CXBORDER) * twipsx) * 4) + 40
           
    vOffset = GetSystemMetrics(SM_CYCAPTION) * twipsy + _
             ((GetSystemMetrics(SM_CYBORDER) * twipsy) * 4) + 40
                        
               
                
  'if the form also has a menu,
  'account for that too
   If GetMenu(Me.hwnd) <> 0 Then
      vOffset = vOffset + GetSystemMetrics(SM_CYMENU) * twipsy
   End If

  'position the picture box and resize the form
   With pbox
      .Left = 0
      .Top = 0
      Me.Move Me.Left, Me.Top, .Width + hOffset, .Height + vOffset
   End With

End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2008 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter