Winform,自定义带有图像和文本的标题栏,但没有控制按钮

本文关键字:标题栏 控制 按钮 自定义 图像 Winform 文本 | 更新日期: 2023-09-27 18:05:55

在winform应用程序中,我如何在"标题栏"中添加图像和标题文本并删除所有控制按钮(min, max &关闭)。我可以显示图像和标题文本,但无法删除所有按钮,包括"关闭"按钮。有什么解决办法吗?

Winform,自定义带有图像和文本的标题栏,但没有控制按钮

你可以将窗体的ControlBox属性设置为False,然后你可以很容易地删除所有按钮(最小,最大,关闭按钮),甚至你可以将标题和图像设置为它,而使用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作为函数的参数。