Ubuntu20.4 Mono C# gtk 编程习练笔记(三)

这篇具有很好参考价值的文章主要介绍了Ubuntu20.4 Mono C# gtk 编程习练笔记(三)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Mono对gtk做了很努力的封装,即便如此仍然与System.Windows.Form中的控件操作方法有许多差异,这是gtk本身特性或称为特色决定的。下面是gtk常用控件在Mono C#中的一些用法。

Button控件

在工具箱中该控件的clicked信号双击后自动生成回调函数prototype,下面的函数当Button12点击后其标签名变为"Button12 is Pressed!"。还有ToggleButton,ImageButton 用法类似。

    protected void OnButton12Clicked(object sender, EventArgs e)
    {
        Button12.Label = "Button12 is Pressed!";
    }

Entry控件

用法与Winform的TextBox非常相似。

    protected void OnButton13Clicked(object sender, EventArgs e)
    {
        string sText = "";
        entry2.Text = "Hello";
        sText = entry2.Text;
    }

Checkbutton和Radiobutton

读:当选中后,它的Active属性是true ; 未选中时,它的Active属性是false。

写:Checkbutton1.Active = true; Radiobutton1.Active = true;

ColorButton颜料按钮

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

自动调出颜色选取dialog,用colorbutton1.Color.Red 读入红色值,或Gr

een/Blue读取绿色和蓝色值。因为调出的是dialog,所以用其它Button调用dialog功效是类同的。

    protected void OnColorbutton1ColorSet(object sender, EventArgs e)
    {
        var redcolor = colorbutton1.Color.Red;
        var greencolor = colorbutton1.Color.Green;
        var bluecolor = colorbutton1.Color.Blue;
    }

下面是通过 ColorSelectionDialog 方式调出颜料盒对话框,用后直接dispose扔给收垃圾的,不需要destroy打砸它。C#是通过runtime执行的,不是CLR平台管理的要自己处理垃圾,自动回收是管不了的。

   protected void OnButton3Clicked(object sender, EventArgs e)
    {
        Gtk.ColorSelectionDialog colorSelectionDialog = new Gtk.ColorSelectionDialog("Color selection dialog");
        colorSelectionDialog.ColorSelection.HasOpacityControl = true;
        colorSelectionDialog.ColorSelection.HasPalette = true;
        colorSelectionDialog.ColorSelection.CurrentColor = StoreColor;

        if (colorSelectionDialog.Run() == (int)ResponseType.Ok)
        {
            StoreColor = colorSelectionDialog.ColorSelection.CurrentColor;
            var redcolor = colorSelectionDialog.ColorSelection.CurrentColor.Red;
            var greencolor = colorSelectionDialog.ColorSelection.CurrentColor.Green;
            var bluecolor = colorSelectionDialog.ColorSelection.CurrentColor.Blue;
        }
        colorSelectionDialog.Dispose();
    }

 FontButton控件

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

它通过FontName返回字体名称、字型、字号,自动调用字体选择对话框。

    protected void OnFontbutton1FontSet(object sender, EventArgs e)
    {
        entry1.Text = fontbutton1.FontName;
    }

也可以使用其它钮调用字体选择对话框,用后Dispose()掉。

    protected void OnButton2Clicked(object sender, EventArgs e)
    {
        Gtk.FontSelectionDialog fontSelectionDialog = new Gtk.FontSelectionDialog("Font selection");
        if (fontSelectionDialog.Run() == (int)ResponseType.Ok)
        {
            entry1.Text = fontSelectionDialog.FontName;
        }
        fontSelectionDialog.Dispose();
    }

ComboBox框

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

用InsertText用AppendText添加新内容,用RemoveText方法删除指定项,用Active属性选中指定项显示在显示框中。ComboBox框类似于Winform的ListBox,是不能输入的。

    protected void OnButton7Clicked(object sender, EventArgs e)
    {
        combobox1.InsertText(0, "Item - 1");
        combobox1.InsertText(0, "Item - 2");
        combobox1.InsertText(0, "Item - 3");
        combobox1.InsertText(0, "Item - 4");
        combobox1.InsertText(0, "Item - 5");
        combobox1.InsertText(0, "Item - 6");
        combobox1.InsertText(0, "Item - 7");
        combobox1.InsertText(0, "Item - 8");
        combobox1.Active = 5;
    }

ComboBoxEntry框

ComboBoxEntry框是可输入的,用法同ComboBox。

