删除 Windows 窗体中的标题栏

Posted

技术标签:

【中文标题】删除 Windows 窗体中的标题栏【英文标题】:Remove the title bar in Windows Forms 【发布时间】:2011-11-20 22:15:10 【问题描述】:

如何删除窗口窗体顶部的蓝色边框? (我不知道它的确切名称。)

【问题讨论】:

它被称为 TitleBar,您可以隐藏它,将表单的边框样式属性更改为无边框或无边框。 【参考方案1】:

您可以在设计器中将属性FormBorderStyle设置为无, 或在代码中:

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

【讨论】:

不幸的是,FormBorderStyle.Noneform resize 上的FormBorderStyle.None 在Windows 10(至少在某些版本上)存在问题。【参考方案2】:

如果 Blue Border thats on top of the Window Form 是指 titlebar,请将 Forms ControlBox 属性设置为 false 并将 Text 属性设置为空字符串 ("")。

这是一个sn-p:

this.ControlBox = false;
this.Text = String.Empty;

【讨论】:

与将边框样式设置为无相比,您的解决方案具有优势,因为...它使边框保持完整:) +1 不知何故,如果您通过FormBorderStyle.None 执行此操作,它会禁止您以某种方式在表单上绘图(OnPaint 在其Dock 设置为Fill 的图片框中设置图像),工作很好,直到我用FormBorderStyle.None 更改了边框设置,但是这样,绘图仍然对我有用:) @JohnNguyen 不工作?奇怪,你确定你已经正确实现了吗? 这个解决方案在 Windows 10 中看起来很糟糕——“隐藏”的标题栏并没有完全消失——在窗口顶部留下一个“凹凸”。我认为这是由 Windows 10 薄窗口边框引起的。我还没有找到解决这个问题的方法。看起来我被困在 FormBorderStyle.None 路线上。 使用上述建议将 FormBorderStyle 设置为 Sizable 有效,但请注意,Windows 10 在客户端矩形外的窗口顶部添加了一个难看的栏,这似乎是抓取区域/调整大小-用于垂直调整窗口大小的边框(似乎顶部边框呈现在可见表单边框内,其他边框呈现在 o_O 之外)。【参考方案3】:

还将这段代码添加到您的表单中,使其仍可拖动。

只需在构造函数之前添加它(调用InitializeComponent()的方法


private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

///
/// Handling the window messages
///
protected override void WndProc(ref Message message)

    base.WndProc(ref message);

    if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
        message.Result = (IntPtr)HTCAPTION;


该代码来自:https://jachman.wordpress.com/2006/06/08/enhanced-drag-and-move-winforms-without-having-a-titlebar/

现在摆脱标题栏但仍有边框结合来自其他响应的代码:

this.ControlBox = false;

this.Text = String.Empty;

用这一行:

this.FormBorderStyle = FormBorderStyle.FixedSingle;


将这 3 行代码放入表单的 OnLoad 事件中,您应该有一个漂亮的“浮动”表单,该表单可以拖动,带有细边框(如果您不想要边框,请使用 FormBorderStyle.None)。

【讨论】:

此选项使窗口变大。比将 FormBorderStyle 设置为 None 要好得多。正是我想要的。 嗨@AntonioRodríguez,您如何调整此表单的大小?我有一个普通表单,并将其放入 Load 事件中,它显示单行边框 + 无标题栏表单,但无法调整大小(我在 Windows 10 上) this.ControlBox = false; this.Text = String.Empty; this.FormBorderStyle = FormBorderStyle.FixedSingle;【参考方案4】:
 Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None

【讨论】:

【参考方案5】:

将表单的FormsBorderStyle 设置为None

如果你这样做了,如何实现窗口的拖动和关闭功能取决于你。

【讨论】:

没有办法保持一个没有边框的相当大的表单,并且顶部没有那个烦人的小标题栏。即使直接使用Win32也不会摆脱它。如果您没有边界,则必须实现自己的关闭、最大化、最小化方法,这些方法很容易。实现相当大的虽然是万无一失的正确痛苦。我尝试过,但最终放弃了,它付出了很多努力却没有多少收获。【参考方案6】:

我正在分享我的代码。 form1.cs:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BorderExp

public partial class Form1 : Form

    public Form1()
    
        InitializeComponent();
    

    private void Form1_Load(object sender, EventArgs e)
    
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

    

    private void ExitClick(object sender, EventArgs e)
    
        Application.Exit();
    

    private void MaxClick(object sender, EventArgs e)
    
        if (WindowState ==FormWindowState.Normal)
        
            this.WindowState = FormWindowState.Maximized;
        
        else
        
            this.WindowState = FormWindowState.Normal;
        
    

    private void MinClick(object sender, EventArgs e)
    
        this.WindowState = FormWindowState.Minimized;
       
    
    

现在,设计师:-

namespace BorderExp
 
   partial class Form1
  
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    
        if (disposing && (components != null))
        
            components.Dispose();
        
        base.Dispose(disposing);
    

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.button1.BackColor = System.Drawing.SystemColors.ButtonFace;
        this.button1.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
        this.button1.FlatAppearance.BorderSize = 0;
        this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
        this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.button1.Location = new System.Drawing.Point(376, 1);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(27, 26);
        this.button1.TabIndex = 0;
        this.button1.Text = "X";
        this.button1.UseVisualStyleBackColor = false;
        this.button1.Click += new System.EventHandler(this.ExitClick);
        // 
        // button2
        // 
        this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.button2.BackColor = System.Drawing.SystemColors.ButtonFace;
        this.button2.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
        this.button2.FlatAppearance.BorderSize = 0;
        this.button2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
        this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.button2.Location = new System.Drawing.Point(343, 1);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(27, 26);
        this.button2.TabIndex = 1;
        this.button2.Text = "[]";
        this.button2.UseVisualStyleBackColor = false;
        this.button2.Click += new System.EventHandler(this.MaxClick);
        // 
        // button3
        // 
        this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.button3.BackColor = System.Drawing.SystemColors.ButtonFace;
        this.button3.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
        this.button3.FlatAppearance.BorderSize = 0;
        this.button3.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
        this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.button3.Location = new System.Drawing.Point(310, 1);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(27, 26);
        this.button3.TabIndex = 2;
        this.button3.Text = "___";
        this.button3.UseVisualStyleBackColor = false;
        this.button3.Click += new System.EventHandler(this.MinClick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackgroundImage = global::BorderExp.Properties.Resources.blank_1_;
        this.ClientSize = new System.Drawing.Size(403, 320);
        this.ControlBox = false;
        this.Controls.Add(this.button3);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    

    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    
   

截图:- NoBorderForm

【讨论】:

以上是关于删除 Windows 窗体中的标题栏的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Windows 窗体 C# 中完全隐藏顶部栏

如何更改 Windows 窗体中标题栏中的文本?

C# winfrom中如何隐藏标题栏中的 关闭按钮图标?

csharp SingleInstance Windows窗体 - 托盘栏中的通知图标

如何在任务栏顶部全屏显示 Windows 窗体? [复制]

Windows窗体应用程序中的进度栏 异常