Results 1 to 8 of 8

Thread: [RESOLVED] Adjust Form to Picture1 size?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Resolved [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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Adjust Form to Picture1 size?

    Quote Originally Posted by LaVolpe View Post
    First, a form should not really be larger than the screen.
    I think it's alright since the

    Quote Originally Posted by Jose_VB View Post
    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)

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adjust Form to Picture1 size?

    Quote Originally Posted by Bonnie West View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Adjust Form to Picture1 size?

    Quote Originally Posted by LaVolpe View Post
    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)

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adjust Form to Picture1 size?

    Quote Originally Posted by Bonnie West View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    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)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    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
  •  



Click Here to Expand Forum to Full Width