Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

two simple question !

A vb program has three form, named main, left and right form. When
 the program is executed, all form should appear on the screen.
   
         - How to disable user to resize main form, while I want to select
           sizeable for form and still have minimize, maximize and close button ?

         - When minimal button is pressed in main form, other forms will also
            be minimized ? And when main form is maximized, all other forms
            should also be maximized ?


Thanks !    
0
sing_web
Asked:
sing_web
  • 3
  • 3
  • 3
  • +2
1 Solution
 
wsh2Commented:
Simplest and most straightforward approach would be to use a MDI form to hold the three forms. Assuming the forms are equal size.. you can issue Tile Horizontal commands to always keep the three properly aligned and sized.. <smile>.
0
 
Brendt HessSenior DBACommented:
1 - how to disable resizing.

Add these constants to your project - set the values to the height & width of your form:

Const LockedFormWidth = 500
Const LockedFormHeight = 400


In the Form_Resize event

Sub Form1_Resize()
  If Me.Height <> LockedFormHeight Or Me.Width<>LockedFormWidth Then
    Me.Height=LockedFormHeight
    Me.Width=LockedFormWidth
  End If
End Sub

You can't actually stop them from resizing the form, but you can instantly set it back to the correct width and height.

2 - Minimize and maximize forms in sync.

Add this global variable into your project:

Global LastWindowState as Integer


Add this to your Form_Load event:

   LastWindowState = -1   ' Not yet loaded window


More code in the Form_Resize event:

Sub Form1_Resize()

  If LastWindowState = vbMinimized and Me.WindowState <> vbMinimized Then
    Form2.WindowState = vbNormal
    Form3.WindowState = vbNormal
  End If

  If Me.WindowState = vbNormal Then   ' Do this if not Minimized or Maximized
    If Me.Height <> LockedFormHeight Or Me.Width<>LockedFormWidth Then
      Me.Height=LockedFormHeight
      Me.Width=LockedFormWidth
    End If
  Else     ' Minimized or Maximized
    If Me.WindowState = vbMinimized Then   ' Window minimized
      Form2.WindowState = vbMinimized
      Form3.WindowState = vbMinimized
    End If
  End If

  LastWindowState = Me.WindowState

End Sub

So, if the user Maximizes Form1, then Form2 and Form3 are returned to normal state.  If Form1 is minimized, form2 and form3 are minimized.  If form1 is restored to normal, then form2 and form3 are returned to normal.  If form1 is resized, it resizes itself back to the proper size.
0
 
Erick37Commented:
If your form has a fixed border, you can change the MinButton and MaxButton properties True in design time.

In the Resize event of the main form, you can set the WindowState of the other forms as well:

Option Explicit

Private Sub Form_Load()
    Form2.Show
End Sub

Private Sub Form_Resize()
    Form2.WindowState = Me.WindowState
End Sub
0
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.

 
Erick37Commented:
Actually, you can stop the user from resizing the form even if the BorderStyle is set to Sizable:

"HOWTO: Limit a Window's Minimum and Maximum Size"

http://support.microsoft.com/support/kb/articles/Q185/7/33.ASP
0
 
sing_webAuthor Commented:
bhess1 :

I think this code will not work because I had this idea before.

Sub Form1_Resize()
   If Me.Height <> LockedFormHeight Or       Me.Width<>LockedFormWidth Then
          Me.Height=LockedFormHeight
          Me.Width=LockedFormWidth
   End If
 End Sub


when you try to minimize it, you will get error.
0
 
sing_webAuthor Commented:
bhee1 :

your second answer should put in the form_activate, right ?
0
 
Erick37Commented:
Why not set the form to Fixed-Single, and enable the min and max buttons?
0
 
Brendt HessSenior DBACommented:
"when you try to minimize it, you will get error."

That's why you check the WindowState, and only reset the size if it is vbNormal.

This code works best in the form_resize event.  You could add the resizing code to form_load, but form_resize is called the first time you show a form as well.
0
 
ArkCommented:
Hi
Your question is not simple, and Complete solytion require subclassing, because Form_resize event occure AFTER resizing, so you'll see  changing size. Anyway, here is a code
'--bas module code
Type POINTAPI
     x As Long
     y As Long
End Type
Type MINMAXINFO
     ptReserved As POINTAPI
     ptMaxSize As POINTAPI
     ptMaxPosition As POINTAPI
     ptMinTrackSize As POINTAPI
     ptMaxTrackSize As POINTAPI
End Type

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Const WM_SYSCOMMAND = &H112
Const SC_MINIMIZE = &HF020&
Const SC_MAXIMIZE = &HF030&
Const SC_CLOSE = &HF060&

Public Const GWL_WNDPROC = (-4)
Const GWL_STYLE = (-16)
Const WS_MAXIMIZE = &H1000000
Const WM_GETMINMAXINFO = &H24

Public OldProc As Long
Public StartWidth As Long
Public StartHeight As Long
Dim MMinfo As MINMAXINFO

Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Select Case wMsg
    Case WM_GETMINMAXINFO
         If GetWindowLong(hwnd, GWL_STYLE) And WS_MAXIMIZE Then
            WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
            Exit Function
         End If
         CopyMemory MMinfo, ByVal lParam, LenB(MMinfo)
' Max/Min size when tracking
         MMinfo.ptMinTrackSize.x = StartWidth
         MMinfo.ptMinTrackSize.y = StartHeight
         MMinfo.ptMaxTrackSize.x = StartWidth
         MMinfo.ptMaxTrackSize.y = StartHeight
         CopyMemory ByVal lParam, MMinfo, LenB(MMinfo)
         Exit Function
    Case WM_SYSCOMMAND
         Select Case wParam
                Case SC_MINIMIZE
'All your code here to minimize other windows
                Case SC_CLOSE
                     'Close code
                Case SC_MAXIMIZE
                     'Max_Code
'All your code here to maximize other windows
         End Select
    End Select
    WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End Function

'--Form code--
Private Sub Form_Load()
 StartWidth = Width / Screen.TwipsPerPixelX
 StartHeight = Height / Screen.TwipsPerPixelY
 OldProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
 SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    SetWindowLong Me.hwnd, GWL_WNDPROC, OldProc
End Sub

'Don't stop this code from IDE - it cause GPF
' I thing it's a complete solution, but post it as comment because you can choose more easy (but not complite) way from previous code
Cheers
0
 
sing_webAuthor Commented:
bhee1 :

your second answer should put in the form_activate, right ?
0
 
Brendt HessSenior DBACommented:
No, the second answer also goes into the resize event
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now