TreeView列表

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

这框比较有特色,可显示图片和文本。图片用Gdk.Pixbuf装载,是个特殊类型。

    protected void OnButton15Clicked(object sender, EventArgs e)
    {
        Gtk.ListStore listStore = new Gtk.ListStore(typeof(Gdk.Pixbuf), typeof(string), typeof(string));
        treeview1.Model = null;
        treeview1.AppendColumn("Icon", new Gtk.CellRendererPixbuf(), "pixbuf", 0);
        treeview1.AppendColumn("Artist", new Gtk.CellRendererText(), "text", 1);
        treeview1.AppendColumn("Title", new Gtk.CellRendererText(), "text", 2);
        listStore.AppendValues(new Gdk.Pixbuf("sample.png"), "Rupert", "Yellow bananas");
        treeview1.Model = listStore;
        listStore.Dispose();
    }

DrawArea Cairo 图像

是做图用的,先创建surface,可以在内存中、图片上、pdf上、DrawArea上画图或写字,也可以存成png图片,图形可以变换座标。在内存中绘图,然后在DrawAre的context上show显示,或在其它地方显示。功能很灵活,与GDI在bitmap上做图,通过hdc显示在pictureBox上有对比性。

    protected void OnButton11Clicked(object sender, EventArgs e)
    {
        //
        // Creates an Image-based surface with with data stored in
        // ARGB32 format.  
        //
        drawingarea1Width = drawingarea1.Allocation.Width;
        drawingarea1Height = drawingarea1.Allocation.Height;
        ImageSurface surface = new ImageSurface(Format.ARGB32, drawingarea1Width, drawingarea1Height);

        // Create a context, "using" is used here to ensure that the
        // context is Disposed once we are done
        //
        //using (Context ctx = new Cairo.Context(surface))
        using (Context ctx = Gdk.CairoHelper.Create(drawingarea1.GdkWindow))
        {
            // Select a font to draw with
            ctx.SelectFontFace("serif", FontSlant.Normal, FontWeight.Bold);
            ctx.SetFontSize(32.0);

            // Select a color (blue)
            ctx.SetSourceRGB(0, 0, 1);
            //ctx.LineTo(new PointD(iArea1ObjX, drawingarea1.Allocation.Height));
            //ctx.StrokePreserve();

            // Draw
            ctx.MoveTo(iArea1ObjX, iArea1ObjY);
            ctx.ShowText("Hello, World");


            /*
            //Drawings can be save to png picture file
            //surface.WriteToPng("test.png");

            //Context ctxArea1 = Gdk.CairoHelper.Create(drawingarea1.GdkWindow);
            //Surface surface1 = new Cairo.ImageSurface("test.png");

            //Option: coordinator change, origin 0,0 is the middle of the drawingarea
            //ctxArea1.Translate(drawingarea1Width / 2, drawingarea1Height / 2);
            //surface.Show(ctxArea1, 0, 0);

            //ctxArea1.Dispose();
            */
        }
    }

About对话框

固定格式的对话框,有贡献者、文档人员、版权说明等,与windows的about不太相同。

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

    protected void OnButton9Clicked(object sender, EventArgs e)
    {
        string[] textabout = {"abcde","fdadf","adsfasdf" };
        const string LicensePath = "COPYING.txt";

        Gtk.AboutDialog aboutDialog = new Gtk.AboutDialog();
        aboutDialog.Title = "About mono learning";
        aboutDialog.Documenters = textabout;
        //aboutDialog.License = "mit license";
        aboutDialog.ProgramName = "my Program";
        aboutDialog.Logo = new Gdk.Pixbuf("logo.png");
        //aboutDialog.LogoIconName = "logo.png";
        //aboutDialog.AddButton("New-Button", 1);
        aboutDialog.Artists = textabout;
        aboutDialog.Authors = textabout;
        aboutDialog.Comments = "This is the comments";
        aboutDialog.Copyright = "The copyright";
        aboutDialog.TranslatorCredits = "translators";
        aboutDialog.Version = "1.12";
        aboutDialog.WrapLicense = true;
        aboutDialog.Website = "www.me.com";
        aboutDialog.WebsiteLabel = "A website";
        aboutDialog.WindowPosition = WindowPosition.Mouse;
        try
        {
            aboutDialog.License = System.IO.File.ReadAllText(LicensePath);
        }
        catch (System.IO.FileNotFoundException)
        {
            aboutDialog.License = "Could not load license file '" + LicensePath + "'.\nGo to http://www.abcd.org";
        }
        aboutDialog.Run();
        aboutDialog.Destroy();
        aboutDialog.Dispose();
    }

