GTK# 学习笔记

Choosing Gtk# or Glade#

Gtk# looks like Windows Forms and Controls, and Glade# is more like WPF, it can define the widgets in a xml file,  and load it into the application.

The Gtk# "Hello world"  application.

  using  System;
 
using  Gtk;
 
 
public   class  GtkHelloWorld {
  
   
public   static   void  Main() {
     Application.Init();
 
     
// Create the Window
     Window myWin  =   new  Window( " My first GTK# Application!  " );
     myWin.Resize(
200 , 200 );
     
     
// Create a label and put some text in it.     
     Label myLabel  =   new  Label();
     myLabel.Text 
=   " Hello World!!!! " ;
          
     
// Add the label to the form     
     myWin.Add(myLabel);
     
     
// Show Everything     
     myWin.ShowAll();
     
     Application.Run();   
   }
 }

 compile it with command:

mcs -pkg :gtk -sharp- 2.0  helloword . cs

 

 To use a glade file:

//  file: glade.cs
using  System;
using  Gtk;
using  Glade;
public   class  GladeApp
{
        
public   static   void  Main ( string [] args)
        {
                
new  GladeApp (args);
        }
 
        
public  GladeApp ( string [] args) 
        {
                Application.Init();
 
                Glade.XML gxml 
=   new  Glade.XML ( null " gui.glade " " window1 " null );
                gxml.Autoconnect (
this );
                Application.Run();
        }
}

 compile it with command:

mcs -pkg :glade -sharp- 2.0  -resource :gui . glade glade . cs

 use -resource:gui.glade option to make it as embedded resource. Or just leave it in the file system,

and use the following code to load it.

Glade.XML gxml  =   new  Glade.XML ( " ./gui.glade " " window1 " null );

 add private member, then they can be used in the program, and can add event handler to catch widget events. 

[Widget]      
Button button1;
       
[Widget]
Label label1;

button1.Clicked 
+=  OnPressButtonEvent;
              
public   void  OnPressButtonEvent(  object   o, EventArgs e)
{
   Console.WriteLine(
" Button press " );
   label1.Text 
= " Mono "
}

 or add the widget events in the glade designer, and add the handler code with the same method name:

void  on_button1_clicked( object  o, EventArgs e)
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值