让窗体从初始时就保持最大化,并且不能改变窗体大小的完美解决方案
我的要求就是让窗体从初始化的时候就最大化,并且用户不能改变窗体大小。最开始的想法是捕获窗体大小改变,即ReSize事件,然后在事件里面把窗体大小设置成初始时的大小。完整的Form.cs代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Size s;
private Point p;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void OnResize(EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
p = this.Location;
s = this.Size;
}
else
{
if (!s.IsEmpty && !p.IsEmpty)
{
this.Location = p;
this.Size = s;
}
}
}
}
}
用这种方法的话,基本没有什么问题。但是在窗体大小改变的时候,不知道为什么(可能是要处理事件,可能是要窗体重绘,我不清楚,希望有人告诉我),会使得窗体在不断地闪烁。这对我来说就不算完美了。后来我的一个叫HAL的网友给了我如下的解决方案,个人认为比较完美的了。
其实说起来也不复杂,就是直接重写Windows窗体处理消息的过程,即WndProc方法。在这个方法里面,我们检测窗体大小恢复以及在非客户区域双击鼠标左键的消息,不处理这两个消息。这样就可以实现在窗体画面不闪动的条件下也保持其大小了。完整Form.cs文件如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND )
{
if (m.WParam.ToInt32() == SC_RESTORE)
{
if (this.WindowState != FormWindowState.Minimized)
return;
}
}
if (m.Msg == WM_NCLBUTTONDBLCLK)
{
return;
}
base.WndProc (ref m);
}
public const int WM_NCLBUTTONDBLCLK =0x00A3;
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_RESTORE = 0xF120;
}
}

































































































其实说起来也不复杂,就是直接重写Windows窗体处理消息的过程,即WndProc方法。在这个方法里面,我们检测窗体大小恢复以及在非客户区域双击鼠标左键的消息,不处理这两个消息。这样就可以实现在窗体画面不闪动的条件下也保持其大小了。完整Form.cs文件如下:






































































































之所以要在Restore消息里面检测是否当前状态是最大化,是因为如果不检测的话,那么在最小化该窗体后就无法再打开了,因为Restore消息都被屏蔽掉了。
还有很重要的一点就是:必须要将窗体的FormBorderStyle设置为FixedSingle,当然只要是Fixed的,不一定要是FixedSingle。因为如果不是Fixed,那么可以通过在任务栏上右键菜单里面的大小操作来改变窗体的大小。
也许很多高手都有其他的方法,或者觉得我这个方法也有问题。欢迎指正,提前感谢了!
分类:
.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!