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

Properly Resizing an MDI window to autofit an image.

I have an MDI window that has a picturebox on it. When the user loads a picture it will set that picturebox to the image the user picked. The picture box is on auto-size but the form needs to auto-size at the same time. Is there an easy way to know how much room a window's border and titlebar take up so that I can properly set the size of the MDI window?
0
TeleKawaru
Asked:
TeleKawaru
  • 4
  • 3
  • 2
1 Solution
 
EDDYKTCommented:
Is just

Private Sub Picture1_Resize()

    Me.Width = Picture1.Width
    Me.Height = Picture1.Height
End Sub
0
 
EDDYKTCommented:
Or

set form's scalemode to 1

Option Explicit

Const SM_CXFRAME = 32
Const SM_CXDLGFRAME = 7
Const SM_CYCAPTION = 4
Const SM_CYSMCAPTION = 51

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


Private Sub Form_Load()
Picture1.Picture = LoadPicture("Your picture file")
End Sub

Private Sub Picture1_Resize()


Dim lMetric As Long
Dim lBorder As Long
Dim lCaption As Long

'Determine what kind of window I am
Select Case Me.BorderStyle
Case 1 'Fixed single
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYCAPTION
Case 2 'Sizable
   lBorder = SM_CXFRAME
   lCaption = SM_CYCAPTION
Case 3 'Fixed Dlg
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYCAPTION
Case 4 'Fixed Tool
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYSMCAPTION
Case 5 'Sizable Tool
   lBorder = SM_CXFRAME
   lCaption = SM_CYSMCAPTION
End Select

'Get the border width in pixels
lMetric = GetSystemMetrics(lBorder)
'Convert to twips
lMetric = lMetric * Screen.TwipsPerPixelX
Debug.Print "Border size in twips: " & CStr(lMetric)
'Get title bar height in pixels
lMetric = GetSystemMetrics(lCaption)
'Convert to twips
lMetric = lMetric * Screen.TwipsPerPixelX
Debug.Print "Titlebar height in twips: " & CStr(lMetric)

    Me.Width = Picture1.Width + (Form1.Width - Form1.ScaleWidth) / 2
    Me.Height = Picture1.Height + lMetric
End Sub
0
 
mccainz2Commented:
Couple of comments , a picture is measured in HiMetric which is .01 of a millimeter .... which causes all kinds of fun .....

code snippet , still not 100% correct ...

Private Declare Function MulDiv Lib "kernel32" ( _
   ByVal Mul As Long, _
   ByVal Nom As Long, _
   ByVal Den As Long) As Long

Private Sub mnuSize_Click()
   
    MDImain.Width = HM2Pix(Picture1.Picture.Width)
    MDImain.Height = HM2Pix(Picture1.Picture.Height)
   
End Sub

Private Function HM2Pix(ByVal Value As Long) As Long

   HM2Pix = MulDiv(Value, 1440, 2540) * Screen.TwipsPerPixelX

End Function

EDDYKT  : I tried the first example but no love! Is there any properties I need to set to get that to work

...Learning as I go...
0
Independent Software Vendors: 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!

 
mccainz2Commented:
BTW: If I wasnt clear I was only commenting  , not proposing an answer , please Do not accept (as my solution doesnt work exactly) .. EDDYKT will bring you home I have no doubt...
0
 
EDDYKTCommented:
Try the second example. It's close enough.

8->

0
 
TeleKawaruAuthor Commented:
I used your second example EDDYKT but the window is still too small in both the horizontal and the vertical.
0
 
mccainz2Commented:
Addendum the HM2Pix function should be written as

Private Function HM2Pix(ByVal Value As Long) As Long

   HM2Pix = MulDiv(Value, 1440, 2540)

End Function
0
 
TeleKawaruAuthor Commented:
I'm giving the points to EDDYKT because I was able to use his as a base to get it right. Here is the right code (for the image_resize part - that's all the was off):

Private Sub picImage_Resize()
Dim lMetric As Long
Dim lBorder As Long
Dim lCaption As Long
Dim lBorderSize As Long
Dim lCaptionSize As Long

'Determine what kind of window I am
Select Case Me.BorderStyle
Case 1 'Fixed single
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYCAPTION
Case 2 'Sizable
   lBorder = SM_CXFRAME
   lCaption = SM_CYCAPTION
Case 3 'Fixed Dlg
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYCAPTION
Case 4 'Fixed Tool
   lBorder = SM_CXDLGFRAME
   lCaption = SM_CYSMCAPTION
Case 5 'Sizable Tool
   lBorder = SM_CXFRAME
   lCaption = SM_CYSMCAPTION
End Select

'Get the border width in pixels
lMetric = GetSystemMetrics(lBorder)
'Convert to twips
lBorderSize = lMetric * Screen.TwipsPerPixelX
Debug.Print "Border size in twips: " & CStr(lBorderSize)
'Get title bar height in pixels
lMetric = GetSystemMetrics(lCaption)
'Convert to twips
lCaptionSize = lMetric * Screen.TwipsPerPixelY
Debug.Print "Titlebar height in twips: " & CStr(lMetric)

    Me.Width = picImage.Width + (lBorderSize * 2)
    Me.Height = picImage.Height + lCaptionSize + (lBorderSize * 2)
End Sub
0
 
EDDYKTCommented:
Whatever it works for you. I won't see a lot different from my eye. Too old to see small different.

8->

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now