[RESOLVED] Manual form maximize code.-VBForums
Results 1 to 15 of 15

Thread: [RESOLVED] Manual form maximize code.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Resolved [RESOLVED] Manual form maximize code.

    There is a borderless form. There is also a picture box. I would like that when the user double clicks the button, it will maximize the form. I don't want to use me.windowstate = vbmaximized because it created a lot of problems. I want to manualy maximize the form using:
    Code:
    me.top = 0
    me.left = 0
    me.width = screen.width
    me.height = screen.height
    So far so good. But what about restoring the form to its normal sizes? I've tried a lot of things, but none worked.
    Thanks.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,142

    Re: Manual form maximize code.

    No idea what "tried a lot of things" means. Did you try setting the position and size to the correct values?

    If you want to be able to restore the size and position to the original then you need to save those values and then use them when you want to restore.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Manual form maximize code.

    That's exactly what I want to do. Save the values before the form mximizes and then restore to those values. But it doesn't work with variables. I think I'll have to save them to a text file or in the registry, but I don't want to do that. Also, I want that all this happens when the user double clicks the picturebox. Here is where I faced all of the problems. Those values of the form's sizes that were saved before the form got maximzed, wouldn't reamain like that, they would change because the picture has been double clicked. And it got very confusing. Also tried using modules to save the values, but the values would always change to 0 for left,0 for top, 1366 for screen.width, 768 for screen.height.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,142

    Re: Manual form maximize code.

    It should work fine with variables so long as you use them properly. Create a few form level variables lets call them OriginalTop OriginalLeft, OriginalHeight, OriginalWidth set these values to the sizes of the form either by hard coding them to the values you are using to set the initial size of your form or via code when the form loads, not when someone clicks on the picture box. These values should never change once they are set so there should not be any issues at all.

    If you still need help you need to show some code so we can see what you are doing wrong.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Manual form maximize code.

    The form is manualy resizable and I can't restore the form to the initial size because that changes according to how the user resizes it.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,142

    Re: Manual form maximize code.

    Then in that case you need to save the value when the user resizes it I suppose. This should be very simple. I really have no idea what you may be doing wrong without seeing what you are doing.

  7. #7
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Manual form maximize code.

    you have to set the variables once the form is loaded and then do not replace the values of the variables.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Manual form maximize code.

    Here is the code I am using. Maxed, is a boolean variable which determines whether the form is maximized(top=0,left=0,width = screen.width, height = screen.height)
    tp is for top,lf is for left, wd is for width, he is for height.
    Code:
    Private Sub ttlBar_DblClick()
    Dim tp, lf, wd, he As Integer
    tp = Me.Top
    lf = Me.Left
    wd = Me.Width
    he = Me.Height
    Dim maxed As Boolean
    If maxed = False Then
    Me.Top = 0
    Me.Left = 0
    Me.Width = Screen.Width
    Me.Height = Screen.Height
    maxed = True
    ElseIf maxed = True Then
    Me.Top = tp
    Me.Left = lf
    Me.Width = wd
    Me.Height = he
    maxed = False
    End If
    End Sub

  9. #9
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Manual form maximize code.

    well first of all you dim your variables in your sub which erases every time... add them as Private for you form. Put it before any sub/functions, it should be the first line of code you can see in your project. Also you will have to make sure not to change it when you double click on your titlebar or whatever it is

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Manual form maximize code.

    I already tried adding them as private. But how are these variables going to read the values of the form when the form resizes?
    I have to use some sub such as form paint or form resize, but when the form maximizes, the variables will store the values of the maximized form and not those of the previous state form. I could use a timer, but that's out of discussion because that would add a lot of flicker to my form.
    How does Windows store the values a form before it is maximized and after it is maximized? When you double click a window on the top bar, it maximizes. And when you double click it again, it goes to it's previous state. How are these values stored by Windows?

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

    Re: Manual form maximize code.

    Try this:

    Code:
    Option Explicit
    
    Private Lf As Single 'Me.Left, Me.Top, Me.Width and Me.Height are all Single
    Private Tp As Single
    Private Wd As Single
    Private He As Single
    
    Private Sub Form_Load()
        Lf = Left
        Tp = Top
        Wd = Width
        He = Height
    End Sub
    
    Private Sub ttlBar_DblClick()
        If Left = 0! Then
            If Top = 0! Then
                If Width = Screen.Width Then
                    If Height = Screen.Height Then 'Short-circuit preceding tests
                        Move Lf, Tp, Wd, He
                        Exit Sub
                    End If
                End If
            End If
        End If
    
        Lf = Left
        Tp = Top
        Wd = Width
        He = Height
        Move 0!, 0!, Screen.Width, Screen.Height
    End Sub
    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)

  12. #12
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Manual form maximize code.

    Here is an example of what you want.
    If you double click the form it will maximize, if maximized it will make it normal.

    Add this to a new project (everything left to default)
    Code:
    Option Explicit
    
    Private Type FormPosition
      Left As Integer
      Top As Integer
      Width As Integer
      Height As Integer
    End Type
      
    Private FormPos As FormPosition
    
    
    
    Private Sub Form_DblClick()
      If WindowState = vbMaximized Then
        WindowState = vbNormal
        With FormPos
          Left = .Left
          Top = .Top
          Width = .Width
          Height = .Height
        End With
      Else
        WindowState = vbMaximized
      End If
    End Sub
    
    Private Sub Form_Resize()
      If Not WindowState = vbMinimized Then
        With FormPos
          .Left = Left
          .Top = Top
          .Width = Width
          .Height = Height
        End With
      End If
    End Sub
    Edit:
    I just noticed the form had to be borderless...
    It will still work if you put borderless though

    The form is manualy resizable and I can't restore the form to the initial size because that changes according to how the user resizes it.
    I went off of this... sorry Bonnie's code probably does what you are asking.
    Last edited by Max187Boucher; Mar 24th, 2013 at 12:41 PM.

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,142

    Re: Manual form maximize code.

    You have a few issues with the code shown here
    Code:
    Private Sub ttlBar_DblClick()
    Dim tp, lf, wd, he As Integer
    tp = Me.Top
    lf = Me.Left
    wd = Me.Width
    he = Me.Height
    Dim maxed As Boolean
    If maxed = False Then
        Me.Top = 0
        Me.Left = 0
        Me.Width = Screen.Width
        Me.Height = Screen.Height
        maxed = True
    ElseIf maxed = True Then
        Me.Top = tp
        Me.Left = lf
        Me.Width = wd
        Me.Height = he
        maxed = False
    End If
    End
    First of all only one of those vars will be correctly dimmed as an Integer
    Second you are setting the values = to the current form size when you enter the routine so you are not remembering the former size but getting the current size.
    You are setting the size = the current size if not max so you are basically not changing anything in this case.
    Third you are dimming Maxed then checking its value which will always be false because you have dimmed a local var and not assigned a value to it.

    What you need to do is either have a preset value in your load or similar as shown by Bonnie above and use that or you need to place code in the re-size event that first checks to see if the form is maxed and if not then record the value of the size and position. This needs to be stored in a form level variable that can be accessed from other subs in order to retain the value and restore it later.

  14. #14
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Manual form maximize code.

    Quote Originally Posted by Bonnie West View Post
    Try this:

    Code:
    Option Explicit
    
    Private Lf As Single 'Me.Left, Me.Top, Me.Width and Me.Height are all Single
    Private Tp As Single
    Private Wd As Single
    Private He As Single
    
    Private Sub Form_Load()
        Lf = Left
        Tp = Top
        Wd = Width
        He = Height
    End Sub
    
    Private Sub ttlBar_DblClick()
        If Left = 0! Then
            If Top = 0! Then
                If Width = Screen.Width Then
                    If Height = Screen.Height Then 'Short-circuit preceding tests
                        Move Lf, Tp, Wd, He
                        Exit Sub
                    End If
                End If
            End If
        End If
    
        Lf = Left
        Tp = Top
        Wd = Width
        He = Height
        Move 0!, 0!, Screen.Width, Screen.Height
    End Sub
    I would not use so many If statements (not neccessary), when your form is moved to either 0 top or 0 left then it will not work properly. My example works also with borderless form

    I added the move form code also

    Code:
    Option Explicit
    
    Private Type FormPosition
      Left As Integer
      Top As Integer
      Width As Integer
      Height As Integer
    End Type
      
    Private FormPos As FormPosition
    Private LMouseDown As Boolean
    Private StartX As Integer
    Private StartY As Integer
    
    
    Private Sub ttlBar_DblClick()
      If WindowState = vbMaximized Then
        WindowState = vbNormal
        With FormPos
          Left = .Left
          Top = .Top
          Width = .Width
          Height = .Height
        End With
      Else
        WindowState = vbMaximized
      End If
    End Sub
    
    Private Sub Form_Resize()
      If Not WindowState = vbMinimized Then
        With FormPos
          .Left = Left
          .Top = Top
          .Width = Width
          .Height = Height
        End With
      End If
    End Sub
    
    Private Sub ttlBar_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If Button = vbKeyLButton Then
        LMouseDown = True
        StartX = X
        StartY = Y
      End If
    End Sub
    
    Private Sub ttlBar_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If LMouseDown Then Me.Move Left + (X - StartX), Top + (Y - StartY)
    End Sub
    
    Private Sub ttlBar_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
      LMouseDown = False
    End Sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

    Re: Manual form maximize code.

    Thank you @BonnieWest. It works perfectly. No problems while resizing or form_paint. I didn't know that height,width,top and left were to declare as singles.
    @DataMiser I got confused with C++ where you can declare them in one line. This happens often.
    Thank you everybody for your help. I really appreciate it.

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.