Solved

Properly Resizing an MDI window to autofit an image.

Posted on 2003-12-08
9
374 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
Try the second example. It's close enough.

8->

0
 

Author Comment

by:TeleKawaru
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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

10 Experts available now in Live!

Get 1:1 Help Now