我的.Net Core 3.0 windows 桌面程序界面绘制 -- 从.net framework复制代码法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/juwikuang/article/details/90217230

今天想用 .net core做个小程序。却意外的发现没有可视化编辑器!!!(重要的事情用三个感叹号)

虽然我搞了十多年的 .net 开发,但是我确实从来没有自己编辑过 Designer.cs 这个文件。微软不是不让我编辑么!

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>

我于是去网上搜,结果,人家确实是编辑Designer.cs文件的。

不过,我还是想动一点小聪明,逃避学习编辑 Designer.cs 。我想到的办法很简单,就是先用老的Windows Form(.net framework)程序,可视化的绘制好界面,然后复制到新的 windows form (.net core)。

绘制 windows form ( .net framework )

在这里插入图片描述
我的目标是绘制一个下拉框Combobox, 一个文本框textbox, 两个按钮 Buttons。相信绝大多数人如果直接敲代码绘制这样一个简单的windows form,也要费半天劲。有了工具栏(Toolbox),就简单多了。

别忘了双击两个按钮,把事件也加上。

复制代码

这四个控件的代码,分为四个部分。分别是变量声明部分,变量实例化部分,具体设置部分,添加控件部分。另外事件也要复制过去。

变量声明部分

既给Form四个属性(Property),分别对应四个控件。所以,这段代码肯定在class的下面。具体到Designer.cs文件里,它们位于最底部。

如下:

        private System.Windows.Forms.TextBox srtFileTextBox;
        private System.Windows.Forms.Button browseButton;
        private System.Windows.Forms.Button okayButton;
        private System.Windows.Forms.ComboBox tvShowComboBox;

找到Windows Forms(.net core)程序的对应部分,复制过去。

变量实例化部分

这部分位于InitializeComponent函数的最开头。

扫描二维码关注公众号,回复: 7193985 查看本文章
        private void InitializeComponent()
        {
            this.srtFileTextBox = new System.Windows.Forms.TextBox();
            this.browseButton = new System.Windows.Forms.Button();
            this.okayButton = new System.Windows.Forms.Button();
            this.tvShowComboBox = new System.Windows.Forms.ComboBox();

找到.net core的对应位置复制过去即可。

具体设置部分

这里具体设置控件的一些属性,最主要的是位置,大小等。

            // 
            // srtFileTextBox
            // 
            this.srtFileTextBox.Location = new System.Drawing.Point(12, 46);
            this.srtFileTextBox.Name = "srtFileTextBox";
            this.srtFileTextBox.Size = new System.Drawing.Size(392, 20);
            this.srtFileTextBox.TabIndex = 0;
            // 
            // browseButton
            // 
            this.browseButton.Location = new System.Drawing.Point(410, 46);
            this.browseButton.Name = "browseButton";
            this.browseButton.Size = new System.Drawing.Size(75, 23);
            this.browseButton.TabIndex = 1;
            this.browseButton.Text = "Browse...";
            this.browseButton.UseVisualStyleBackColor = true;
            this.browseButton.Click += new System.EventHandler(this.BrowseButton_Click);
            // 
            // okayButton
            // 
            this.okayButton.Location = new System.Drawing.Point(224, 72);
            this.okayButton.Name = "okayButton";
            this.okayButton.Size = new System.Drawing.Size(75, 23);
            this.okayButton.TabIndex = 2;
            this.okayButton.Text = "Okay";
            this.okayButton.UseVisualStyleBackColor = true;
            this.okayButton.Click += new System.EventHandler(this.OkayButton_Click);
            // 
            // tvShowComboBox
            // 
            this.tvShowComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.tvShowComboBox.FormattingEnabled = true;
            this.tvShowComboBox.Items.AddRange(new object[] {
            "权力的游戏",
            "纸牌屋",
            "复仇者联盟"});
            this.tvShowComboBox.Location = new System.Drawing.Point(12, 12);
            this.tvShowComboBox.Name = "tvShowComboBox";
            this.tvShowComboBox.Size = new System.Drawing.Size(473, 21);
            this.tvShowComboBox.TabIndex = 3;

找到.net core的对应部分,替换即可。

添加控件部分

所以的控件,必须添加到相应的容器之中。像GroupBox, Panel这些都是容器。当然Windows Form本身也是容器。

在这里插入图片描述

这里的四个控件,都直接隶属于Windows Form控件。

找到.net core的对应代码替换之即可。

复制事件

把form.cs里面的事件复制到 .net core 的 form.cs即可。

        private void BrowseButton_Click(object sender, EventArgs e)
        {

        }

        private void OkayButton_Click(object sender, EventArgs e)
        {

        }

调整位置大小

我满心期待着成功,结果出来这么个玩意儿。

在这里插入图片描述
我研究了一下,原来是这行代码捣的鬼:

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(500, 109);

把它也复制过去,这下样式正常了。

在这里插入图片描述
看看是不是和.net framework 的windows form一样了。

编辑逻辑代码

这里我和以前一样,使用了一个OpenFileDialog,选择一个srt文件并处理。

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "Subtitle files (*.srt)|*.srt";
            if(openFileDlg.ShowDialog() ==DialogResult.OK  )
            {
                srtFileTextBox.Text = openFileDlg.FileName;
            }
        }

        private void OkayButton_Click(object sender, EventArgs e)
        {
            //do something
            Application.Exit();
        }

源代码

见Github

https://github.com/juwikuang/CreateNetCoreWindowsFormFromFramework

猜你喜欢

转载自blog.csdn.net/juwikuang/article/details/90217230