【发布时间】:2011-08-16 06:31:47
【问题描述】:
在 winform 应用程序中,如何在“标题栏”中添加图像和标题文本并删除所有控制按钮(最小、最大和关闭)。我可以显示图像和标题文本,但无法删除所有按钮,包括“关闭”按钮。有什么解决方法吗?
【问题讨论】:
在 winform 应用程序中,如何在“标题栏”中添加图像和标题文本并删除所有控制按钮(最小、最大和关闭)。我可以显示图像和标题文本,但无法删除所有按钮,包括“关闭”按钮。有什么解决方法吗?
【问题讨论】:
您可以将表单的 ControlBox 属性设置为 False,然后您可以轻松删除所有按钮(最小、最大、关闭按钮),甚至可以将 Title 和 Image 设置为它,而使用 FormBorderStyle 它将完全删除标题栏,这对您的问题没有帮助。
所以我建议你设置ControlBox=false的形式
【讨论】:
在 FormDesigner 中将 FormBorderStyle 设置为 None 可能会有所帮助。
【讨论】:
不幸的是,您需要使用 PInvoke 调用 Windows API 函数。
// Changes an attribute of the specified window.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
// Retrieves information about the specified window.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_STYLE = (-16);
public const int WS_SYSMENU = 0x00080000;
public const int WS_MAXIMIZEBOX = 0x00010000;
public static void SetDialogStyle(Form window)
{
// We disable the control box functionality for the window
// i.e. remove the minimize, maximize and close button as
// well as the system menu.
int style = GetWindowLong(window.Handle, GWL_STYLE);
style &= ~(WS_SYSMENU | WS_MAXIMIZEBOX);
SetWindowLong(window.Handle, GWL_STYLE, style);
}
你可以在 OnLoad 事件中调用这个函数,传递 this 作为函数的参数。
【讨论】: