How to: Use GTK# in .NET.
For those interested in portablity between Mono and .NET, the biggest issue you will have is windows and controls. On mono there has been work on GTK# which is a managed wrapper of the GTK+ windowing widgets often used to wite gui apps on Linux. I have spent lots of time trying to get around using GTK# on Windows with .NEWT so that I can use it instead of the normal System.Windows.Forms to make it easier to port my .NET windows developed stuff over to Linux.
Finally I figured out the steps and some example code to create a window, buttons, textbox and adding text to textbox from a button click. Simple stuff really, but enough to get started in understanding using GTK.
Here are the step you need to do first to use them.
1) Install Cygwin - www.cygwin.com
2) Install GTK+ runtimes - http://www.dropline.net/gtk/download.php
3) Install GTK# - http://prdownloads.sourceforge.net/g...p.exe?download
4) Right click My Computer. Properties. Click Advanced tab. Goto Environment variables and change your path variable to include the following
a) path_you_installed_cygwin\bin
b) path_you_installed_gtk+\lib
c) path_you_installed_gtk#
Now for an example
VB Code:
Imports System
Imports Gtk
Imports GtkSharp
Public Module ToggleButtons
Dim tb As Gtk.TextView
Dim buffer As Gtk.TextBuffer
Sub OnDeleteEvent(ByVal obj As Object, ByVal args As DeleteEventArgs)
Application.Quit()
End Sub
Sub OnExitButtonEvent(ByVal obj As Object, ByVal args As EventArgs)
Application.Quit()
End Sub
Sub OnClick(ByVal obj As Object, ByVal args As EventArgs)
' Create a new buffer object
buffer = New Gtk.TextBuffer(System.IntPtr.Zero)
' Set the buffer to point to the textviews buffer
buffer = tb.Buffer
' Set text to display
buffer.SetText("hey there")
End Sub
Public Sub Main()
Application.Init() ' pre initialization
Dim window As Window = New Window("Testing Widgets") ' Declaring a new window
AddHandler window.DeleteEvent, AddressOf OnDeleteEvent
window.BorderWidth = Convert.ToUInt32(0)
Dim box1 As VBox = New VBox(False, 10) ' Creatinga child container for window to hold
' Note that the window object can only hold 1 child. So we use the Boxes to hold our main widegts
window.Add(box1)
box1.Show()
' Create a button with a Clicked event wired up
Dim btnClick As Button = New Button("Click to add text to textview!")
AddHandler btnClick.Clicked, AddressOf OnClick
box1.PackStart(btnClick, True, True, Convert.ToUInt32(0)) ' Add it to the Box container we creates
btnClick.Show() ' make it visible
Dim separator As HSeparator = New HSeparator() ' horizontal seperator
box1.PackStart(separator, False, True, Convert.ToUInt32(0))
separator.Show()
' Create a quit button
Dim btnQuit As Button = New Button("Quit")
AddHandler btnQuit.Clicked, AddressOf OnExitButtonEvent
box1.PackStart(btnQuit, True, True, Convert.ToUInt32(0))
tb = New Gtk.TextView()
tb.SetSizeRequest(100, 100)
box1.PackStart(tb, True, True, Convert.ToUInt32(0))
btnQuit.CanDefault = True ' Make quit button default
btnQuit.GrabDefault()
btnQuit.Show()
window.ShowAll() ' Display the child to window object
Application.Run() ' Run the application
End Sub
End Module
Add reference to the 3 follwoing dll's in the gtk# dir
atk-sharp
glib-sharp
gtk-sharp
Now build and run the built exe. For some reason it doesnt work by just hiting F5 and running it in the IDE. Nopt sure why yet.
If everything was done right, you should have a GTK based window come up.
Now this code SHOULD compile and work using mono bas on Linux as well. I will test this once I get my Linux system back up.
If nothing else, this is something new to play with.
Enjoy