Hiding a form's Close ControlBox button

G

Guest

Is there a "best" technique for hiding a form's Close ControlBox button (i.e., the button with an "X" in the top-right corner of a form) but still show the minimize and maximize buttons? I've found several techniques for doing this on the web, but they all are different. This makes me wonder which one is the best. Any feedback would be appreciated, but an example would be exceptionally helpful

Thank you
Lance
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?TGFuY2U=?= said:
Is there a "best" technique for hiding a form's Close ControlBox
button (i.e., the button with an "X" in the top-right corner of a form)
but still show the minimize and maximize buttons? I've found several
techniques for doing this on the web, but they all are different. This
makes me wonder which one is the best. Any feedback would be
appreciated, but an example would be exceptionally helpful.

You cannot hide it, but disable it:

\\\
Private Declare Auto Function GetSystemMenu Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal bRevert As Int32 _
) As IntPtr

Private Declare Auto Function GetMenuItemCount Lib "user32.dll" ( _
ByVal hMenu As IntPtr _
) As Int32

Private Declare Auto Function DrawMenuBar Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As Int32

Private Declare Auto Function RemoveMenu Lib "user32.dll" ( _
ByVal hMenu As IntPtr, _
ByVal nPosition As Int32, _
ByVal wFlags As Int32 _
) As Int32

Private Const MF_BYPOSITION As Int32 = &H400
Private Const MF_REMOVE As Int32 = &H1000

Private Sub RemoveCloseButton(ByVal frmForm As Form)
Dim hMenu As IntPtr, n As Int32
hMenu = GetSystemMenu(frmForm.Handle, 0)
If Not hMenu.Equals(IntPtr.Zero) Then
n = GetMenuItemCount(hMenu)
If n > 0 Then
RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)
RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)
DrawMenuBar(frmForm.Handle)
End If
End If
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top