[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Properly Resizing an MDI window to autofit an image.

Posted on 2003-12-08
9
Medium Priority
?
382 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 1200 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
Industry Leaders: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

649 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