开发没有表单设计器的UI软件?

时间:2014-01-24 17:11:27

标签: c# winforms user-interface

我有兴趣开发像Text Editors这样的软件。

我目前知道如何使用C#开发软件的唯一方法是使用Visual Studio的表单设计器:http://i.imgur.com/oRAd6M4.png

在Java中,有可能(我知道如何)这样做。

是否有可能在C#中开发软件,就像在Java中完成它一样(通过100%代码)。

1 个答案:

答案 0 :(得分:1)

是的,这是非常可能的。表单设计器只是一个可视化的包装器,可以在幕后生成代码。您可以使用WPF作为UI设计的声明方法。您可以使用WinForms执行相同的操作。这是一个手工编写的简单表单示例。除了练习之外,我不明白你为什么要为非平凡的UI应用程序做这个。

namespace MyTestApp
{
    public static class Program
    {
        [System.STAThread]
        private static void Main ()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

            System.Windows.Forms.Application.Run(new MyForm());
        }

        public class MyForm: System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button ButtonClose { get; set; }
            private System.Windows.Forms.RichTextBox RichTextBox { get; set; }

            public MyForm ()
            {
                this.ButtonClose = new System.Windows.Forms.Button();
                this.RichTextBox = new System.Windows.Forms.RichTextBox();

                this.ButtonClose.Text = "&Close";
                this.ButtonClose.Click += new System.EventHandler(ButtonClose_Click);

                this.Controls.Add(this.ButtonClose);
                this.Controls.Add(this.RichTextBox);

                this.Load += new System.EventHandler(MyForm_Load);
            }

            private void MyForm_Load (object sender, System.EventArgs e)
            {
                int spacer = 4;

                this.RichTextBox.Location = new System.Drawing.Point(spacer, spacer);
                this.RichTextBox.Size = new System.Drawing.Size(this.ClientSize.Width - this.RichTextBox.Left - spacer, this.ClientSize.Height - this.RichTextBox.Top - spacer - this.ButtonClose.Height - spacer);

                this.ButtonClose.Location = new System.Drawing.Point(this.ClientSize.Width - this.ButtonClose.Width - spacer, this.ClientSize.Height - this.ButtonClose.Height - spacer);
            }

            private void ButtonClose_Click (object sender, System.EventArgs e)
            {
                this.Close();
            }
        }
    }
}

或者,在使用设计器时,请查看FormName.Designer.cs文件,该文件包含与上面相同的初始化代码。