Results 1 to 6 of 6

Thread: How to: Use GTK# in .NET.

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    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:
    1. Imports System
    2. Imports Gtk
    3. Imports GtkSharp
    4.  
    5. Public Module ToggleButtons
    6.     Dim tb As Gtk.TextView
    7.     Dim buffer As Gtk.TextBuffer
    8.  
    9.     Sub OnDeleteEvent(ByVal obj As Object, ByVal args As DeleteEventArgs)
    10.  
    11.         Application.Quit()
    12.  
    13.     End Sub
    14.  
    15.     Sub OnExitButtonEvent(ByVal obj As Object, ByVal args As EventArgs)
    16.  
    17.         Application.Quit()
    18.  
    19.     End Sub
    20.  
    21.     Sub OnClick(ByVal obj As Object, ByVal args As EventArgs)
    22.         ' Create a new buffer object
    23.         buffer = New Gtk.TextBuffer(System.IntPtr.Zero)
    24.         ' Set the buffer to point to the textviews buffer
    25.         buffer = tb.Buffer
    26.         ' Set text to display
    27.         buffer.SetText("hey there")
    28.     End Sub
    29.  
    30.     Public Sub Main()
    31.  
    32.         Application.Init() ' pre initialization
    33.  
    34.         Dim window As Window = New Window("Testing Widgets") ' Declaring a new window
    35.         AddHandler window.DeleteEvent, AddressOf OnDeleteEvent
    36.         window.BorderWidth = Convert.ToUInt32(0)
    37.  
    38.         Dim box1 As VBox = New VBox(False, 10) ' Creatinga child container for window to hold
    39.         ' Note that the window object can only hold 1 child. So we use the Boxes to hold our main widegts
    40.         window.Add(box1)
    41.         box1.Show()
    42.  
    43.         ' Create a button with a Clicked event wired up
    44.         Dim btnClick As Button = New Button("Click to add text to textview!")
    45.         AddHandler btnClick.Clicked, AddressOf OnClick
    46.         box1.PackStart(btnClick, True, True, Convert.ToUInt32(0)) ' Add it to the Box container we creates
    47.         btnClick.Show() ' make it visible
    48.  
    49.         Dim separator As HSeparator = New HSeparator() ' horizontal seperator
    50.         box1.PackStart(separator, False, True, Convert.ToUInt32(0))
    51.         separator.Show()
    52.  
    53.         ' Create a quit button
    54.         Dim btnQuit As Button = New Button("Quit")
    55.         AddHandler btnQuit.Clicked, AddressOf OnExitButtonEvent
    56.         box1.PackStart(btnQuit, True, True, Convert.ToUInt32(0))
    57.  
    58.         tb = New Gtk.TextView()
    59.         tb.SetSizeRequest(100, 100)
    60.         box1.PackStart(tb, True, True, Convert.ToUInt32(0))
    61.         btnQuit.CanDefault = True ' Make quit button default
    62.         btnQuit.GrabDefault()
    63.         btnQuit.Show()
    64.  
    65.         window.ShowAll() ' Display the child to window object
    66.         Application.Run() ' Run the application
    67.     End Sub
    68.  
    69. 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
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    It actually does run in the IDE. I just had to close VS.NET as the path changes didnt get reloaded by the ide.

    Still get some kind of exception on close of the app. Dont know why yet.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    UPDATE:

    Yes the same exact code compiled under Linux. Here is the screenshot.


    So yes protable code between Windows and Linux is possible under .NET/.Mono
    Attached Images Attached Images  
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Good stuff. Will this run on Redhat? I'll try this in VMware.

  5. #5

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Shouldnt have any problems under REd Hat. It was running it under Gentoo using VMware as well.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Ok cool.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width