GTK # Study Notes

Source: Internet
Author: User
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. 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)
{
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.