百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

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

bigegpt 2024-08-16 14:10 2 浏览

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
  {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

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

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要修改
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    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
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

相关推荐

了解Linux目录,那你就了解了一半的Linux系统

大到公司或者社群再小到个人要利用Linux来开发产品的人实在是多如牛毛,每个人都用自己的标准来配置文件或者设置目录,那么未来的Linux则就是一团乱麻,也对管理造成许多麻烦。后来,就有所谓的FHS(F...

Linux命令,这些操作要注意!(linux命令?)

刚玩Linux的人总觉得自己在演黑客电影,直到手滑输错命令把公司服务器删库,这才发现命令行根本不是随便乱用的,而是“生死簿”。今天直接上干货,告诉你哪些命令用好了封神!喜欢的一键三连,谢谢观众老爷!!...

Linux 命令速查手册:这 30 个高频指令,拯救 90% 的运维小白!

在Linux系统的世界里,命令行是强大的武器。对于运维小白而言,掌握一些高频使用的Linux命令,能极大提升工作效率,轻松应对各种系统管理任务。今天,就为大家奉上精心整理的30个Linu...

linux必学的60个命令(linux必学的20个命令)

以下是Linux必学的20个基础命令:1.cd:切换目录2.ls:列出文件和目录3.mkdir:创建目录4.rm:删除文件或目录5.cp:复制文件或目录6.mv:移动/重命名文件或目录7....

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

15 个实用 Linux 命令(linux命令用法及举例)

Linux命令行是系统管理员、开发者和技术爱好者的强大工具。掌握实用命令不仅能提高效率,还能解锁Linux系统的无限潜力,本文将深入介绍15个实用Linux命令。ls-列出目录内容l...

Linux 常用命令集合(linux常用命令全集)

系统信息arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DM...

Linux的常用命令就是记不住,怎么办?

1.帮助命令1.1help命令#语法格式:命令--help#作用:查看某个命令的帮助信息#示例:#ls--help查看ls命令的帮助信息#netst...

Linux常用文件操作命令(linux常用文件操作命令有哪些)

ls命令在Linux维护工作中,经常使用ls这个命令,这是最基本的命令,来写几条常用的ls命令。先来查看一下使用的ls版本#ls--versionls(GNUcoreutils)8.4...

Linux 常用命令(linux常用命令)

日志排查类操作命令查看日志cat/var/log/messages、tail-fxxx.log搜索关键词grep"error"xxx.log多条件过滤`grep-E&#...

简单粗暴收藏版:Linux常用命令大汇总

号主:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部下午好,我的网工朋友在Linux系统中,命令行界面(CLI)是管理员和开发人员最常用的工具之一。通过命令行,用户可...

「Linux」linux常用基本命令(linux常用基本命令和用法)

Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们。总结送免费学习资料(包含视频、技术学习路线图谱、文档等)1、显示日期的指令:d...

Linux的常用命令就是记不住,怎么办?于是推出了这套教程

1.帮助命令1.1help命令#语法格式:命令--help#作用:查看某个命令的帮助信息#示例:#ls--help查看ls命令的帮助信息#netst...

Linux的30个常用命令汇总,运维大神必掌握技能!

以下是Linux系统中最常用的30个命令,精简版覆盖日常操作核心需求,适合快速掌握:一、文件/目录操作1.`ls`-列出目录内容`ls-l`(详细信息)|`ls-a`(显示隐藏文件)...

Linux/Unix 系统中非常常用的命令

Linux/Unix系统中非常常用的命令,它们是进行文件操作、文本处理、权限管理等任务的基础。下面是对这些命令的简要说明:**文件操作类:*****`ls`(list):**列出目录内容,显...