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

news2024/10/7 2:22:55

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.coloradmin.cn/o/1400273.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

spawn_group | spawn_group_template | linked_respawn

字段介绍 spawn_group | spawn_group_template 用来记录与脚本事件或boss战斗有关的 creatures | gameobjects 的刷新数据linked_respawn 用来将 creatures | gameobjects 和 boss 联系起来,这样如果你杀死boss, creatures | gameobjects 在副本重置之前…

华为OD机试 - 查找一个有向网络的头节点和尾节点(Java JS Python C)

题目描述 给定一个有向图,图中可能包含有环,图使用二维矩阵表示,每一行的第一列表示起始节点,第二列表示终止节点,如 [0, 1] 表示从 0 到 1 的路径。 每个节点用正整数表示。 求这个数据的首节点与尾节点,题目给的用例会是一个首节点,但可能存在多个尾节点。同时图中…

万户OA-senddocument_import.jsp任意文件上传

0x01阅读须知 本文章仅供参考,此文所提供的信息只为网络安全人员对自己所负责的网站、服务器等(包括但不限于)进行检测或维护参考。本文章仅用于信息安全防御技术分享,因用于其他用途而产生不良后果,作者不承担任何法律责任&#…

systemverilog/verilog文件操作

1、Verilog文件操作 Verilog具有系统任务和功能,可以打开文件、将值输出到文件、从文件中读取值并加载到其他变量和关闭文件。 1.1 、Verilog文件操作 1.1.1、打开和关闭文件 module tb; // 声明一个变量存储 file handler integer fd; initial begin // 以写权限打开一个文…

翻译: Anaconda 与 miniconda的区别

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

VC++中使用OpenCV进行颜色检测

VC中使用OpenCV进行颜色检测 在VC中使用OpenCV进行颜色检测非常简单,首选读取一张彩色图像,并调用函数cvtColor(img, imgHSV, COLOR_BGR2HSV);函数将原图img转换成HSV图像imgHSV,再设置好HSV三个分量的上限和下限值,调用inRange函…

android使用相机 intent.resolveActivity returns null

问题 笔者使用java进行android开发,启动相机时 intent.resolveActivity returns null takePictureIntent.resolveActivity(getPackageManager()) null详细问题 笔者使用如下代码启动相机 // 启动相机SuppressLint("LongLogTag")private void dispatc…

计算机网络——第四层:传输层以及TCP UDP

1. 传输层的协议 1.1 TCP (传输控制协议) - rfc793 连接模式的传输。 保证按顺序传送数据包。 流量控制、错误检测和在数据包丢失时的重传。 用于需要可靠传输的应用,如网络(HTTP/HTTPS)、电子邮件(SMTP, IMAP, POP3)…

TensorRT部署-Windows环境配置

系列文章目录 文章目录 系列文章目录前言一、安装Visual Studio (2019)二、下载和安装nvidia显卡驱动三、下载CUDA四、下载安装cuDNN五、安装Anaconda六、TensorRT安装七、安装Opencv八、Cmake 配置总结 前言 TensorRT部署-Windows环境配置 一、安装Vis…

微服务不死 — 共享变量在策略引擎项目的落地详解

01 背景 1、共享变量的提出 前段时间,来自亚马逊 Prime Video 团队的一个案例研究在开发者社区中掀起了轩然大波。大体是这样一件事,作为一个流媒体平台,Prime Video每天都会向客户提供成千上万的直播流。为了确保客户无缝接收内容&#xff0…

一、用户管理中心——前端初始化

一、Ant Design Pro初始化 1.创建空文件夹 2.打开Ant Design Pro官网 3.打开终端进行初始化 在终端输入npm i ant-design/pro-cli -g 在终端输入pro create myapp 选择umi3 选择simple 项目创建成功后,在文件夹中出现myapp 4.安装依赖 使用vscode打开项目 …

jquery动态引入js和css

直接上代码吧&#xff0c;但是有时候这个方法会失败&#xff0c;js文件里面的方法不生效&#xff0c;原因还在找 // 动态引入cssvar cssFileUrl index.css;$("head").append("<link>");css $("head").children(":last");css.a…

【C++干货铺】C++11新特性——lambda表达式 | 包装器

个人主页点击直达&#xff1a;小白不是程序媛 C系列专栏&#xff1a;C干货铺 代码仓库&#xff1a;Gitee 目录 C98中的排序 lambda表达式 lambda表达式语法 表达式中的各部分说明 lambda表达式的使用 基本的使用 [var]值传递捕捉变量var ​编辑 [&var]引用传递捕…

AI教我学编程之C#类的实例化与访问修饰符

前言 在这篇文章中&#xff0c;我将带大家深入了解C#编程语言的核心概念&#xff0c;包括类的实例化、访问修饰符的应用&#xff0c;以及C#中不同数据类型的默认值。我会通过逐步分析和具体实例&#xff0c;详细解释如何在C#中正确创建和操作对象&#xff0c;并探讨如何通过访…

【实操】基于 GitHub Pages + Hexo 搭建个人博客

《开发工具系列》 【实操】基于 GitHub Pages Hexo 搭建个人博客 一、引言二、接入 Node.js2.1 下载并安装 Node.js2.2 环境变量配置 三、接入 Git3.1 下载并安装 Git3.2 环境变量配置 四、接入 Hexo4.1 安装 Hexo4.2 建站4.3 本地启动服务器 五、接入 GitHub Pages5.1 初识 G…

C#调用C动态链接库

前言 已经没写过博客好久了&#xff0c;上一篇还是1年半前写的LTE Gold序列学习笔记&#xff0c;因为工作是做通信协议的&#xff0c;然后因为大学时没好好学习专业课&#xff0c;现在理论还不扎实&#xff0c;不敢瞎写&#xff1b; 因为工作原因&#xff0c;经常需要分析一些字…

在k8s上部署ClickHouse

概述 clickhouse的容器化部署&#xff0c;已经有非常成熟的生态了。在一些互联网大厂也已经得到了大规模的应用。 clickhouse作为一款数据库&#xff0c;其容器化的主要难点在于它是有状态的服务&#xff0c;因此&#xff0c;我们需要配置PVC。 目前业界比较流行的部署方式有…

实时云渲染服务:流式传输 VR 和 AR 内容

想象一下无需专用的物理计算机&#xff0c;甚至无需实物连接&#xff0c;就能获得高质量的 AR/VR 体验是种什么样的体验&#xff1f; 过去&#xff0c;与 VR 交互需要专用的高端工作站&#xff0c;并且根据头显、壁挂式传感器和专用的物理空间。VR 中的复杂任务会突破传感器范…

AI相关资料

文心一格收费,有免费额度 通义万相_AI创意作画_AI绘画_人工智能-阿里云 AI AIchatOS 即时 AI - 生成式图像创作及 UI 设计工具 Framer — The internet is your canvas

分布式锁的产生以及使用

日常开发中&#xff0c;针对一些需要锁定资源的操作&#xff0c;例如商城的订单超卖问题、订单重复提交问题等。 都是为了解决在资源有限的情况限制客户端的访问&#xff0c;对应的是限流。 单节点锁问题 目前针对这种锁资源的情况采取的往往是互斥锁&#xff0c;例如 java 里…