Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Close Button on VB Form

Status
Not open for further replies.

RangerFan

MIS
May 4, 2000
61
0
0
US
How do you disable the clos button on a vb form? We do not want to turn off the Form_unload event (i.e., we don't want to set the Cancel = True property to disable the close button)

Is there any way to disable this or just have it not appear on the form by altering its style?
 
This solution comes by way of VBaccelerator:

Add the following code to the project's form:
[tt]
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000

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 Enum ESetWindowPosStyles
[tab]SWP_SHOWWINDOW = &H40
[tab]SWP_HIDEWINDOW = &H80
[tab]SWP_FRAMECHANGED = &H20 ' The frame changed: send
[tab]WM_NCCALCSIZE
[tab]SWP_NOACTIVATE = &H10
[tab]SWP_NOCOPYBITS = &H100
[tab]SWP_NOMOVE = &H2
[tab]SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering
[tab]SWP_NOREDRAW = &H8
[tab]SWP_NOREPOSITION = SWP_NOOWNERZORDER
[tab]SWP_NOSIZE = &H1
[tab]SWP_NOZORDER = &H4
[tab]SWP_DRAWFRAME = SWP_FRAMECHANGED
[tab]HWND_NOTOPMOST = -2
End Enum

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
[tab]Left As Long
[tab]Top As Long
[tab]Right As Long
[tab]Bottom As Long
End Type

Private Function ShowTitleBar(ByVal bState As Boolean)
Dim lStyle As Long
Dim tR As RECT

' Get the window's position:
GetWindowRect Me.hwnd, tR

' Modify whether title bar will be visible:
lStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
If (bState) Then
[tab]Me.Caption = Me.Tag
[tab]If Me.ControlBox Then
[tab][tab]lStyle = lStyle Or WS_SYSMENU
[tab]End If
[tab]If Me.MaxButton Then
[tab][tab]lStyle = lStyle Or WS_MAXIMIZEBOX
[tab]End If
[tab]If Me.MinButton Then
[tab][tab]lStyle = lStyle Or WS_MINIMIZEBOX
[tab]End If
[tab]If Me.Caption <> &quot;&quot; Then
[tab][tab]lStyle = lStyle Or WS_CAPTION
[tab]End If
Else
[tab]Me.Tag = Me.Caption
[tab]Me.Caption = &quot;&quot;
[tab]lStyle = lStyle And Not WS_SYSMENU
[tab]lStyle = lStyle And Not WS_MAXIMIZEBOX
[tab]lStyle = lStyle And Not WS_MINIMIZEBOX
[tab]lStyle = lStyle And Not WS_CAPTION
End If
SetWindowLong Me.hwnd, GWL_STYLE, lStyle

' Ensure the style takes and make the window the
' same size, regardless that the title bar etc
' is now a different size:
SetWindowPos Me.hwnd, 0, tR.Left, tR.Top, tR.Right - tR.Left, tR.Bottom - tR.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED
Me.Refresh

' Ensure that your resize code is fired, as the client area
' has changed:
Form_Resize

End Function

'To try out the hiding and showing the title bar, add a CheckBox to the project's form. Set the check box's Value property to 1 (Checked). Then put the following code under the Check box's click event:
Private Sub Check1_Click()
If (Check1.Value = Checked) Then
[tab]ShowTitleBar True
Else
[tab]ShowTitleBar False
End If
End Sub

'When you click on the check box, the form's titlebar will be alternately hidden and shown.
[/tt]
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Not sure which version you are using but VB6 provides the QueryUnload Event that fires prior to any form or application closing. You can check the cause of the Form closing and determine if the Form Close button has been pressed and subsequently cancel the close if it was:
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
  If UnloadMode = vbFormControlMenu Then
    Cancel = True
  End If
    
End Sub
 
Alt255 is correct... If you want to 'grey' the close button, you will have to modify the forms style in its window class.

Read Charles Petzold's book on programming windows for information about window classes. His examples are in C, but you could do this in VB as Alt255 has shown. In the code above, Alt255 simply modifies the window type and calls SetWindowLong to change the style...

Tom
 
You can also remove it from the system menu, and this works fine!

Try this code, courtesy of John Percival: (I found this at
Declarations

Paste this into the declaration section:

Declare Function GetSystemMenu Lib &quot;user32&quot; _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long

Declare Function GetMenuItemCount Lib &quot;user32&quot; _
(ByVal hMenu As Long) As Long

Declare Function DrawMenuBar Lib &quot;user32&quot; _
(ByVal hwnd As Long) As Long

Declare Function RemoveMenu Lib &quot;user32&quot; _
(ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400&
Public Const MF_REMOVE = &H1000&
Form Code

Then paste this into the form:

Private Sub Form_Load()
Dim hSysMenu As Long
Dim nCnt As Long

'First, show the form
Me.Show

'Get handle to our form's system menu
'(Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
'Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)

If nCnt Then

'Menu count is based on 0 (0, 1, 2, 3...)

RemoveMenu hSysMenu, nCnt - 1, _
MF_BYPOSITION Or MF_REMOVE

RemoveMenu hSysMenu, nCnt - 2, _
MF_BYPOSITION Or MF_REMOVE 'Remove the seperator

DrawMenuBar Me.hwnd
'Force caption bar's refresh. Disabling X button

Me.Caption = &quot;Try to close me!&quot;
End If
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top