|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic
Window/Form Routines GetSystemMetrics: Resizing a Form to Fit an Image |
||
Posted: | Friday January 03, 2003 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows XP; Updated with VB6, Vista | |
OS restrictions: | None | |
Author: | 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.
In response to a comment about it not resizing perfectly due to 'magic
numbers' I had used in the original resizing routine, I have updated the
code to work without any such hard-coded values and thus resize the form
irrespective of image loaded. 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. The image-form has a dummy menu shown, to demonstrate the code works with or without a menu present. GetSystemMetrics retrieves the sizes of various windows bits and pieces, calculating the required size to tightly fit the form around the loaded image. Note that in order to display small images, its is necessary to remove the Control menu of the image-display form; the form can't resize smaller than the width of the control menu plus the form min/max/close buttons displayed. Note too the comments in AutoSizeToPicture related to the existence of a hidden menu on the image 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 - Common Dialog Form |
Add a common dialog control and a command button to a form, along with the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 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 = "e:\" 'change as required .ShowOpen If Len(.FileName) > 0 Then Load Form2 Form2.Picture1.Picture = LoadPicture(.FileName) Form2.Caption = .FileName Form2.Show End If End With End Sub |
Form Code |
Add a single image control to the form. The load event sets it up for use. |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 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 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_CYCAPTION = 4 Private Const SM_CYMENU = 15 Private Const SM_CXFRAME = 32 Private Const SM_CYFRAME = 33 Private twipsx As Long Private twipsy As Long Private Sub Form_Load() twipsx = Screen.TwipsPerPixelX twipsy = Screen.TwipsPerPixelY With Picture1 .AutoSize = True .Appearance = 0 .Appearance = 0 .BackColor = &HFFFF80 .BorderStyle = 0 End With 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_CXFRAME) * 2) * twipsx vOffset = (GetSystemMetrics(SM_CYCAPTION) + (GetSystemMetrics(SM_CXFRAME)) * 2) * twipsx 'if the form also has a menu, 'account for that too. ' 'NOTE: If you are just hiding the menu, then 'GetMenu(Me.hwnd) will return non-zero even 'if the menu is hidden, causing an incorrect 'vertical offset to be used. Either delete 'the menu using the menu editor, or if you 'must have the ability to show/hide a menu 'on the picture form, you will need to code 'to also test for me.mnuX.visible then... ' 'You can determine whether the correct sizing 'is taking place by viewing the values returned 'to the immediate window from the debug.print 'code below; the values for the form and 'picture should be the same, e.g. ' picture 3450 2385 ' form 3450 2385 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.Width = .Width + hOffset Me.Height = .Height + vOffset End With 'these values should be the same 'if the calculations worked Debug.Print "picture", Picture1.Width, Picture1.Height Debug.Print "form", Form2.ScaleWidth, Form2.ScaleHeight End Sub |
Comments |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |