Solved

Properly Resizing an MDI window to autofit an image.

Posted on 2003-12-08
9
375 Views
Last Modified: 2012-08-13
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
Comment
Question by:TeleKawaru
  • 4
  • 3
  • 2
9 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9898019
Is just

Private Sub Picture1_Resize()

    Me.Width = Picture1.Width
    Me.Height = Picture1.Height
End Sub
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 400 total points
ID: 9898105
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
 
LVL 5

Expert Comment

by:mccainz2
ID: 9898183
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
 
LVL 5

Expert Comment

by:mccainz2
ID: 9898197
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 9898232
Try the second example. It's close enough.

8->

0
 

Author Comment

by:TeleKawaru
ID: 9898286
I used your second example EDDYKT but the window is still too small in both the horizontal and the vertical.
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9898294
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
 

Author Comment

by:TeleKawaru
ID: 9898303
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9898323
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now