Mono提倡使用Gtk来制作跨平台的UI,Gtk编程有点像java,都有自己的容器类,如果想摆出合适的界面就必须正确的使用Gtk中的容器类,
常用的有如下如 Gtk.Box
就有 Vbox
using System;
using Gtk;
class VBoxTester {
static void Main ()
{
Application.Init ();
Window myWindow = new Window ("VBox Widget");
VBox myBox = new VBox (false, 4);
//Add the box to a Window container
myWindow.Add (myBox);
AddButton (myBox);
AddButton (myBox);
AddButton (myBox);
myWindow.ShowAll ();
Application.Run ();
}
static void AddButton (VBox box)
{
box.PackStart (new Button ("Button"), true, false, 0);
}
}
同样也有Hbox
using System;
using Gtk;
class HBoxTester {
static void Main ()
{
Application.Init ();
Window myWindow = new Window ("HBox Widget");
HBox myBox = new HBox (false, 4);
//Add some buttons to the horizontal box
AddButton (myBox);
AddButton (myBox);
//Add the box to a Window container
myWindow.Add (myBox);
myWindow.ShowAll ();
Application.Run ();
}
static void AddButton (HBox box)
{
box.PackStart (new Button ("Button"), true, false, 0);
}
}
其中还有一个比较常用的旧Table,
它的坐标系统如下:
0(leftAttach) 1 2(rightAttach)
(topAttach)
0+---------------------+-----------------------+
| | |
1+---------------------+-----------------------+
| | |
2+---------------------+-----------------------+
(bottomAttach)
当要加空间进去的时候,应该指明这个空间在表格里的大小
使用
table1.Attach ( Widget child,
int leftAttach,
int rightAttach,
int topAttach,
int bottomAttach,
);
namespace GtkSharpTutorial {
using Gtk;
using System;
using System.Drawing;
public class table
{
static void callback( object obj, EventArgs args)
{
Button mybutton = (Button) obj;
Console.WriteLine("Hello again - {0} was pressed", (string) mybutton.Label);
}
static void delete_event (object obj, DeleteEventArgs args)
{
Application.Quit();
}
static void exit_event (object obj, EventArgs args)
{
Application.Quit();
}
public static void Main(string[] args)
{
Application.Init ();
Window window = new Window ("Table");
window.DeleteEvent += delete_event;
window.BorderWidth= 20;
Table table = new Table (2, 2, true);
window.Add(table);
Button button = new Button("button 1");
button.Clicked += callback;
*/
table.Attach(button, 0, 1, 0, 1);
button.Show();
Button button2 = new Button("button 2");
button2.Clicked += callback;
*/
table.Attach(button2, 1, 2, 0, 1);
button2.Show();
Button quitbutton = new Button("Quit");
quitbutton.Clicked += exit_event;
table.Attach(quitbutton, 0, 2, 1, 2);
quitbutton.Show();
table.Show();
window.ShowAll();
Application.Run();
}
}
}
编译以上的代码的时候 因为用了Gtk所以要显示引用Gtk库
mcs –pkg:gtk-sharp Demo.cs