Building programs using GTK or Windows Forms

Compatibility


Linux + .NET

Linux + Mono

macOS + .NET

macOS + Mono

Windows + .NET

Windows + Mono

GTK 2


yes


yes

(yes)

(yes)

GTK 3

yes

yes

yes

no

yes


Windows Forms

no

yes

no

no

yes


Gtk

Installing GtkSharp 3 for .NET

On macOS, you'll first need to install Gtk 3:

  1. Install the Homebrew package manager:

      $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    This step will take several minutes.

  2. Install Gtk:

      $ brew install gtk+3

    This step will take approximately 5 minutes.

Creating a GtkSharp 3 program for .NET

from scratch

First create a console application:

$ dotnet new console

Now add a reference to the GtkSharp package:

$ dotnet add package GtkSharp

Now you can write GTK code in your program. You may want to begin with the source code of one of the GTK programs in the lecture notes, e.g. the "Hello, GTK" program, or the circle-drawing program in the "drawing graphics" section. To build and run your program, type

$ dotnet run

using a template

Alternatively, you can create a GTK program using one of the project templates from the GtkSharp package. To do that, you'll first need to install these templates:

$ dotnet new --install GtkSharp.Template.CSharp

Now you can create a new GTK application like this:

$ dotnet new gtkapp

This application will have some features that you probably don't need, such as a user interface defined using a .glade file. You may want to delete its source files and instead use one of the GTK programs from our lecture notes.

Installing Gtk# 2 for Mono

On Ubuntu, install the libgtk2.0-cil-dev package:

$ sudo apt install libgtk2.0-cil-dev

On macOS you don't need to install anything, since Gtk# 2 is already included in Mono and Visual Studio for Mac.

If you are using Windows, visit the Mono for Windows page, then click "Download Mono 32-bit". Run the downloaded installer.

Building Gtk 2 programs for Mono from the command line

On Linux:

$ csc `pkg-config --libs gtk-sharp-2.0` -r:Mono.Cairo.dll program.cs

On macOS:

$ csc -lib:/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/gtk-sharp-2.0 \
    -r:gdk-sharp.dll,glib-sharp.dll,gtk-sharp.dll,Mono.Cairo.dll \
     program.cs

Building Gtk 2 programs with MonoDevelop (any platform) or Visual Studio for Mac

  1. Create a new project, choosing the template "Gtk# 2.0 Project".

  2. In the Solution sidebar, right click on References and select Edit References…

  3. In the window that pops up, check the box beside Mono.Cairo.

You should now be able to build a Gtk# program in the current project.

Windows Forms

Windows Forms is included in Mono (on Linux) and .NET (on Windows) by default. You don't need to install anything extra to use it.

Creating a project

To create a Windows Forms project in .NET on Windows, type

C:\> dotnet new winforms

Producing debugging output

By default, a Windows Forms project builds a Windows executable that doesn't produce its own terminal window and can't write output to the terminal from which it is launched. And so if you add statements to Console.WriteLine() for debugging, they will produce no output.

To allow your program to produce console output, you'll need to make two changes to your .csproj project file.

1. Change the OutputType from WinExe to Exe:

    <OutputType>Exe</OutputType>

2. Add the following line before the </PropertyGroup> end tag:

    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>