Solved

Properly Resizing an MDI window to autofit an image.

Posted on 2003-12-08
9
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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
 
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

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!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

688 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