235.md 6.6 KB
Newer Older
W
wizardforcel 已提交
1
# GTK# 中的小部件 
W
wizardforcel 已提交
2 3 4

> 原文: [http://zetcode.com/gui/gtksharp/widgets/](http://zetcode.com/gui/gtksharp/widgets/)

W
wizardforcel 已提交
5
在 GTK# 编程教程的这一部分中,我们将介绍一些 GTK# 小部件。
W
wizardforcel 已提交
6

W
wizardforcel 已提交
7
小部件是 GUI 应用的基本构建块。 多年来,几个小部件已成为所有 OS 平台上所有工具包中的标准。 例如,按钮,复选框或滚动条。 GTK# 工具箱的理念是将小部件的数量保持在最低水平。 将创建更多专门的小部件作为自定义 GTK# 小部件。
W
wizardforcel 已提交
8

W
wizardforcel 已提交
9
## `Label`
W
wizardforcel 已提交
10 11 12 13 14

`Label`小部件显示文本。

`label.cs`

W
wizardforcel 已提交
15
```cs
W
wizardforcel 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
using Gtk;

class SharpApp : Window {

   string text = @"Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
and sniff me out like I was Tanqueray

cause you're my fella, my guy
hand me your stella and fly
by the time I'm out the door
you tear men down like Roger Moore

I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good";

    public SharpApp() : base("You know I'm No Good")
    {
        BorderWidth = 8;
        SetPosition(WindowPosition.Center);

        DeleteEvent += delegate { Application.Quit(); };

        Label lyrics = new Label(text);
        Add(lyrics);

        ShowAll();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

```

该代码示例在窗口上显示了一些歌词。

W
wizardforcel 已提交
60
```cs
W
wizardforcel 已提交
61 62 63 64 65 66
    string text = @"Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
...

```

W
wizardforcel 已提交
67
在 C# 编程语言中,多行字符串以`@`字符开头。
W
wizardforcel 已提交
68

W
wizardforcel 已提交
69
```cs
W
wizardforcel 已提交
70 71 72 73 74 75
BorderWidth = 8;

```

`Label`周围有一些空白。

W
wizardforcel 已提交
76
```cs
W
wizardforcel 已提交
77 78 79 80 81 82 83 84 85
Label lyrics = new Label(text);
Add(lyrics);

```

`Label`小部件已创建并添加到窗口。

![Label Widget](img/498fc47dbb9826541f52171075796116.jpg)

W
wizardforcel 已提交
86
图:`Label`小部件
W
wizardforcel 已提交
87

W
wizardforcel 已提交
88
## `CheckButton`
W
wizardforcel 已提交
89 90 91 92 93

`CheckButton`是具有两种状态的窗口小部件:打开和关闭。 开状态通过复选标记显示。 它用来表示一些布尔属性。

`checkbutton.cs`

W
wizardforcel 已提交
94
```cs
W
wizardforcel 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
using Gtk;
using System;

class SharpApp : Window {

    public SharpApp() : base("CheckButton")
    {
        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);

        DeleteEvent += delegate { Application.Quit(); };

        CheckButton cb = new CheckButton("Show title");
        cb.Active = true;
        cb.Toggled += OnToggle;

        Fixed fix = new Fixed();
        fix.Put(cb, 50, 50);

        Add(fix);
        ShowAll();
    }

    void OnToggle(object sender, EventArgs args) 
    {
        CheckButton cb = (CheckButton) sender;

        if (cb.Active) {
            Title = "CheckButton";
        } else {
            Title = " ";
        }
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

```

根据`CheckButton`的状态,我们将在窗口的标题栏中显示标题。

W
wizardforcel 已提交
141
```cs
W
wizardforcel 已提交
142 143 144 145 146 147
CheckButton cb = new CheckButton("Show title");

```

`CheckButton`小部件已创建。

W
wizardforcel 已提交
148
```cs
W
wizardforcel 已提交
149 150 151 152 153 154
cb.Active = true;

```

默认情况下标题是可见的,因此我们默认情况下选中复选按钮。

W
wizardforcel 已提交
155
```cs
W
wizardforcel 已提交
156 157 158 159 160 161
CheckButton cb = (CheckButton) sender;

```

在这里,我们将发送方对象转换为`CheckButton`类。

W
wizardforcel 已提交
162
```cs
W
wizardforcel 已提交
163 164 165 166 167 168 169 170 171 172 173 174
if (cb.Active) {
    Title = "CheckButton";
} else {
    Title = " ";
}

```

根据`CheckButton``Active`属性,我们显示或隐藏窗口的标题。

![CheckButton](img/aa620806576bdbdddb83f48e00d16bbe.jpg)

W
wizardforcel 已提交
175
图:`CheckButton`
W
wizardforcel 已提交
176

W
wizardforcel 已提交
177
## `ComboBox`
W
wizardforcel 已提交
178 179 180 181 182

`ComboBox`是一个小部件,允许用户从选项列表中进行选择。

`combobox.cs`

W
wizardforcel 已提交
183
```cs
W
wizardforcel 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
using Gtk;
using System;

class SharpApp : Window {

    Label label;

    public SharpApp() : base("ComboBox")
    {
       string[] distros = new string[] {"Ubuntu",
            "Mandriva",
            "Red Hat",
            "Fedora",
            "Gentoo" };

        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);
        BorderWidth = 7;
        DeleteEvent += delegate { Application.Quit(); };

        Fixed fix = new Fixed();

        ComboBox cb = new ComboBox(distros);
        cb.Changed += OnChanged;
        label = new Label("-");

        fix.Put(cb, 50, 30);
        fix.Put(label, 50, 140);
        Add(fix);

        ShowAll();
    }

    void OnChanged(object sender, EventArgs args)
    {
        ComboBox cb = (ComboBox) sender;
        label.Text = cb.ActiveText;
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

```

该示例显示了一个组合框和一个标签。 组合框具有六个选项的列表。 这些是 Linux 发行版的名称。 标签窗口小部件显示了从组合框中选择的选项。

W
wizardforcel 已提交
235
```cs
W
wizardforcel 已提交
236 237 238 239 240 241 242 243 244 245
string[] distros = new string[] {"Ubuntu",
    "Mandriva",
    "Red Hat",
    "Fedora",
    "Gentoo" };

```

这是一个字符串数组,将显示在`ComboBox`小部件中。

W
wizardforcel 已提交
246
```cs
W
wizardforcel 已提交
247 248 249 250 251 252
ComboBox cb = new ComboBox(distros);

```

`ComboBox`小部件已创建。 构造函数将字符串数组作为参数。

W
wizardforcel 已提交
253
```cs
W
wizardforcel 已提交
254 255 256 257 258 259 260 261 262 263 264 265
void OnChanged(object sender, EventArgs args)
{
    ComboBox cb = (ComboBox) sender;
    label.Text = cb.ActiveText;
}

```

`OnChanged()`方法内部,我们从组合框中获取选定的文本并将其设置为标签。

![ComboBox](img/c2fce922a2ee60e570fa5c6406221b87.jpg)

W
wizardforcel 已提交
266
图:`ComboBox`
W
wizardforcel 已提交
267

W
wizardforcel 已提交
268
## `Image`
W
wizardforcel 已提交
269 270 271 272 273

下一个示例介绍`Image`小部件。 此小部件显示图片。

`image.cs`

W
wizardforcel 已提交
274
```cs
W
wizardforcel 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
using Gtk;
using System;

class SharpApp : Window {

    Gdk.Pixbuf castle;

    public SharpApp() : base("Red Rock")
    {
        BorderWidth = 1;
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };

        try {
            castle = new Gdk.Pixbuf("redrock.png");
        } catch {
            Console.WriteLine("Image not found");
            Environment.Exit(1);
        }

        Image image = new Image(castle);
        Add(image);

        ShowAll();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}

```

我们在窗口中显示红色岩石城堡。

W
wizardforcel 已提交
313
```cs
W
wizardforcel 已提交
314 315 316 317 318 319 320 321 322 323 324
try {
    castle = new Gdk.Pixbuf("redrock.png");
} catch {
    Console.WriteLine("Image not found");
    Environment.Exit(1);
}

```

我们创建`Gdk.Pixbuf`小部件。 我们将构造函数放在`try``catch`关键字之间,以处理可能的错误。

W
wizardforcel 已提交
325
```cs
W
wizardforcel 已提交
326 327 328 329 330 331 332 333 334
Image image = new Image(castle);
Add(image);

```

`Image`小部件已创建并添加到窗口。

![Image](img/28fa6d4c8e5cb3d0ae4cbf332ebe3990.jpg)

W
wizardforcel 已提交
335
图:图像
W
wizardforcel 已提交
336

W
wizardforcel 已提交
337
在本章中,我们展示了 GTK# 编程库的第一组基本小部件。