表格大小不会改变

时间:2016-04-14 15:00:45

标签: c# winforms

我有一个窗口,它总是小于设计师。无论设计师有多大,以及我在属性中放大的数字,它总是显得很小。我尝试使用autosize,autovalidate,autoscale选项来设置最大和最小尺寸。尽管如此,它总是小而且大小相同。我通过在初始化期间放置form.widthform.height来解决它。但我想知道发生了什么以及如何在将来阻止它。它发生在我的第二次,我不知道为什么。有什么提示可以查看更多内容吗?

c#designer

    namespace WindowsFormsApplication3
{
    partial class Form1
    {
        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.button4 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            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.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.label7 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.button1.Location = new System.Drawing.Point(77, 187);
            this.button1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(200, 55);
            this.button1.TabIndex = 0;
            this.button1.Text = "Segment";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.button2.Location = new System.Drawing.Point(77, 309);
            this.button2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(200, 55);
            this.button2.TabIndex = 1;
            this.button2.Text = "Triangle";
            this.button2.UseVisualStyleBackColor = false;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.button3.Location = new System.Drawing.Point(77, 433);
            this.button3.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(200, 55);
            this.button3.TabIndex = 2;
            this.button3.Text = "Quadrangle";
            this.button3.UseVisualStyleBackColor = false;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.button4.Location = new System.Drawing.Point(77, 559);
            this.button4.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(200, 55);
            this.button4.TabIndex = 3;
            this.button4.Text = "Hexagon";
            this.button4.UseVisualStyleBackColor = false;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.pictureBox1.Location = new System.Drawing.Point(101, 1027);
            this.pictureBox1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(176, 119);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(320, 193);
            this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 32);
            this.label1.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(320, 315);
            this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(0, 32);
            this.label2.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(320, 439);
            this.label3.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(0, 32);
            this.label3.TabIndex = 7;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(320, 565);
            this.label4.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(0, 32);
            this.label4.TabIndex = 8;
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(40, 40);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(2419, 49);
            this.menuStrip1.TabIndex = 10;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem,
            this.saveToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(75, 45);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // newToolStripMenuItem
            // 
            this.newToolStripMenuItem.Name = "newToolStripMenuItem";
            this.newToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
            this.newToolStripMenuItem.Text = "New";
            this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
            // 
            // saveToolStripMenuItem
            // 
            this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            this.saveToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
            this.saveToolStripMenuItem.Text = "Save";
            this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 49);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.label7);
            this.splitContainer1.Panel1.Controls.Add(this.label6);
            this.splitContainer1.Panel1.Controls.Add(this.button4);
            this.splitContainer1.Panel1.Controls.Add(this.button1);
            this.splitContainer1.Panel1.Controls.Add(this.label4);
            this.splitContainer1.Panel1.Controls.Add(this.button2);
            this.splitContainer1.Panel1.Controls.Add(this.label3);
            this.splitContainer1.Panel1.Controls.Add(this.button3);
            this.splitContainer1.Panel1.Controls.Add(this.label2);
            this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
            this.splitContainer1.Panel1.Controls.Add(this.label1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.pictureBox2);
            this.splitContainer1.Size = new System.Drawing.Size(2419, 1402);
            this.splitContainer1.SplitterDistance = 621;
            this.splitContainer1.TabIndex = 11;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(95, 927);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(309, 32);
            this.label7.TabIndex = 10;
            this.label7.Text = "A color for new shapes:";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(71, 98);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(120, 32);
            this.label6.TabIndex = 9;
            this.label6.Text = "Shapes:";
            // 
            // pictureBox2
            // 
            this.pictureBox2.BackColor = System.Drawing.Color.White;
            this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox2.Location = new System.Drawing.Point(0, 0);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(1794, 1402);
            this.pictureBox2.TabIndex = 9;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDoubleClick);
            this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
            this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
            this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
            this.pictureBox2.Resize += new System.EventHandler(this.pictureBox2_Resize);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange;
            this.ClientSize = new System.Drawing.Size(2419, 1451);
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.Name = "Form1";
            this.Text = "Simple Paint";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.PictureBox pictureBox1;
        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.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
    }
}

