WinForm Question

J

Jack

Hi,

I am wondering is there a way to have the WinForm ControlBox option set to
true and disable the close button (x)? Also, is there a way to force the
form to be maximized when activated?

Thanks
 
H

Herfried K. Wagner [MVP]

* "Jack said:
I am wondering is there a way to have the WinForm ControlBox option set to
true and disable the close button (x)? Also, is there a way to force the
form to be maximized when activated?

(1):

\\\
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
///

(2)

Set the form's 'WindowState' property to 'Maximized' in its 'Activated'
event handler.
 
G

Guest

Here is the C# code if anyone is interested:

[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr GetSystemMenu ( IntPtr hWnd, int bRevert );

[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int GetMenuItemCount(IntPtr hMenu);

[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int DrawMenuBar(IntPtr hWnd);

[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int
wFlags);

private const int MF_BYPOSITION = 0x400;
private const int MF_REMOVE = 0x1000;

private void RemoveCloseButton(Form frmForm)
{
IntPtr hMenu;
int n;
hMenu = GetSystemMenu(frmForm.Handle, 0);
if (!(hMenu == IntPtr.Zero))
{
n = GetMenuItemCount(hMenu);
if (n > 0)
{
RemoveMenu(hMenu, n - 1, MF_BYPOSITION);
RemoveMenu(hMenu, n - 2, MF_BYPOSITION);
}
DrawMenuBar(frmForm.Handle);
}
}
 
H

Herfried K. Wagner [MVP]

* "jg said:
you can disable the close button with e.cancel in the on_closing event.

This will disable its functionality, but the button will still appear
enabled. That may be very irritating for the user.
 

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