[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.
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
Re: Adjust Form to Picture1 size?
Quote:
Originally Posted by
LaVolpe
First, a form should not really be larger than the screen.
I think it's alright since the
Quote:
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
Re: Adjust Form to Picture1 size?
Quote:
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
Re: Adjust Form to Picture1 size?
Quote:
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.
Re: Adjust Form to Picture1 size?
Quote:
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
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. :bigyello:
Re: Adjust Form to Picture1 size?
Thanks to all. The solution has worked.