把winForm从Windows平台搬到Linux mint平台

把winForm从Windows平台搬到Linux mint平台

编码文章call10242025-04-08 11:23:2320A+A-

Xlib为gnome和qt奠定了根本性基础,才有了后来的gdk/gtk,gnome,gtk-builder, gnome-builder,glade和qt,qtcreator,qtdesigner。gnome和qt基本上代表了Linux的两大主流派系,现在都是开源可用的,尤其对于个人使用就都不在话下了。看啊,学啊,总算能回头看了,虽不是很清晰,但也基本上看清了它们的大模样。

不得不说mono团队还是非常努力,开始我并不怎么喜欢它,一是它与.net不同步,比较滞后;二是它并不完全兼容.net,一个Windows平台上winform的C#程序要想搬到linux平台上,看一下mono站点上的porting就知道了,要改许多地方,然后免强运行后还要调试修改;三个在linux平台上运行慢,且不稳定。

现在什么情况呢?站点上的描述与实际情况不符,换句话说程序已经完全不同只是站点上的文档陈旧没变。试运行后,感觉是:

完全不同于以往的新景象

下面的实例是将一个VS2022上C#的winform搬到linux运行。

VS2022上设计的HelloWorld程序

很简单地创建一个winform程序(略),在窗体上放一个table窗口,和linux上的grid差不多一个意思,把控件label1,button1,button2拖进格子,因为只是示例,RowSpan就不用管它了。布局的AutoSize属性设置为 true; 控件Alignment居中摆放。

双击button1,写一句 label1.Text = "Hello World!";

双击button2,写一句 Application.Exit();

F5运行,点击button1, label1显示Hello world; 点击 button2,程序退出。

Linux上的 HelloWorld.cs 程序

创建个空的 HelloWorls.cs 文件,将vs2022的 Form1()代码拷贝过来,去掉红圈中的partial,因为vs2022上是拆分代码的用了partial,到linux上合并一个就不需要partial了。接下来是构造函数 InitializeComponent() 一句, 去找到这段代码放在构造函数前面。

将这部分代码搬过来,放到构造函数前。

把这两个函数也拷贝了

然后保存、编译

csc -r:System.Windows.Forms.dll -r:System.Drawing.dll HelloWorld.cs

生成 HelloWorld.exe , 运行 mono HelloWorld.exe , 结果:

对比 vs2022 在windows 上的显示

有不同吗?有, 窗体风格不一样,是window manager的原因,如果放在 deepin上运行则还会是另外的轮廓。

C#在Linux上的窗体应用程序可以用mono自带的dll产生窗体,gtk不是必须的,当然也是可以使用的,由编程人员自行选择,但linux平台上人们更习惯于gtk的窗体样式,原因说也说不清楚。Windows上C#的winForm应用迁移到Linux上只需把CS文件的东西和调用的函数带到Linux平台上就行了,无需改动源代码。或许有更简单的办法,我尚且没学到呢

Linux mint 下的IDE环境

atom + build 插件 + mono的 csc, msc, mono本身。


附源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
  public class Form1 : Form
  {
    /// 
    /// 必需的设计器变量。
    /// 
    private System.ComponentModel.IContainer components = null;

    /// 
    /// 清理所有正在使用的资源。
    /// 
    /// 如果应释放托管资源,为 true;否则为 false。
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// 
    /// 设计器支持所需的方法 - 不要修改
    /// 使用代码编辑器修改此方法的内容。
    /// 
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.tableLayoutPanel1.SuspendLayout();
        this.SuspendLayout();
        //
        // button1
        //
        this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.button1.Location = new System.Drawing.Point(203, 228);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(194, 219);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        //
        // button2
        //
        this.button2.Dock = System.Windows.Forms.DockStyle.Fill;
        this.button2.Location = new System.Drawing.Point(403, 228);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(194, 219);
        this.button2.TabIndex = 2;
        this.button2.Text = "button2";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        //
        // label1
        //
        this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.label1.Font = new System.Drawing.Font("宋体", 32F);
        this.label1.Location = new System.Drawing.Point(203, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(194, 225);
        this.label1.TabIndex = 3;
        this.label1.Text = "label1";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        //
        // tableLayoutPanel1
        //
        this.tableLayoutPanel1.AutoSize = true;
        this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.tableLayoutPanel1.ColumnCount = 4;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
        this.tableLayoutPanel1.Controls.Add(this.button2, 2, 1);
        this.tableLayoutPanel1.Controls.Add(this.button1, 1, 1);
        this.tableLayoutPanel1.Controls.Add(this.label1, 1, 0);
        this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.RowCount = 2;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel1.Size = new System.Drawing.Size(800, 450);
        this.tableLayoutPanel1.TabIndex = 4;
        //
        // Form1
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.tableLayoutPanel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.tableLayoutPanel1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
      }
         public Form1()
         {
             InitializeComponent();
         }

         private void button2_Click(object sender, EventArgs e)
         {
             Application.Exit();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             label1.Text = "Hello World!";
         }

    #endregion
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
  }

    public class Program
    {
        /// 
        /// 应用程序的主入口点。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4