Timer时钟

使用Glib的时钟,100ms

timerID1 = GLib.Timeout.Add(100, OnTimedEvent1);

使用System的时钟,300ms

 aTimer = new System.Timers.Timer(300);
 aTimer.Elapsed += OnTimedEvent;
 aTimer.AutoReset = true;
 aTimer.Enabled = true;

异步写文件(VB.NET)

    protected void OnButton4Clicked(object sender, EventArgs e)
    {
        Task r = Writedata();

        async Task Writedata()
        {
            await Task.Run(() =>
            {
                VB.FileSystem.FileOpen(1, "VBNETTEST.TXT", VB.OpenMode.Output, VB.OpenAccess.Write, VB.OpenShare.Shared);
                VB.FileSystem.WriteLine(1, "Hello World! - 1");
                VB.FileSystem.WriteLine(1, "Hello World! - 2");
                VB.FileSystem.WriteLine(1, "Hello World! - 3");
                VB.FileSystem.WriteLine(1, "Hello World! - 4");
                VB.FileSystem.WriteLine(1, "Hello World! - 5");
                VB.FileSystem.FileClose(1);
                return 0;
            });
        }
    }

SQLite操作

创建Table

    protected void OnButton13Clicked(object sender, EventArgs e)
    {
        const string connectionString = "URI=file:SqliteTest.db, version=3";
        IDbConnection dbcon = new SqliteConnection(connectionString);
        dbcon.Open();
        IDbCommand dbcmd = dbcon.CreateCommand();
        string sql;
            sql =
                 "CREATE TABLE employee (" +
                 "firstname nvarchar(32)," +
                 "lastname nvarchar(32))";
            dbcmd.CommandText = sql;
            dbcmd.ExecuteNonQuery();

            dbcmd.Dispose();
            dbcon.Close();
    }

INSERT记录

    protected void OnButton13Clicked(object sender, EventArgs e)
    {
        const string connectionString = "URI=file:SqliteTest.db, version=3";
        IDbConnection dbcon = new SqliteConnection(connectionString);
        dbcon.Open();
        IDbCommand dbcmd = dbcon.CreateCommand();
        string sql;
            sql =
                 "INSERT INTO employee(" +
                 "firstname, lastname)" +
                 "values('W1ang', 'B1odang')";
            dbcmd.CommandText = sql;
            dbcmd.ExecuteNonQuery();

            dbcmd.Dispose();
            dbcon.Close();
    }

循环读

    protected void OnButton13Clicked(object sender, EventArgs e)
    {
        const string connectionString = "URI=file:SqliteTest.db, version=3";
        IDbConnection dbcon = new SqliteConnection(connectionString);
        dbcon.Open();
        IDbCommand dbcmd = dbcon.CreateCommand();
        string sql;
            sql =
               "SELECT firstname, lastname " +
               "FROM employee";
            dbcmd.CommandText = sql;
            IDataReader reader = dbcmd.ExecuteReader();
            while (reader.Read())
            {
                string firstName = reader.GetString(0);
                string lastName = reader.GetString(1);
                Console.WriteLine("Name: {0} {1}",
                    firstName, lastName);
            }
            // clean up
            reader.Dispose();
            dbcmd.Dispose();
            dbcon.Close();
    }

包/项目/程序集

测试引用Windows.Form并创建了窗体和对话框,也能显示,但不是Linux平台原生的,不太美观且速度不理想。如果只是创建了不Show,让它处于Hide状态,比如带sort属性的ListBox,还是可以使用的。

Mono自己有许多基础库

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

还有DOTNET的库

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言

还有其它第三方库

Ubuntu20.4 Mono C# gtk 编程习练笔记(三),c#,笔记,开发语言文章来源地址https://www.toymoban.com/news/detail-800791.html

