+ Reply to Thread
Results 1 to 12 of 12

close/minimise form control

Hybrid View

  1. #1
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    close/minimise form control

    Hi

    I have a button that opens a user form, the form has the ability to be minimised to the task bar. I would like to get the button to either restore the form the task bar or open a new form if it isn't open all ready. currently the button will open a new form and this will confuse the end user if they have one open already but minimised to the task bar!

    Can anyone help?

    The form name is ControlPanel

    many thanks.

    nods
    Last edited by nods; 11-11-2010 at 12:41 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close/minimise form control

    Hello nods,

    You should post your workbook. We can then see the code you are using and the layout of your project. The Devil is in the details.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    I have tried uploading it a few times and I keep getting a database error message.

    It is a 3.5meg zip file.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: close/minimise form control

    What kind of userform allows minimization? In 2007, I couldn't find a way to minimize a userform.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    If you paste this code into the form it should add a minimize button.

    (this was taken from an example posted on this site)




    Option Explicit
    
    'API functions
    Private Declare Function GetWindowLong Lib "User32" _
                                           Alias "GetWindowLongA" _
                                           (ByVal hwnd As Long, _
                                            ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "User32" _
                                           Alias "SetWindowLongA" _
                                           (ByVal hwnd As Long, _
                                            ByVal nIndex As Long, _
                                            ByVal dwNewLong As Long) As Long
    Private Declare Function SetWindowPos Lib "User32" _
                                          (ByVal hwnd As Long, _
                                           ByVal hWndInsertAfter As Long, _
                                           ByVal X As Long, _
                                           ByVal y As Long, _
                                           ByVal cx As Long, _
                                           ByVal cy As Long, _
                                           ByVal wFlags As Long) As Long
    Private Declare Function FindWindow Lib "User32" _
                                        Alias "FindWindowA" _
                                        (ByVal lpClassName As String, _
                                         ByVal lpWindowName As String) As Long
    Private Declare Function GetActiveWindow Lib "user32.dll" _
                                             () As Long
    Private Declare Function SendMessage Lib "User32" _
                                         Alias "SendMessageA" _
                                         (ByVal hwnd As Long, _
                                          ByVal wMsg As Long, _
                                          ByVal wParam As Long, _
                                          lParam As Any) As Long
    Private Declare Function DrawMenuBar Lib "User32" _
                                         (ByVal hwnd As Long) As Long
    
    
    'Constants
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const GWL_EXSTYLE = (-20)
    Private Const HWND_TOP = 0
    Private Const SWP_NOACTIVATE = &H10
    Private Const SWP_HIDEWINDOW = &H80
    Private Const SWP_SHOWWINDOW = &H40
    Private Const WS_EX_APPWINDOW = &H40000
    Private Const GWL_STYLE = (-16)
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const SWP_FRAMECHANGED = &H20
    Private Const WM_SETICON = &H80
    Private Const ICON_SMALL = 0&
    Private Const ICON_BIG = 1&
    
    
    
    
    
    Private Sub UserForm_Activate()
        AddMinimiseButton   'Add a Minimize button to Userform
        AppTasklist Me    'Add this userform into the Task bar
    End Sub
    
    
    
    
    
    Private Sub AddMinimiseButton()
    '//Add a Minimize button to Userform
        Dim hwnd As Long
        hwnd = GetActiveWindow
        Call SetWindowLong(hwnd, GWL_STYLE, _
                           GetWindowLong(hwnd, GWL_STYLE) Or _
                           WS_MINIMIZEBOX)
        Call SetWindowPos(hwnd, 0, 0, 0, 0, 0, _
                          SWP_FRAMECHANGED Or _
                          SWP_NOMOVE Or _
                          SWP_NOSIZE)
    End Sub
    
    
    Private Sub AppTasklist(myForm)
    'Add this userform into the Task bar
        Dim WStyle As Long
        Dim Result As Long
        Dim hwnd As Long
    
        hwnd = FindWindow(vbNullString, myForm.Caption)
        WStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
        WStyle = WStyle Or WS_EX_APPWINDOW
        Result = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, _
                              SWP_NOMOVE Or _
                              SWP_NOSIZE Or _
                              SWP_NOACTIVATE Or _
                              SWP_HIDEWINDOW)
        Result = SetWindowLong(hwnd, GWL_EXSTYLE, WStyle)
        Result = SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, _
                              SWP_NOMOVE Or _
                              SWP_NOSIZE Or _
                              SWP_NOACTIVATE Or _
                              SWP_SHOWWINDOW)
    End Sub

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close/minimise form control

    Hello nods,

    That is crazy. Here is module to allow you to place the minimize, and restore buttons onto your UserForm. NOTE: Set the UserForm ShowModal property to False.
    'Written: October 07, 2007
    'Author:  Leith Ross
    'Summary: Add Minimize, and Maximize/Restore buttons to a VBA UserForm
    
    Private Const GWL_STYLE As Long = -16
    Public Const MIN_BOX As Long = &H20000
    Public Const MAX_BOX As Long = &H10000
    
    Const SC_CLOSE As Long = &HF060
    Const SC_MAXIMIZE As Long = &HF030
    Const SC_MINIMIZE As Long = &HF020
    Const SC_RESTORE As Long = &HF120
    
    Private Declare Function GetWindowLong _
      Lib "user32.dll" _
       Alias "GetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long) As Long
                   
     Private Declare Function SetWindowLong _
      Lib "user32.dll" _
       Alias "SetWindowLongA" _
        (ByVal hwnd As Long, _
         ByVal nIndex As Long, _
         ByVal dwNewLong As Long) As Long
         
    'Redraw the Icons on the Window's Title Bar
     Private Declare Function DrawMenuBar _
      Lib "user32.dll" _
       (ByVal hwnd As Long) As Long
    
    'Returns the Window Handle of the Window accepting input
     Private Declare Function GetForegroundWindow _
      Lib "user32.dll" () As Long
    
    Public Sub AddToForm(ByVal Box_Type As Long)
    
     Dim BitMask As Long
     Dim Window_Handle As Long
     Dim WindowStyle As Long
     Dim Ret As Long
    
       If Box_Type = MIN_BOX Or Box_Type = MAX_BOX Then
          Window_Handle = GetForegroundWindow()
      
           WindowStyle = GetWindowLong(Window_Handle, GWL_STYLE)
           BitMask = WindowStyle Or Box_Type
      
          Ret = SetWindowLong(Window_Handle, GWL_STYLE, BitMask)
          Ret = DrawMenuBar(Window_Handle)
       End If
    
    End Sub

    How to Call the Macro
    Private Sub UserForm_Activate()
      AddToForm MIN_BOX
      AddToForm MAX_BOX
    End Sub


    EDIT: Changed UserForm Event from Initialize to Activate.
    Last edited by Leith Ross; 11-06-2010 at 03:08 AM.

  7. #7
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    Thanks leith that worked a treat, wish I had the code earlier.

    But back to my original problem

    what i want to achieve when the button is pressed but I keep getting a syntax error.

    If the form is minimized then restore else open a new form.

    code I have so far



    Sub ControlPanel()
    
    Dim frm As ControlPanel
    
    If ControlPanel.WindowState = 1
    
     
    Then ControlPanel.WindowState = 0
    
    
    Else: Set frm = New ControlPanel
    frm.Show
    End Sub

    do you have any ideas?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close/minimise form control

    Hello nods,

    If you want to minimize and restore the UserForm from a button on the worksheet then this makes it easier. Remove the previous code module and use this code.
    'Written: November 06,2010
    'Author:  Leith Ross
    'Summary: Minimize and Restore a UserForm from a Excel Button.
    
    'Returns the Window Handle of the Window accepting input
     Public Declare Function GetForegroundWindow _
      Lib "user32.dll" () As Long
    
    'API Function to Change how Window is Displayed
     Private Declare Function ShowWindow _
      Lib "user32.dll" _
       (ByVal hWnd As Long, _
        ByVal nCmdShow As Long) As Long
      
    'Constants for ShowWindow (nCmdShow)
     Const SW_HIDE = 0
     Const SW_HIDDEN As Long = 0
     Const SW_NORMAL As Long = 1
     Const SW_SHOWNORMAL = 1
     Const SW_MINIMIZED As Long = 2
     Const SW_SHOWMINIMIZED = 2
     Const SW_MAXIMIZED As Long = 3
     Const SW_SHOWMAXIMIZED = 3
     Const SW_NOTACTIVE As Long = 4
     Const SW_SHOWNOACTIVATE = 4
     Const SW_UNHIDDEN As Long = 5
     Const SW_MINWITHFOCUS As Long = 6
     Const SW_MINNOTACTIVE As Long = 7
     Const SW_SHOWMINNOACTIVE = 7
     Const SW_SHOWNA = 8
     Const SW_RESTORE As Long = 9
     Const SW_SHOWDEFAULT = 10
     Const SW_MAX = 10
    
    
    Public hWndFrm As Long
    Public Btn As Object
    Public FrmLoaded As Object
    
    Sub ShowRestoreForm()
    
      Dim Frm As MSForms.UserForm
      Dim I As Integer
      Dim FormName As String
      
        FormName = "ControlPanel"
        
        Set Btn = ActiveSheet.Buttons(Application.Caller)
       
       'Check if Form is loaded
        If FrmLoaded Is Nothing Then
          UserForms.Add(FormName).Show
          hWndFrm = GetForegroundWindow
          Btn.Caption = "Minimize"
          Exit Sub
        End If
        
       'Change the button caption to show its function
        Select Case ActiveSheet.Buttons(Application.Caller).Caption
          Case Is = "Minimize"
            ShowWindow hWndFrm, SW_SHOWMINNOACTIVE
            Btn.Caption = "Restore"
          Case Is = "Restore"
            ShowWindow hWndFrm, SW_RESTORE
            Btn.Caption = "Minimize"
        End Select
      
    End Sub

    UserForm Code Neeeded
    Private Sub UserForm_Activate()
        TestForm.Tag = GetForegroundWindow
        Set FrmLoaded = Me
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        Btn.Caption = "Show Form"
        Set FrmLoaded = Nothing
    End Sub

  9. #9
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    Thanks Leith That solved the problem.

  10. #10
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    Hi leith

    I was using Excel 2010 when I cread the module for the Min/Max controls using the code you gave me in this thread and it worked fine. I just checked in 2007 and the min button closes the form and in 2003 the controls dont show at all unless I open the form from the VBA editor.

    Any Ideas on what the problem could be or how to solve it? The majority of the people using the work book will be uisng excel 2003.

    Many thanks

    Code below.

    Private Sub UserForm_Activate()
      AddToForm MIN_BOX
      AddToForm MAX_BOX
    End Sub

    Private Const GWL_STYLE As Long = -16
    Public Const MIN_BOX As Long = &H20000
    Public Const MAX_BOX As Long = &H10000
    
    Const SC_CLOSE As Long = &HF060
    Const SC_MAXIMIZE As Long = &HF030
    Const SC_MINIMIZE As Long = &HF020
    Const SC_RESTORE As Long = &HF120
    
    Private Declare Function GetWindowLong _
      Lib "user32.dll" _
       Alias "GetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long) As Long
                   
     Private Declare Function SetWindowLong _
      Lib "user32.dll" _
       Alias "SetWindowLongA" _
        (ByVal hwnd As Long, _
         ByVal nIndex As Long, _
         ByVal dwNewLong As Long) As Long
         
    'Redraw the Icons on the Window's Title Bar
     Private Declare Function DrawMenuBar _
      Lib "user32.dll" _
       (ByVal hwnd As Long) As Long
    
    'Returns the Window Handle of the Window accepting input
     Private Declare Function GetForegroundWindow _
      Lib "user32.dll" () As Long
    
    Public Sub AddToForm(ByVal Box_Type As Long)
    
     Dim BitMask As Long
     Dim Window_Handle As Long
     Dim WindowStyle As Long
     Dim Ret As Long
    
       If Box_Type = MIN_BOX Or Box_Type = MAX_BOX Then
          Window_Handle = GetForegroundWindow()
      
           WindowStyle = GetWindowLong(Window_Handle, GWL_STYLE)
           BitMask = WindowStyle Or Box_Type
      
          Ret = SetWindowLong(Window_Handle, GWL_STYLE, BitMask)
          Ret = DrawMenuBar(Window_Handle)
       End If
    
    End Sub

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: close/minimise form control

    Hello nods,

    I can't say what the cause of the problem is. I originally wrote the code using Excel 2000 on Windows 2000. It has run fine on my Windows 2003 XP machine with no problems since then.

  12. #12
    Registered User
    Join Date
    10-06-2010
    Location
    Dunstable,England
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: close/minimise form control

    Thanks Leith.

    I have had a few compatibilty problems when doing stuff in excell 2010.

    Back to 2003 me thinks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1