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

2024-01-21 11:12

本文主要是介绍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的库

还有其它第三方库

这篇关于Ubuntu20.4 Mono C# gtk 编程习练笔记(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/629332

相关文章

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念