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颜料按钮

自动调出颜色选取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控件

它通过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框

用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列表

这框比较有特色,可显示图片和文本。图片用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不太相同。

    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自己有许多基础库

还有DOTNET的库

还有其它第三方库

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/337542.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

AI嵌入式K210项目(17)-快速傅里叶变换加速器 (FFT)

文章目录 前言一、什么是傅里叶变换?二、K210的快速傅里叶变换加速器实验过程总结 前言 K210内置了丰富的加速器,包括神经网络处理器 (KPU),AES(高级加密加速器),APU 麦克风阵列语音数据加速计算处理器,现场可编程 IO…

Netty-Netty源码分析流程图

netty服务端流程图 补充 完善客户端

AttributeError: module ‘numpy‘ has no attribute ‘float‘解决方案

大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的…

《WebKit 技术内幕》之六(2): CSS解释器和样式布局

2 CSS解释器和规则匹配 在了解了CSS的基本概念之后,下面来理解WebKit如何来解释CSS代码并选择相应的规则。通过介绍WebKit的主要设施帮助理解WebKit的内部工作原理和机制。 2.1 样式的WebKit表示类 在DOM树中,CSS样式可以包含在“style”元素中或者使…

java数据类型

基本数据类型:(8种) 整型:byte、short、int、long 浮点型:float、double 字符型:char 布尔类型:boolean 1、整数类型(整型) 2、浮点类型 细节:如果是直接查询得到的小数或者直接赋值…

C++入门学习(十)如何显示浮点数的完整形态

在C中&#xff0c;如果你想要显示浮点数的完整数字&#xff08;包括小数部分和指数部分&#xff09;&#xff0c;可以使用 std::setprecision 和 std::fixed 来设置精度和固定小数点表示&#xff1a; #include <iostream> #include <iomanip> // 必须包含这个头…

Python实现离散选择Logit模型(Logit算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 Logit模型&#xff08;Logit model&#xff0c;也译作“评定模型”&#xff0c;“分类评定模型”&…

小程序使用echarts图表-雷达图

本文介绍下小程序中如何使用echarts 如果是通过npm安装&#xff0c;这样是全部安装的&#xff0c;体积有点大 我这边是使用echarts中的一个组件来实现的&#xff0c;下边是具体流程&#xff0c;实际效果是没有外边的红色边框的&#xff0c;加红色边框的效果是这篇说明 1.echa…

测试覆盖与矩阵

4. Coverage - 衡量测试的覆盖率 我们已经掌握了如何进行单元测试。接下来&#xff0c;一个很自然的问题浮现出来&#xff0c;我们如何知道单元测试的质量呢&#xff1f;这就提出了测试覆盖率的概念。覆盖率测量通常用于衡量测试的有效性。它可以显示您的代码的哪些部分已被测…

【SpringBoot技术专题】「开发实战系列」Undertow web容器的入门实战及调优方案精讲

Undertow web容器的入门实战及调优方案精讲 Undertow web容器Undertow 介绍官网API给出一句话概述Undertow&#xff1a;官网API总结特点&#xff1a;Lightweight&#xff08;轻量级&#xff09;HTTP Upgrade Support&#xff08;支持http升级&#xff09;、HTTP/2 Support支持H…

【Linux 内核源码分析】堆内存管理

堆 堆是一种动态分配内存的数据结构&#xff0c;用于存储和管理动态分配的对象。它是一块连续的内存空间&#xff0c;用于存储程序运行时动态申请的内存。 堆可以被看作是一个由各个内存块组成的堆栈&#xff0c;其中每个内存块都有一个地址指针&#xff0c;指向下一个内存块…

学生公寓智能控电系统的重要性

学生公寓智能控电系统石家庄光大远通电气有限公司学生宿舍内漏电&#xff0c;超负荷用电&#xff0c;违规用电等现象一直是困扰后勤管理的普遍问题。随着学生日常生活方式以及生活用品的改变&#xff0c;电脑以及各种电器逐渐的普及&#xff0c;导致用电量与日俱增&#xff0c;…

竞赛保研 机器视觉人体跌倒检测系统 - opencv python

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 机器视觉人体跌倒检测系统 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f947;学长这里给一个题目综合评分(每项满分5分) 难度系数&…

翻译: Anaconda 与 miniconda的区别

Anaconda 和 miniconda 是广泛用于数据科学的软件发行版&#xff0c;用于简化包管理和部署。 1. 主要有两个区别&#xff1a; packages包数量&#xff1a; Anaconda 附带了 150 多个数据科学包&#xff0c;而 miniconda 只有少数几个。Interface接口&#xff1a;Anaconda 有…

mfc110.dll丢失是什么意思?全面解析mfc110.dll丢失的解决方法

在使用计算机的过程中&#xff0c;用户可能会遭遇一个常见的困扰&#xff0c;即系统提示无法找到mfc110.dll文件。这个动态链接库文件&#xff08;DLL&#xff09;是Microsoft Foundation Classes&#xff08;MFC&#xff09;库的重要组成部分&#xff0c;对于许多基于Windows的…

node.js项目express的初始化

目录 1.初始化项目2.配置跨域3.开始编写API3.1准备3.2路由处理函数router_make下的user.js3.3路由模块router下的user.js3.4入口文件app.js里面去新增这段代码3.5启动项目进行测试 &#x1f44d; 点赞&#xff0c;你的认可是我创作的动力&#xff01; ⭐️ 收藏&#xff0c;你…

设备管理——WinCC 给你神助功

要实现“设备高效”&#xff0c;就必须“管之有道”&#xff0c;来自设备层的数据支撑将是必不可少的&#xff0c;提高设备效能的2个关键在于降低平时停机时间 (MDT) 和提高平均无故障时间 (MTBF)。通常来说&#xff0c;设备维护可大致可分为三个层次&#xff1a;纠正性维护&am…

[Tool] python项目中集成使用Firebase推送功能

背景介绍 目前&#xff0c;App推送功能已经非常普遍&#xff0c;几乎所有App都有推送功能。推送功能可以自己实现&#xff0c;也可以使用第三方提供的推送服务&#xff08;免费的收费的都有&#xff09;。本文主要介绍使用Firebase提供的推送服务Firebase Cloud Messaging&…

matlab appdesigner系列-常用13-标签

标签&#xff0c;用来显示各类文本 此示例&#xff0c;就是在标签之外的画布上单击鼠标左键&#xff0c;显示王勃的《滕王阁诗》 操作如下&#xff1a; 1&#xff09;将2个标签拖拽到画布上&#xff0c;并修改相应文字。将第二个标签的右侧文本信息中的Wordwrap打开&#xf…

[自动化分布式] Zabbix自动发现与自动注册

abbix 自动发现&#xff08;对于 agent2 是被动模式&#xff09; zabbix server 主动的去发现所有的客户端&#xff0c;然后将客户端的信息登记在服务端上。 缺点是如果定义的网段中的主机数量多&#xff0c;zabbix server 登记耗时较久&#xff0c;且压力会较大 部署 添加zabb…
最新文章