c#开发旅程

收集了所有我自己开发历程使用到过的所有点点滴滴 可能有时引用了他人的文章而没有注明请见谅

博客园 首页 联系 订阅 管理

让窗体从初始时就保持最大化,并且不能改变窗体大小的完美解决方案

我的要求就是让窗体从初始化的时候就最大化,并且用户不能改变窗体大小。最开始的想法是捕获窗体大小改变,即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(513);
            
this.ClientSize = new System.Drawing.Size(292266);
            
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(513);
            
this.ClientSize = new System.Drawing.Size(292266);
            
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;
    }

}

之所以要在Restore消息里面检测是否当前状态是最大化,是因为如果不检测的话,那么在最小化该窗体后就无法再打开了,因为Restore消息都被屏蔽掉了。

还有很重要的一点就是:必须要将窗体的FormBorderStyle设置为FixedSingle,当然只要是Fixed的,不一定要是FixedSingle。因为如果不是Fixed,那么可以通过在任务栏上右键菜单里面的大小操作来改变窗体的大小。

posted on 2005-07-28 14:55  Kinbor  阅读(3901)  评论(4编辑  收藏  举报