怎么才能使我的WinForm窗口不能被鼠标改变大小呢。也就是固定窗口。我说的不能把窗口改成None,Fiexed这样子的。窗口要有标题,是一个普通的窗口。

解决方案 »

  1.   

    设置Form的最大及最小值相等。
      

  2.   

    将FormBorderStyle属性设置为:FixedSingle试试
      

  3.   

    在Windows编程中,有时需要限制窗体的大小。那么在C#中如何实现对窗体大小的限制呢?要限制窗体的大小,就必须处理WM_SIZEING消息,而WM_SIZEING消息的lParam参数,是一个指针,它指向RECT结构。这样,C#中又该如何处理指针类型的数据呢?下面的代码仅对窗体的宽度作限制,是根据AOGO的汇编程序改写。看看代码。就知道C#中是如何处理指针类型的数据了。(由此可见C#是多么健壮强劲!)using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;   //  注意此命名空间的Marshal类,
                                            //  它提供了许多类型转化方法:
                                            //  Marshal.PtrToStringAnsi();
                                            //    Marshal.PtrToStringAuto();
                                            //    Marshal.PtrToStringBSTR();
                                            //    Marshal.PtrToStringUni();
                                            //    Marshal.PtrToStructure();
                                            //    Marshal.StringToBSTR();
                                            //    Marshal.StringToCoTaskMemAnsi();
                                            //    Marshal.StringToCoTaskMemAuto();
                                            //    Marshal.StringToCoTaskMemUni();
                                            //    Marshal.StringToHGlobalAnsi();
                                            //    Marshal.StringToHGlobalAuto();
                                            //    Marshal.StringToHGlobalUni();
                                            //    Marshal.StructureToPtr();namespace WinMsgApp
    {    public class Form1 : System.Windows.Forms.Form
        {        // 声明Winodws消息常量
            private const int WM_SIZEING = 0x0214;        // 声明RECT结构
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }        private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox3;
            private System.Windows.Forms.TextBox textBox4;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.Button button1; 
            
            private System.ComponentModel.Container components = null;        public Form1()
            {
                InitializeComponent();
            }        protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }        #region Windows Form Designer generated code        private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.textBox4 = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(80, 8);
                this.textBox1.Name = "textBox1";
                this.textBox1.TabIndex = 0;
                this.textBox1.Text = "";
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(80, 32);
                this.textBox2.Name = "textBox2";
                this.textBox2.TabIndex = 1;
                this.textBox2.Text = "";
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(80, 56);
                this.textBox3.Name = "textBox3";
                this.textBox3.TabIndex = 2;
                this.textBox3.Text = "";
                // 
                // textBox4
                // 
                this.textBox4.Location = new System.Drawing.Point(80, 80);
                this.textBox4.Name = "textBox4";
                this.textBox4.TabIndex = 3;
                this.textBox4.Text = "";
                // 
                // label1
                // 
                this.label1.Location = new System.Drawing.Point(0, 8);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(78, 23);
                this.label1.TabIndex = 4;
                this.label1.Text = "Left:";
                this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                // 
                // label2
                // 
                this.label2.Location = new System.Drawing.Point(0, 32);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(78, 23);
                this.label2.TabIndex = 5;
                this.label2.Text = "Top:";
                this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                // 
                // label3
                // 
                this.label3.Location = new System.Drawing.Point(0, 56);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(78, 23);
                this.label3.TabIndex = 6;
                this.label3.Text = "Right:";
                this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                // 
                // label4
                // 
                this.label4.Location = new System.Drawing.Point(0, 80);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(78, 23);
                this.label4.TabIndex = 7;
                this.label4.Text = "Bottom:";
                this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(208, 8);
                this.button1.Name = "button1";
                this.button1.TabIndex = 8;
                this.button1.Text = "button1";
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(296, 117);
                this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                              this.button1,
                                                                              this.label4,
                                                                              this.label3,
                                                                              this.label2,
                                                                              this.label1,
                                                                              this.textBox4,
                                                                              this.textBox3,
                                                                              this.textBox2,
                                                                              this.textBox1});
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }
            #endregion
      

  4.   

    接上:// 以下是处理WINDOWS消息的主要代码
            protected override void WndProc(ref System.Windows.Forms.Message m )
            {
                switch(m.Msg) 
                {         
                    case WM_SIZEING:                    ///*****以下各种方法,生成代码大小顺序是由小到大
                         
                        // 第一种方法,简洁明了
                        // RECT rc = (RECT)m.GetLParam(typeof(RECT));                    // 第二种方法,普适通用。代码比第一种方法多 5 字节
                        // RECT rc=(RECT)Marshal.PtrToStructure(m.LParam ,typeof(RECT));                    // 第三种方法,易懂稍繁。代码比第二种方法多 4 字节
                        RECT rc = new RECT();
                        rc = (RECT)m.GetLParam(rc.GetType());
                        // 限制窗体宽度不超过600
                        if (rc.right - rc.left >450)
                        {
                            rc.right = rc.left + 450;
                        }
                        else if (rc.right - rc.left <200)
                        {
                            rc.right = rc.left + 200;
                        }                    // 将结构rc各字段的值显示出来
                        textBox1.Text = rc.left.ToString();
                        textBox2.Text = rc.top.ToString();
                        textBox3.Text = rc.right.ToString();
                        textBox4.Text = rc.bottom.ToString();                    // 将结构rc复制到m.LParam指针所指向的RECT结构
                        Marshal.StructureToPtr(rc,m.LParam,true);                    break;
                    default: 
                        base.WndProc(ref m);   // 调用基类函数处理其他消息。 
                        break; 
                } 
            }
            
        
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }        private void button1_Click(object sender, System.EventArgs e)
            {
                MessageBox.Show( Environment.GetFolderPath(Environment.SpecialFolder.System)); 
            }
        }
    }
      

  5.   

    谢谢,kkk_visual(宇宙中我是最厉害的!哈哈(但不包括地球)) 。