|
-
Apr 3rd, 2015, 04:33 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Adjust Form to Picture1 size?
I've a form with a picturebox control in it. The picturebox.autosize value is true, so after loading the jpg in picture1 I want to automatically resize form to adjust to the picture1 height and width.
How can I accomplish that?
Because I've tried different ways and I get another size of the form.
Just one note, form.borderstyle = fixed single.
form is mdichild.
-
Apr 3rd, 2015, 04:42 PM
#2
Re: Adjust Form to Picture1 size?
First, a form should not really be larger than the screen. So, if AutoSize is True, this could cause problems for you if a very large picture was loaded
This I think should work, with a few caveats
Code:
Dim cx as long, cy as long
' get width of the non-client area of the form
cx = Me.Width - ScaleX(Me.ScaleWidth, Me.ScaleMode, vbTwips)
' get height of the non-client area of the form
cy = Me.Height - ScaleY(Me.ScaleHeight, Me.ScaleMode, vbTwips)
' add the calculations to the picturebox size
cx = cx + ScaleX(Picture1.Width, Me.ScaleMode, vbTwips)
cy = cy + ScaleY(Picture1.Height, Me.ScaleMode, vbTwips)
Me.Move Me.Left, Me.Top, cx, cy
Caveats
1) Above assumes that the picturebox is aligned in top/left corner of the form. If any buffers are needed, they need to be added to the overall adjusted width/height & in twips
2) The sample code does not adjust the position of the form, that should be considered especially if form will grow and be clipped by right/bottom edges of the screen
3) If form has menus, then it is possible that resizing the form can wrap/unwrap any menus and the size calculation would be off
Another option would be to use the API AdjustWindowRect
Edited. Another note. A form cannot go below a set size, depending on its window style. So if the image is really small, the form may be larger than the picturebox
Last edited by LaVolpe; Apr 3rd, 2015 at 04:53 PM.
-
Apr 4th, 2015, 08:19 AM
#3
Re: Adjust Form to Picture1 size?
 Originally Posted by LaVolpe
First, a form should not really be larger than the screen.
I think it's alright since the
 Originally Posted by Jose_VB
form is mdichild.
Here's an enhancement of your code that takes into account the original padding around the PictureBox when resizing the Form:
Code:
Option Explicit 'In the MDIChild
Private m_Right As Single
Private m_Bottom As Single
Private m_NonClientWidth As Single
Private m_NonClientHeight As Single
Private Sub Form_Load()
m_NonClientWidth = Width - ScaleX(ScaleWidth, ScaleMode, vbTwips)
m_NonClientHeight = Height - ScaleY(ScaleHeight, ScaleMode, vbTwips)
With Picture1
m_Right = ScaleWidth - (.Left + .Width)
m_Bottom = ScaleHeight - (.Top + .Height)
End With
End Sub
Private Sub Picture1_Resize()
Dim NewClientWidth As Single, NewClientHeight As Single
With Picture1
NewClientWidth = ScaleX(.Left + .Width + m_Right, ScaleMode, vbTwips)
NewClientHeight = ScaleY(.Top + .Height + m_Bottom, ScaleMode, vbTwips)
End With
Move Left, Top, m_NonClientWidth + NewClientWidth, m_NonClientHeight + NewClientHeight
End Sub
Last edited by Bonnie West; Apr 4th, 2015 at 08:25 AM.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 4th, 2015, 09:22 AM
#4
Re: Adjust Form to Picture1 size?
 Originally Posted by Bonnie West
I think it's alright since the form is mdichild.
Well, yes and no. My comments were generic overall. However, a form has a max window size and some images may exceed that size. Just something for the OP to keep in mind. May even be possible to receive errors if image is too large and AutoRedraw is also true
Personally, I'd suggest, not using AutoSize and setting a min/max range for any loaded picture. If the image is outside the min/max, then scale to the min/max as needed. In any case, size the picturebox via code vs AutoSize & use VB's PaintPicture to render/scale the loaded image. Just my 2 cents
-
Apr 4th, 2015, 10:16 AM
#5
Re: Adjust Form to Picture1 size?
 Originally Posted by LaVolpe
However, a form has a max window size ...
My experiments suggests that doesn't seem to apply to MDIChild Forms whose BorderStyle is neither vbSizable nor vbSizableToolWindow. The OP's Form appears to be one such exception.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 4th, 2015, 10:46 AM
#6
Re: Adjust Form to Picture1 size?
 Originally Posted by Bonnie West
My experiments suggests that doesn't seem to apply to MDIChild Forms whose BorderStyle is neither vbSizable nor vbSizableToolWindow. The OP's Form appears to be one such exception.
In this case it does by proxy. A control definitely has a max size, therefore, since the form is dependent on the control size, it will also be restricted
-
Apr 4th, 2015, 12:10 PM
#7
Re: Adjust Form to Picture1 size?
After doing a few more tests, I've found out that there is indeed a limit to the maximum size of windows. In my system, child windows were restricted to a maximum width and/or height of 16,383 (&H3FFF) pixels. I believe that should be more than enough pixels for most images/pictures.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 5th, 2015, 10:00 AM
#8
Thread Starter
Hyperactive Member
Re: Adjust Form to Picture1 size?
Thanks to all. The solution has worked.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|