界面設(shè)計(jì)習(xí)練后,下面寫一些程序設(shè)計(jì)心得。
程序結(jié)構(gòu)
先看一下程序總體結(jié)構(gòu),先在program.cs中找到main入口,在命名空間下是MainClass類,Main函數(shù)進(jìn)入后首先建立應(yīng)用程序環(huán)境 Application.Init,然后對(duì)MainWindow進(jìn)行實(shí)例化,顯示窗體并運(yùn)行程序 Application.Run()。
//Program.cs
using System;
using Gtk;
namespace csharp3
{
class MainClass
{
public static void Main(string[] args)
{
Application.Init();
MainWindow win = new MainWindow();
win.Show();
Application.Run();
}
}
}
main -> application init -> mainwindow{builder} ->application run/quit
上述過(guò)程中,mainwindow如下,它通過(guò)gtk庫(kù)產(chǎn)生一個(gè)窗體及窗體包含的其它widgets,然后連通widgets的信號(hào) - 屬性和事件。這其中用到一個(gè) Build() 函數(shù),在C調(diào)用中常命名為 builder的指針,在gnome-builder構(gòu)建器中稱之為 template ,將界面設(shè)計(jì)器的設(shè)計(jì)文件變成程序來(lái)實(shí)現(xiàn)。public partial class MainWindow, 指明這只是 partial,一部分內(nèi)容,還有一部分由 Build() 產(chǎn)生。在linux中,Gtk界面庫(kù)與用戶程序之間一般都是這種方式。
//mainwindow.cs
using System;
using Gtk;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}
protected void OnButton5Clicked(object sender, EventArgs e)
{
entry2.Text = "Hello!";
}
protected void OnButton6Clicked(object sender, EventArgs e)
{
Application.Quit();
}
}
也可以不用設(shè)計(jì)器,自己寫build的內(nèi)容。Mono的設(shè)計(jì)器支持C#,但不支持VB.NET,而Mono本身是支持VB.NET的。因此,如果在Mono上用VB.NET開發(fā)的話,需要自己寫build后的界面語(yǔ)句。大至是下面的樣子。首先產(chǎn)生個(gè)Window,然后向window中加入控件并顯示控件,如果需要產(chǎn)生事件則將事件與回調(diào)函數(shù)過(guò)程的地址連起來(lái)(Addressof),最后show的是window。除了主窗體給個(gè)初始尺寸,控件本身一般都是由sizer,比如vbox等去控制的。習(xí)慣于linux界面這種方式后,可以不用界面設(shè)計(jì)器,腦子想它是什么樣就add上什么widget,個(gè)別屬性調(diào)整一下,fill和expand用好,加控件時(shí)pack靠start或靠end,最后試運(yùn)行調(diào)整一下就可以了。
Dim window As Gtk.Window = New Gtk.Window("Buttons")
AddHandler window.DeleteEvent, AddressOf OnDeleteEvent
window.BorderWidth = 0
window.Resize(800, 600)
window.WindowPosition = WindowPosition.Center
Dim box1 As VBox = New VBox(False, 10)
window.Add(box1)
box1.Show()
box1.Homogeneous = False
Dim box2 As VBox = New VBox(False, 10)
box2.BorderWidth = 10
box1.PackStart(box2, True, True, 0)
box2.Show()
button1 = New Button("Button 1")
box2.PackStart(button1, True, True, 0)
button1.Show()
AddHandler button1.Clicked, AddressOf OnButton1Clicked
Dim button2 As Button = New Button("Button 2")
'button2.Active = true
box2.PackStart(button2, True, True, 0)
button2.Show()
Dim separator As HSeparator = New HSeparator()
box1.PackStart(separator, False, True, 0)
separator.Show()
Dim box3 As VBox = New VBox(False, 10)
box3.BorderWidth = 10
box1.PackStart(box3, False, True, 0)
Dim button3 As Button = New Button("Close")
AddHandler button3.Clicked, AddressOf OnExitButtonEvent
Dim entry1 As Entry = new Entry("Please")
box3.PackStart(entry1, True, True, 0)
entry1.Show()
box3.PackStart(button3, True, True, 0)
button3.CanDefault = True
button3.GrabDefault()
button3.Show()
Mono C#編程中,可以不關(guān)注Build后的內(nèi)容,它們?cè)诹硗庖粋€(gè).cs中。如果抽象一些理解的話,builder是圖中的樣子,像個(gè)黑盒子接線箱,編程時(shí)連通相應(yīng)的信號(hào)即可。
C#有設(shè)計(jì)器還是比VB.NET方便一些,不過(guò)在C#中也可以使用VB.NET功能,在引用中加上:
using?VB?=?Microsoft.VisualBasic
引用后即產(chǎn)生新的命名空間VB,比如在C#中使用VB.NET的文件讀寫,可以寫成:
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);
微軟的Microsoft.VisualBasic命名空間中VB.NET功能繁多,喜歡VB.NET編程的話可以把它引用到進(jìn)程序中,Mono的C#設(shè)計(jì)器也就間接地支持VB.NET語(yǔ)言開發(fā)了。
用戶程序
默認(rèn)的MainWindow.cs產(chǎn)生MainWindow:Gtk.Window類,里面有個(gè)MainWindow()方法,方法中首先是Build(),自己的程序一般使用這個(gè)類,加入一些類內(nèi)變量,在類內(nèi)加入一些方法等。需要的話也可另僻空間,用另類,用另類方法等。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-812111.html
//MainWindow.cs
using System;
using Gtk;
using System.Threading.Tasks;
using VB = Microsoft.VisualBasic;
using Cairo;
public partial class MainWindow : Gtk.Window
{
//Store previous background color of drawingarea1
private Gdk.Color StoreColor = new Gdk.Color(255, 255, 255);
private int iArea1ObjX = 10;
private int iArea1ObjY = 50;
private uint timerID1, timerID2;
private ImageSurface surfacepub;
private Context ctxpub;
private Context ctxArea1;
private int drawingarea1Width;
private int drawingarea1Height;
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
drawingarea1.AppPaintable = true;
CreateContext();
timerID1 = GLib.Timeout.Add(100, OnTimedEvent1);
timerID2 = GLib.Timeout.Add(100, OnTimedEvent2);
/*
aTimer = new System.Timers.Timer(300);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
*/
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
DestroyContext();
Gtk.Application.Quit();
a.RetVal = true;
}
protected void OnButton16Clicked(object sender, EventArgs e)
{
DestroyContext();
Gtk.Application.Quit();
}
protected void OnColorbutton1ColorSet(object sender, EventArgs e)
{
var redcolor = colorbutton1.Color.Red;
var greencolor = colorbutton1.Color.Green;
var bluecolor = colorbutton1.Color.Blue;
}
先寫這么多,一些常用widgets使用方法在下篇心得中寫。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-812111.html
到了這里,關(guān)于Ubuntu20.4 Mono C# gtk 編程習(xí)練筆記(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!