The Gnome Canvas

The CanvasText Item

The CanvasText is used to display text on the canvas. You already have seen the CanvasText in "Hello Canvas!". First, let's start, by creating a new CanvasText.
CanvasText text1 = new CanvasText(root);
The first argument "root" is the Canvas root as created in the first example. Now as the CanvasText has been created we can use it. The first step is to define the coordinates, where the text should be placed. This can be done easily by using the X / Y properties.
text1.X = int x_coordinate;
text1.Y = int y_coordinate;
It is important to know, that those coordinates do not describe the top-left corner of the text, but the center.

It is just as important to define a color for your text. If you don't do this, the text will not display on the screen. For consistency with other items the Color is called FillColor.

text1.FillColor = string color;
The color can be either a name like "red", "green", "black" or a HTML-like color code.

Often used examples for color-codes are:

#000000 - black
#ffffff - white
        - red
Last, but not least the most important property is of course the text itself:
text1.Text = string text;

After that the text must be shown using Show().

That's it.

using System;
using Gtk;
using GtkSharp;
using Gnome;

class CanvasTest {

        public CanvasTest() {


                Application.Init();

                Window window1 = new Window("Hello Canvas!");
                window1.DeleteEvent += new DeleteEventHandler (delete_event);

                Canvas canvas1 = Canvas.NewAa();

                int Width = 100;
                int Height = 100;

                canvas1.SetScrollRegion(0, 0, Width, Height);
                canvas1.WidthRequest = Width;
                canvas1.HeightRequest = Height;
                CanvasGroup root = canvas1.Root();

                // Draw Background

                CanvasRect background = new CanvasRect(root);
                background.X1 = 0;
                background.X2 = Width;
                background.Y1 = 0;
                background.Y2 = Height;
                background.FillColor = "#ffffff";
                background.Show();

                // Here we go

                CanvasText hello = new CanvasText(root);
                hello.X = 40;
                hello.Y = 10;
                hello.FillColor = "#000000";
                hello.Text = "Hello, Canvas!";
                hello.Show();

                canvas1.Show();

                window1.Add(canvas1);
                window1.ShowAll();
                Application.Run();

        }

        public static void Main()
        {
                new CanvasTest();
        }

        void delete_event (object obj, DeleteEventArgs args)
        {
                Application.Quit ();
        }

}

Credits

Author: Johannes Roith (johannes at jroith.de)