Tartalomjegyzék

< Mono

Gtk

A mono segítségével, nem csak Windows.Forms névtérrel állíthatunk elő GUI felületet. Itt rövid bevezetést találunk a Gtk használatára.

Debian GNU/Linux alatt a Gtk# használatához szükség van a gtk-sharp2 csomag telepítésére:

apt-get install gtk-sharp2

Helló Világ

using Gtk;
using System;
 
public class Program
{
    public static void Main()
    {
        Application.Init();
 
        Window ablak = new Window("Helló Világ");
        ablak.Show();
 
        Application.Run();
    }
}

fordítás:

gmcs -pkg:gtk-sharp-2.0 main.cs

Ablak örökléssel

using Gtk;
using System;
 
class Program : Window
{
	Program():base("")
	{		
		DefaultWidth = 300;
		DefaultHeight = 200;
		Show();		
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

Ablak mérete

using Gtk;
using System;
 
class Program
{
	static void Main()
	{	
		Application.Init ();		
 
		Window ablak = new Window("");
		ablak.DefaultWidth = 300;
		ablak.DefaultHeight = 200;
		ablak.Show();
 
		Application.Run();	
 
	}
}

Nyomgomb

using Gtk;
using System;
 
class Program
{
	static void Main()
	{	
		Application.Init ();		
 
		Window ablak = new Window("");
		Button kilepesgomb = new Button("Nyomj le");
		kilepesgomb.Clicked += new EventHandler(Kilepesgomb_Clicked);
		ablak.Add(kilepesgomb);
		ablak.ShowAll();
 
		Application.Run();	
 
	}
	static private void Kilepesgomb_Clicked(object sender, EventArgs e)
	{
		Application.Quit();
	}	
}

Szövegdoboz

using Gtk;
using System;
 
class Program : Window
{
 
	Entry szovegdoboz = new Entry();
	Program():base("")
	{		
 
 
		Add(szovegdoboz);
		DefaultWidth = 300;
		DefaultHeight = 200;
		ShowAll();		
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

Két komponens az ablakon

using Gtk;
using System;
 
class Program : Window
{
	VPaned paned1 = new VPaned();
	Button gomb1 = new Button("Nyomj le");
	Label cimke1 = new Label("Címke");
	Program():base("")
	{		
 
		paned1.Pack1(gomb1, true, false);
		paned1.Pack2(cimke1, true, false);
 
		Add(paned1);
		DefaultWidth = 300;
		DefaultHeight = 200;
		ShowAll();		
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

Üzenetablak

using Gtk;
using System;
 
class Program : Window
{
 
	Button gomb1 = new Button("Nyomj meg");
	Program():base("")
	{		
		gomb1.Clicked += new EventHandler(Gomb1_Clicked);
 
		Add(gomb1);
		DefaultWidth = 300;
		DefaultHeight = 200;
		ShowAll();		
	}
	private void Gomb1_Clicked(object sender, EventArgs e)
	{
 
		MessageDialog msg = new MessageDialog(this, DialogFlags.DestroyWithParent,
			MessageType.Info, ButtonsType.Ok, "Névjegy");
		msg.Run();
		msg.Destroy();
 
 
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

Lekérdezzük melyik gombra kattintott a felhasználó:

using Gtk;
using System;
 
class Program : Window
{
 
	Button gomb1 = new Button("Nyomj meg");
	Program():base("")
	{		
		gomb1.Clicked += new EventHandler(Gomb1_Clicked);
 
		Add(gomb1);
		DefaultWidth = 300;
		DefaultHeight = 200;
		ShowAll();		
	}
	private void Gomb1_Clicked(object sender, EventArgs e)
	{
 
		MessageDialog msg = new MessageDialog(this, DialogFlags.DestroyWithParent,
			MessageType.Info, ButtonsType.YesNo, "Szeretnéd ezt?");
		ResponseType rsp = (ResponseType)msg.Run();
		msg.Destroy();
 
		if(rsp == ResponseType.Yes)
			Title = "Szeretné";
		else
			Title = "Nem szeretné";
 
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

A ButtonType felsorolt típus tagjai:

using Gtk;
using System;
 
class Program : Window
{
 
	MenuBar menubar = new MenuBar();
 
	Menu file_menu = new Menu();
	MenuItem file_item = new MenuItem("Fájl");
	MenuItem exit_item = new MenuItem("Kilépés");
 
	Menu help_menu = new Menu();
	MenuItem help_item = new MenuItem("Súgó");
	MenuItem content_item = new MenuItem("Tartalom");
	MenuItem about_item = new MenuItem("Névjegy");
 
	Label label1 = new Label("A menüs program");
 
	VBox box = new VBox(false, 2);
 
 
	Program():base("")
	{		
 
		exit_item.Activated += new EventHandler(Exit_item_Activated);
		file_menu.Append(exit_item);
		file_item.Submenu = file_menu;		
 
 
		content_item.Activated += new EventHandler(Content_item_Activated);
		about_item.Activated += new EventHandler(About_item_Activated);
		help_menu.Append(content_item);
		help_menu.Append(about_item);
		help_item.Submenu = help_menu;
 
		menubar.Append(file_item);
		menubar.Append(help_item);
 
		box.PackStart(menubar, false, false, 0);
		box.PackStart(label1, true, true, 0);
 
		Add(box);
		DefaultWidth = 300;
		DefaultHeight = 200;
		ShowAll();		
	}
	private void Content_item_Activated(object sender, EventArgs e)
	{
		MessageDialog msg = new MessageDialog(this, DialogFlags.DestroyWithParent,
			MessageType.Info, ButtonsType.Ok, "Tartalom árnyékeljárás");
		msg.Run();
		msg.Destroy();
	}
	private void About_item_Activated(object sender, EventArgs e)
	{
		MessageDialog msg = new MessageDialog(this, DialogFlags.DestroyWithParent,
			MessageType.Info, ButtonsType.Ok, "Névjegy");
		msg.Run();
		msg.Destroy();
	}
	private void Exit_item_Activated(object sender, EventArgs e)
	{
		Application.Quit();
	}
	static void Main()
	{	
		Application.Init ();		
		new Program();		
		Application.Run();	
	}
}

Linkek