Disabling "close" box in controlbox on a form

C

Colin McGuire

Hi, is it possible to create/show a form with a controlbox in the
title bar at the top where:
a) The minimize box is enabled
b) The maximize box is disabled
c) The close box is disabled

I can get the minimize & maximize boxes to be enabled or disabled by
changing the properties "MinimizeBox" and "MaximizeBox" in the IDE
properties window to one of true or false. However I can't sort out
how to disable the close box. My current "FormBorderStyle" is
currently "Fixed".

Thank you
Colin
 
H

Herfried K. Wagner [MVP]

Colin McGuire said:
Hi, is it possible to create/show a form with a controlbox in the
title bar at the top where:
a) The minimize box is enabled
b) The maximize box is disabled
c) The close box is disabled

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

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

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

Private Declare 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
///
 
C

Cor

Herfried,
I hate pages were a part of the controlbox is disabled
I have to close them in the statusbar or with taskmanager.
:))))
Cor
 
M

Mick Doherty

With this method you can't close it from the statusbar. So now all he has to
do to lose your custom is hide the app from taskmanager.
 
H

Herfried K. Wagner [MVP]

Cor said:
I hate pages were a part of the controlbox is disabled
I have to close them in the statusbar or with taskmanager.

I hate them too.
 
D

David Gordon

Hi,

Colin,

I would like to know how to implement this code as I too am trying to
have just a Minimise button on my form, no maximise or close.

Appreciate your soonest respose.

David

++++++++++++++++++++++++++++++++++


Hi, is it possible to create/show a form with a controlbox in the
title bar at the top where:
a) The minimize box is enabled
b) The maximize box is disabled
c) The close box is disabled

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

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

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

Private Declare 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
///
 
H

Herfried K. Wagner [MVP]

* David Gordon said:
\\\
Private Declare Function GetSystemMenu Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal bRevert As Int32 _
) As IntPtr

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

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

Private Declare 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
///

I remember I saw this code somewhere... Just add the code to a form and call 'RemoveCloseButton(Me)'.

;-)
 

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