CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2000
    Posts
    140

    Form Close Button

    Hi

    Like to check if it is possible to disable the VB Form top right hand corner close button as in my application , i have a form to prompt user to select. I need to disable it so as to prevent user from closing the form accidentally .

    Any eg of illustration is appreciated. Thanks

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Form Close Button

    Quote Originally Posted by darth vader
    Hi

    Like to check if it is possible to disable the VB Form top right hand corner close button as in my application , i have a form to prompt user to select. I need to disable it so as to prevent user from closing the form accidentally .

    Any eg of illustration is appreciated. Thanks
    There are multiple ways of doing this. The easiest would be to use the QueryUnload Event of the Form itself and check how the form is being closed.
    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    	'prevents use of close button
    	If UnloadMode = vbFormControlMenu Then
    		Cancel = True
    	End If
    End Sub
    However this does not show the button as disabled. User will be able to click the button, but the Form won't unload itself.

    More Complex way would be to use API
    Code:
    Private Const MF_BYPOSITION = &H400
    Private Const MF_REMOVE = &H1000 'API Functions that need to be called to Disable the Close Button on the Form Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Sub Form_Initialize() Dim hMenu As Long Dim menuItemCount As Long 'Get the Handle of the Menu hMenu = GetSystemMenu(Me.hWnd, 0) If hMenu Then 'Get the Number of Items present in the Menu menuItemCount = GetMenuItemCount(hMenu) 'Remove the system menu Close menu item. 'The menu item is 0-based, so the last 'item on the menu is menuItemCount - 1 Call RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE Or MF_BYPOSITION) 'Remove the Separator line from the Menu Call RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE Or MF_BYPOSITION) 'Redraw the menu, this will refresh the Title Bar and disable the X button Call DrawMenuBar(hWnd) End If End Sub
    This will disable the Close button on the Form.

  3. #3
    Join Date
    Nov 2005
    Posts
    36

    Re: Form Close Button

    There is a way to do this from form properties too.

    I don't have it infront of me right now, but it's something like:

    You can disable the control box of the form in the properties window, and that either removes everything up there, or just the min/max buttons.

    There is also a way to gray it out... I will go play around and see what I come up with

    Just seems simpler than using an API.

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Form Close Button

    Quote Originally Posted by Buddy008
    There is a way to do this from form properties too.

    I don't have it infront of me right now, but it's something like:

    You can disable the control box of the form in the properties window, and that either removes everything up there, or just the min/max buttons.
    Make the ControlBox property of the Form False. But this will remove Maximize and Minimize buttons too from the Form.

    Quote Originally Posted by Buddy008
    There is also a way to gray it out... I will go play around and see what I come up with

    Just seems simpler than using an API.
    API is the way that I have used so far to Disable just the Close(X) button on the Form.

  5. #5
    Join Date
    Jul 2004
    Posts
    6

    Re: Form Close Button

    Posted - 06/30/2004 : 08:15:10 AM


    --------------------------------------------------------------------------------

    Hi Guys

    It might be similar to the the privious code posted, this code remove the close button.

    I fund this code in a book, and it should do the trick.
    The code should be placed in the userforms code module.

    'Hide a UserForm's Close Button
    'Find the userform's Window
    Private Declare Function FindWindow Lib "user32" _
    Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    'Get the current window style
    Private Declare Function GetWindowLong Lib "user32" _
    Alias "GetWindowLongA" ( _
    ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long

    'Set the new window style
    Private Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" ( _
    ByVal hWnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

    Const GWL_STYLE = -16
    Const WS_SYSMENU = &H80000

    Private Sub UserForm_Initialize()
    Dim hWnd As Long, lStyle As Long

    'Which type of userform
    If Val(Application.Version) >= 9 Then
    hWnd = FindWindow("ThunderDFrame", Me.Caption)
    Else
    hWnd = FindWindow("ThunderXFrame", Me.Caption)
    End If

    'Get the current window style and turn off the Close button
    lStyle = GetWindowLong(hWnd, GWL_STYLE)
    SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
    End Sub

    Cheers
    Karsten

  6. #6
    Join Date
    Apr 2005
    Location
    New Amsterdam
    Posts
    36

    Re: Form Close Button

    You may find FormBdr sample project by Karl E. Perterson very usefull.
    There is a wrapper class that does all this tricks and it's quite easy to use.

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

Featured