Form.cs删除了一个函数,因为我达到了极限。

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private bool isStrip = false;
        private string NameString;
        private string StorePath;
        private int VertexCatch = -1;
        private int button = -1;
        private int checker = 0;
        private int dblclck = -1;
        private Bitmap graphic;
        private Graphics g;
        private Name NameForm;
        private Point[] points;
        private Pen p = new Pen(Color.Black);
        private ColorDialog colorDialog1 = new ColorDialog();
        private List<Label> LabelList = new List<Label>();
        private List<Button> ButtonList = new List<Button>();
        private List<Point[]> PointsList = new List<Point[]>();
        private List<Brush> BrushList = new List<Brush>();
        public Form1()
        {
            InitializeComponent();
            graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            g = Graphics.FromImage(graphic);
            LabelList.Add(label1);
            LabelList.Add(label2);
            LabelList.Add(label3);
            LabelList.Add(label4);
            ButtonList.Add(button1);
            ButtonList.Add(button2);
            ButtonList.Add(button3);
            ButtonList.Add(button4);
            this.Width = 1200;
            this.Height = 600;
        }
        private void DrawFullList()
        {
            graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height + 100);
            g = Graphics.FromImage(graphic);
            pictureBox2.Image = graphic;
            for (int i = 0; i < PointsList.Count; i++)
            {
                bool yellowframe = false;
                if (i == dblclck)
                    yellowframe = true;
                Draw(BrushList[i], PointsList[i], yellowframe);
            }
        }
        private void Draw(Brush brush, Point[] points, bool yellowframe)
        {
            Pen PathPen = new Pen(brush);
            if (yellowframe)
                PathPen = new Pen(Color.Yellow);
            PathPen.Width = 3;
            g.DrawPolygon(PathPen, points);
            g.FillPolygon(brush, points);
            pictureBox2.Image = graphic;
        }

        private int showNumberForButton(int button)
        {
            if (button == -1)
                return 0;
           else if (button > 2)
                return 6;
            else
                return button + 2;
        }
        private void ButtonClick(int buttonint)
        {
            button = buttonint;
            LabelList[button].Text = "Choose " + (showNumberForButton(button)) + " more points\nto construct shape"; ;
            if(showNumberForButton(button) == 2)
                points = new Point[4];
            else
                points = new Point[showNumberForButton(button)];
            for (int i = 0; i < 4; i++)
                ButtonList[i].Enabled = false;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DialogResult result = colorDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                p.Color = colorDialog1.Color;
                pictureBox1.BackColor = colorDialog1.Color;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ButtonClick(0);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            ButtonClick(1);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            ButtonClick(2);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            ButtonClick(3);
        }
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                StorePath = dialog.SelectedPath;
                NameForm = new Name();
                NameForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
                NameForm.ShowDialog();
            }
            else
                MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void ChildFormClosed(object sender, FormClosedEventArgs e)
        {
            NameString = NameForm.PassName();
            if (NameString != null && StorePath != null)
                graphic.Save(StorePath + "\\" + NameString + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            else
                MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < PointsList.Count; i++)
            {
                if (IsPointInPolygon4(PointsList[i], pictureBox2.PointToClient(Cursor.Position)))
                {
                    dblclck = -1;
                    DrawFullList();
                }
            }
            if (checker < showNumberForButton(button))
            {
                points[checker++] = pictureBox2.PointToClient(Cursor.Position);
                if (showNumberForButton(button) == 2 && checker == 2)
                {
                    points[2] = new Point(points[1].X + 3, points[1].Y + 3);
                    points[3] = new Point(points[0].X + 3, points[0].Y + 3);
                }
                if (checker == showNumberForButton(button))
                {
                    Brush brush = new SolidBrush(colorDialog1.Color);
                    Draw(brush, points, false);
                    PointsList.Add(points);
                    BrushList.Add(brush);
                    checker = 0;
                    button = -1;
                    for (int i = 0; i < 4; i++)
                    {
                        ButtonList[i].Enabled = true;
                        LabelList[i].Text = string.Empty;
                    }
                }
                else
                    LabelList[button].Text = "Choose " + (showNumberForButton(button) - checker) + " more points\nto construct shape";
            }
            Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
            for (int i = 0; i < PointsList.Count; i++)
            {
                if (IsPointInPolygon4(PointsList[i], MouseCatch))
                {
                    for (int j = 0; j < PointsList[i].Length; j++)
                    {
                        if (MouseCatch.X >= PointsList[i][j].X - 20 && MouseCatch.X <= PointsList[i][j].X + 20 && MouseCatch.Y >= PointsList[i][j].Y - 20 && MouseCatch.Y <= PointsList[i][j].Y + 20)
                        {
                            VertexCatch = j;
                            Point[] temp = PointsList[i];
                            Brush tempB = BrushList[i];
                            PointsList.Remove(PointsList[i]);
                            BrushList.Remove(BrushList[i]);
                            PointsList.Add(temp);
                            BrushList.Add(tempB);
                            if (isItStrip(PointsList[PointsList.Count - 1]))
                                isStrip = true;
                            break;
                        }
                    }

                }
            }
        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            PointsList.Clear();
            BrushList.Clear();
            checker = 0;
            button = -1;
            for (int i = 0; i < 4; i++)
            {
                ButtonList[i].Enabled = true;
                LabelList[i].Text = string.Empty;
            }
            VertexCatch = -1;
            g = Graphics.FromImage(graphic);
            pictureBox2.Image = graphic;
        }
        private bool isItStrip(Point[] Points)
        {
            if (Points.Length == 4 && (points[2].X == (points[1].X + 3)) && (points[2].Y == points[1].Y + 3) && (points[3].X == (points[0].X + 3)) && (points[0].Y + 3 == points[3].Y))
                return true;
            return false;
       }
        private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
        {
            if (VertexCatch > -1)
            {
                Point k = pictureBox2.PointToClient(Cursor.Position);
                PointsList[PointsList.Count - 1][VertexCatch] = k;
                if (isStrip)
                {
                    if (VertexCatch == 0)
                        PointsList[PointsList.Count - 1][VertexCatch + 3] = new Point (k.X + 3,k.Y + 3);
                    else if(VertexCatch == 1)
                        PointsList[PointsList.Count - 1][VertexCatch + 1] = new Point(k.X + 3, k.Y + 3);
                    else if (VertexCatch == 2)
                        PointsList[PointsList.Count - 1][VertexCatch - 1] = new Point(k.X - 3, k.Y + 3);
                    else if (VertexCatch == 3)
                        PointsList[PointsList.Count - 1][VertexCatch - 3] = new Point(k.X - 3, k.Y + 3);
                }
                DrawFullList();
            }
        }
        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            isStrip = false;
            VertexCatch = -1;
        }
        private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
                Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
                for (int i = PointsList.Count-1; i >=0 ; i--)
                {
                    if (IsPointInPolygon4(PointsList[i], MouseCatch))
                    {
                            dblclck = i;
                        break;
                    }
                }
                DrawFullList();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (dblclck != -1) {
                switch (keyData) {
                    case Keys.Left:
                            for (int i = 0; i < PointsList[dblclck].Length; i++)
                                PointsList[dblclck][i].X -= 20;
                        break;
                    case Keys.Right:
                        for (int i = 0; i < PointsList[dblclck].Length; i++)
                            PointsList[dblclck][i].X += 20;
                        break;
                    case Keys.Up:
                        for (int i = 0; i < PointsList[dblclck].Length; i++)
                            PointsList[dblclck][i].Y -= 20;
                        break;
                    case Keys.Down:
                        for (int i = 0; i < PointsList[dblclck].Length; i++)
                            PointsList[dblclck][i].Y += 20;
                        break;
                    case Keys.Enter:
                        dblclck = -1;
                        break;
                    default:
                        return base.ProcessCmdKey(ref msg, keyData);
                }
                DrawFullList();
                return true;
        }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        private void pictureBox2_Resize(object sender, EventArgs e)
        {
            DrawFullList();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

设计器中的表单设置为2419 x 1451像素...如果这大于屏幕,WinForms会忽略该属性的设置(检查this.ClientSize = new System.Drawing.Size(2419, 1451);文件中的Form1.designer.cs),所以窗口获得“默认”大小。

在设计器中将表单设置为合理的大小(例如,1200 x 600,就像您在构造函数中所做的那样)。

如果您将其设置为1200x600但仍然被序列化为2419x1451,那么您可能遇到了winform设计器具有高dpi显示的 MANY 问题之一。将AutoscaleMode设置为None,并且在序列化时不应尝试将属性调整为“当前”dpi。