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 withctx.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();// Drawctx.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 upreader.Dispose();dbcmd.Dispose();dbcon.Close();}

包/项目/程序集

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

Mono自己有许多基础库

还有DOTNET的库

还有其它第三方库

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

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

相关文章

【前后端分离与不分离的区别】

Web 应用的开发主要有两种模式: 前后端不分离 前后端分离 理解它们的区别有助于我们进行对应产品的测试工作。 前后端不分离 在早期,Web 应用开发主要采用前后端不分离的方式,它是以后端直接渲染模板完成响应为主的一种开发模式。以前后端不…

【华为GAUSS数据库】IDEA连接GAUSS数据库方法

背景:数据库为华为gauss for opengauss 集中式数据库 IDEA提供了丰富的各类型数据库驱动,但暂未提供Gauss数据库。可以通过以下方法进行连接。 连接后, 可以自动检查xml文件中的sql语句是否准确,表名和字段名是否正确还可以直接在…

转转交易猫自带客服多模板全开源完整定制版源码

商品发布; 请在后台商品添加成功后, 再点击该商品管理,可重新编辑当前商品的所有信息及配图以及支付等等相关信息 可点击分享或者跳转,将链接地址进行发布分享 请在手机端打开访问 访问商品主要模板文件路径目录 咸鱼&#…

【实操】基于 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…

Java--类继承

文章目录 主要内容一.学生类1.源代码代码如下(示例): 2.结果 二.交通工具类1.源代码代码如下(示例): 2.结果 三.圆类1.源代码代码如下(示例): 2.结果 总结 主要内容 学生类交通工具类圆类 一.学生类 具有…

ChatGLM vs ChatGPT

所有的NLP大模型 都是transformer结构 1.Mask attention 的策略不同 2.训练任务目标不同 国内大模型nb公司:百度、清华智谱 一、主流大模型 粉色:Encoder-only。 绿色:Encoder-Decoder,尽头智谱ChatGLM。 蓝色:…

【MongoDB】下载安装、指令操作

目录 1.下载安装 2.指令 2.1.基础操作指令 2.2.增加 2.3.查询 2.4.修改 2.5.删除 前言: 关于MongoDB的核心概念请移步: 【文档数据库】ES和MongoDB的对比-CSDN博客 1.下载安装 本文以安装Windows版本的mongodb为例,Linux版本的其实…

三、arcgispro二次开发创建第一个工程

忙了几天,总算可以创建第一工程了。 步骤一: 步骤二: 工具介绍: 项目创建成功:项目目录在解决方案资源管理器中,整个工具都是动态可调整的,如下图: 想把窗口放哪里就把鼠标移到红…

Linux重定向:深入理解与实践

🎬慕斯主页:修仙—别有洞天 ♈️今日夜电波:晴る—ヨルシカ 0:20━━━━━━️💟──────── 4:30 🔄 ◀️ ⏸ ▶️ ☰ &…

RHEL - 更新升级软件或系统

《OpenShift / RHEL / DevSecOps 汇总目录》 文章目录 小版本软件更新yum update 和 yum upgrade 的区别升级软件和升级系统检查软件包是否可升级指定升级软件使用的发行版本方法1方法2方法3方法4 查看软件升级类型更新升级指定的 RHSA/RHBA/RHEA更新升级指定的 CVE更新升级指定…

L1-069 胎压监测(Java)

小轿车中有一个系统随时监测四个车轮的胎压,如果四轮胎压不是很平衡,则可能对行车造成严重的影响。 让我们把四个车轮 —— 左前轮、右前轮、右后轮、左后轮 —— 顺次编号为 1、2、3、4。本题就请你编写一个监测程序,随时监测四轮的胎压&…

Java中的包、类、接口说明

写在开头 包、类、接口、方法、变量、参数、代码块,这些都是构成Java程序的核心部分,即便最简单的一段代码里都至少要包含里面的三四个内容,这两天花点时间梳理了一下,理解又深刻了几分。 Java中的包 Java 定义了一种名字空间,称之为包:package。一个类总是属于某个包…

Servlet系列:两种创建方式(xml,注解)

一、使用web.xml的方式配置(Servlet2.5之前使用) 在早期版本的Java EE中,可以使用XML配置文件来定义Servlet。在web.xml文件中,可以定义Servlet的名称、类名、初始化参数等。然后,在Java代码中实现Servlet接口&#x…

wayland(xdg_wm_base) + egl + opengles 最简实例

文章目录 前言一、ubuntu 下相关环境准备1. 获取 xdg_wm_base 依赖的相关文件2. 查看 ubuntu 上安装的opengles 版本3. 查看 weston 所支持的 窗口shell 接口种类二、xdg_wm_base 介绍三、egl_wayland_demo1.egl_wayland_demo2_0.c2.egl_wayland_demo3_0.c3. xdg-shell-protoco…

Chatopera 云服务支持大语言模型对话(LLM),定制您的聊天机器人

2024 年,Chatopera 云服务继续不断完善,为开发者提供最好的定制聊天机器人的工具。在过去的一年,用户们反映最多的建议是 Chatopera 云服务内置大语言模型的对话,今天 Chatopera 云服务完成了产品升级,满足了这个诉求。…

HCIP-BGP选路实验

一.实验拓扑图 二.详细配置 R1 interface GigabitEthernet0/0/0 ip address 12.1.1.1 255.255.255.0interface LoopBack0 ip address 1.1.1.1 255.255.255.0interface LoopBack1 ip address 10.1.1.1 255.255.255.0bgp 1 router-id 1.1.1.1 peer 12.1.1.2 as-number 2ipv4-fa…

0基础开发EtherNet/IP:协议格式,JAVA、C#、C++处理

经过一阵倒腾,把CIP、Ethernet/ip协议搞到手 协议的概念和理论就不提及了,上网随便一搜索EtherNet/IP遍地都是。 直接将协议关键点列举出来吧。 更多协议资料 www.jngbus.com 通讯软件群 30806722 这里讲解的是TCP和UDP协议的格式,EtherN…

如何本地部署虚拟数字克隆人 SadTalker

环境: Win10 SadTalker 问题描述: 如何本地部署虚拟数字克隆人 SadTalker 解决方案: SadTalker:学习逼真的3D运动系数,用于风格化的音频驱动的单图像说话人脸动画 单张人像图像🙎 ♂️音频&#x1f3…

伊恩·斯图尔特《改变世界的17个方程》傅里叶变换笔记

主要是课堂的补充(yysy,我觉得课堂的教育模式真有够无聊的,PPT、写作业、考试,感受不到知识的魅力。 它告诉我们什么? 空间和时间中的任何模式都可以被看作不同频率的正弦模式的叠加。 为什么重要? 频率分量…

【图解数据结构】顺序表实战指南:手把手教你详细实现(超详细解析)

🌈个人主页:聆风吟 🔥系列专栏:图解数据结构、算法模板 🔖少年有梦不应止于心动,更要付诸行动。 文章目录 一. ⛳️线性表1.1 🔔线性表的定义1.2 🔔线性表的存储结构 二. ⛳️顺序表…