到了这里,关于Ubuntu20.4 Mono C# gtk 编程习练笔记(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Ubuntu20.4配置arm交叉编译环境

    我是在虚拟机中配置的,如果你的嵌入式设备足够完成自己的编译,可以不考虑虚拟机的。 新安装的Ubuntu20.04系统请先执行以下代码 到aarch64下载对应的aarch64的base镜像。 之后执行如下命令,创建armsys文件夹,之后将刚下载的镜像拷贝到该文件夹下并且解压 安装一些必要的软

    2024年02月06日
    浏览(44)
  • ubuntu20.4升级OpenSSL和OpenSSH

    参考:https://blog.csdn.net/weixin_37534043/article/details/120822689 https://blog.csdn.net/xujiamin0022016/article/details/87817124 openssl 官方下载地址: https://www.openssl.org/source/ openssh 官方下载地址:https://fastly.cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/ zlib 官方下载地址: http://www.zlib.net/ CSDN资源下载链接: http

    2024年02月01日
    浏览(81)
  • ubuntu20.4安装配置ros系统(noetic)

    不同ubuntu版本对应的ros版本 ubuntu版本 ros1版本 ros2版本 16.04 kinetic ardent 18.04 melodic dashing 20.04 noetic foxy   1、打开软件与更新,切换ubuntu软件源(国内中科大源) 2、打开终端,添加ros软件源(中科大镜像站) 3、配置公钥   4、更新软件源   5、安装ros系统(ps:命令中的noetic根

    2024年02月07日
    浏览(36)
  • ubuntu20.4 静态网络配置(保姆级图文教程)

    之前一直使用的Linux系统都是centOs,突然换成Ubuntu之后不知道怎么配置网络,网上查找了很多资料都不可用,最后终于在一篇博客里看到了20.4版本的网络配置教程,在此贴上链接,并记录 Linux ubuntu20.04 网络配置(图文教程)_ubuntu20.04网络配置_isOllie的博客-CSDN博客 系统:ubu

    2024年02月05日
    浏览(37)
  • ubuntu 20.4完全卸载opencv 安装opencv 4.4

    首先完全卸载刷机时自带的opencv旧版本,不同版本版本会引起冲突。 1. 先到opencv编译安装的目录build下 cd build sudo make uninstall cd .. sudo rm -r build 2.也可以手动删除,总之删除干净即可。 sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/sh

    2024年02月06日
    浏览(34)
  • Ubuntu20.4 docker运行stable diffusion webui

    系统环境 ubuntu20.04 nvida cuda显卡驱动 默认已经安装成功 更新系统依赖: 确认是否之前安装过docker并卸载: 启动并查看docker运行状态: 注:这一部步基本可以忽略,因为没有使用国外dockerhub镜像! 安装 nvidia-docker2 包 安装完成后需要重启docker: 能输出cuda相关信息安装成功:

    2024年02月09日
    浏览(35)
  • AMD显卡Ubuntu20.4系统下部署stable diffusion

    今天来给大家做一个AMD显卡使用stable diffusion的小白(硬核)教程。最近这段时间AI特别火,很多小伙伴看到各种大佬用AI画的老婆非常精美(色情),弄的人心痒痒,自己也想画一个心仪的老婆(画作)。 太露骨的图片不能放,你们懂得!! 但是很多小伙伴用的是AMD的独立显

    2024年02月03日
    浏览(40)
  • 基于ubuntu20.4安装谷歌拼音中文输入法

    1.首先命令行安装汉语语言包 sudo apt-get install language-pack-zh-hans 执行该命令后,系统就会自动安装所需要的汉语语言包 图1 安装汉语语言包 2.然后命令行安装谷歌拼音输入法 sudo apt-get install fcitx-googlepinyin 执行该命令后,系统就会自动安装 fcitx 和 goolgepinyin 程序,也同时会安装

    2024年02月06日
    浏览(36)
  • ubuntu20.4源码安装最新gcc(gcc V12.2)

            Ubuntu20.4自带的gcc为V9.4.0的版本,老版本的gcc存在不支持新版本C语言规范,在配置、编译、安装应用软件或工具时,可能存在不能正确安装的问题。(比如:libpqxx-7.7.4在配置时要求c++17版本的支持,但gcc V9.4.0版本不支持c++17)。因此本文介绍ubuntu20.4的系统下载最新

    2024年02月01日
    浏览(65)
  • Ubuntu20.4 WSL2 无法访问github终极解决方案

    sudo rm /etc/resolv.conf sudo bash -c \\\'echo \\\"nameserver 8.8.8.8\\\" /etc/resolv.conf\\\' sudo bash -c \\\'echo \\\"[network]\\\" /etc/wsl.conf\\\' sudo bash -c \\\'echo \\\"generateResolvConf = false\\\" /etc/wsl.conf\\\'      

    2024年02月07日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包