Inaktives Schliessenbutton

DerStauner

Erfahrenes Mitglied
Hallo wieder!

Ich habe das Forum durchsucht, bevor ich diese Frage gestellt habe.

Wie kann ich das Schliessenbutton einfach inaktiv machen, so wie z. B. das MaximizeButton

Und hier denke ich nicht an die Eigenschaft "Controlbox".

Danke im Voraus.
 
Diese Eigenschaft hat MS leider vergessen. Da musst du über NativeCalls was regeln.

Native Imports:
C#:
public enum SysCommands : int
{
    SC_MINIMIZE     = 0xF020,
    SC_MAXIMIZE     = 0xF030,
    SC_CLOSE        = 0xF060 
}

public enum MenuFlags : int
{
    MF_ENABLED      = 0x0;
    MF_GRAYED       = 0x1;
    MF_DISABLED     = 0x2;
}

/// <summary>
/// The EnableMenuItem function enables, disables, or grays the specified menu item.
/// </summary>
/// <param name="hMenu">Handle to the menu.</param>
/// <param name="uIDEnableItem">
/// Specifies the menu item to be enabled, disabled, or grayed, 
/// as determined by the uEnable parameter. This parameter specifies an item in a menu bar, menu, or submenu.
/// </param>
/// <param name="uEnable">
/// Controls the interpretation of the uIDEnableItem parameter and indicate whether the menu item is enabled, disabled, or grayed. 
/// This parameter must be a combination of either MF_BYCOMMAND or MF_BYPOSITION and MF_ENABLED, MF_DISABLED, or MF_GRAYED.
/// </param>
/// <returns>The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.</returns>
[DllImport("user32.dll")]
public static extern int EnableMenuItem(IntPtr hMenu, SysCommands uIDEnableItem, MenuFlags uEnable);

/// <summary>
/// The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying.
/// </summary>
/// <param name="hWnd">Handle to the window that will own a copy of the window menu.</param>
/// <param name="bRevert">
/// Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns a handle to the copy of the window menu currently in use. 
/// The copy is initially identical to the window menu, but it can be modified. If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state.
/// The previous window menu, if any, is destroyed.
/// </param>
/// <returns>If the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL.</returns>
[DllImport("user32.dll")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

Anwendung:
C#:
private void Form1_Load(object sender, System.EventArgs e)
{
    EnableMenuItem(GetSystemMenu(this.Handle, false), SysCommands.SC_CLOSE, MenuFlags.MF_GRAYED);
}
 
sorry, beim nächsten Mal werde ich es immer signalisieren: es handelt sich um Visual Basic 2005.

aber der Aufbau ist doch ähnlich, oder?
 
VB.net, kein Problem:

Imports
Visual Basic:
 Public Enum SysCommands As Integer
    SC_MINIMIZE = 61472
    SC_MAXIMIZE = 61488
    SC_CLOSE = 61536
End Enum
Public Enum MenuFlags As Integer
    
    MF_ENABLED = 0
    MF_GRAYED = 1
    MF_DISABLED = 2
End Enum

''' <summary>
''' The EnableMenuItem function enables, disables, or grays the specified menu item.
''' </summary>
''' <param name="hMenu">Handle to the menu.</param>
''' <param name="uIDEnableItem">
''' Specifies the menu item to be enabled, disabled, or grayed,
''' as determined by the uEnable parameter. This parameter specifies an item in a menu bar, menu, or submenu.
''' </param>
''' <param name="uEnable">
''' Controls the interpretation of the uIDEnableItem parameter and indicate whether the menu item is enabled, disabled, or grayed.
''' This parameter must be a combination of either MF_BYCOMMAND or MF_BYPOSITION and MF_ENABLED, MF_DISABLED, or MF_GRAYED.
''' </param>
''' <returns>The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.</returns>
<DllImport("user32.dll")> _
Public Shared Function EnableMenuItem(ByVal hMenu As IntPtr, ByVal uIDEnableItem As SysCommands, ByVal uEnable As MenuFlags) As Integer
End Function

''' <summary>
''' The GetSystemMenu function allows the application to access the window menu (also known as the system menu or the control menu) for copying and modifying.
''' </summary>
''' <param name="hWnd">Handle to the window that will own a copy of the window menu.</param>
''' <param name="bRevert">
''' Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns a handle to the copy of the window menu currently in use.
''' The copy is initially identical to the window menu, but it can be modified. If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state.
''' The previous window menu, if any, is destroyed.
''' </param>
''' <returns>If the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL.</returns>
<DllImport("user32.dll")> _
Public Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
End Function

Anwendung:
Visual Basic:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles this.Load
    EnableMenuItem(GetSystemMenu(Me.Handle, False), SysCommands.SC_CLOSE, MenuFlags.MF_GRAYED) 
End Sub
 
dieses "<DllImport("user32.dll")> _ " will nicht funktionieren.

Type "DllImport" is not defined.

Irgendeine Idee?
 
Zuletzt bearbeitet:
Den Import musst du natürlich noch hinzufügen. DIe Klasse befindet sich im Namespace System.Runtime.InteropServices
Visual Basic:
Imports System.Runtime.InteropServices
 
Zurück