240.md 11.1 KB
Newer Older
W
wizardforcel 已提交
1
# GTK# 中的 Cario 绘图
W
wizardforcel 已提交
2 3 4

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

W
wizardforcel 已提交
5
在 GTK# 编程教程的这一部分中,我们将使用 Cairo 库进行一些绘制。
W
wizardforcel 已提交
6 7 8 9 10 11 12 13 14 15 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

Cairo 是用于创建 2D 矢量图形的库。 我们可以使用它来绘制自己的小部件,图表或各种效果或动画。

## 简单绘图

笔划操作绘制形状的轮廓,填充操作填充形状的内部。 接下来,我们将演示这两个操作。

`simpledrawing.cs`

```
using Gtk;
using Cairo;
using System;

class SharpApp : Window {

    public SharpApp() : base("Simple drawing")
    {
        SetDefaultSize(230, 150);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };;

        DrawingArea darea = new DrawingArea();
        darea.ExposeEvent += OnExpose;

        Add(darea);

        ShowAll();
    }

    void OnExpose(object sender, ExposeEventArgs args)
    {
        DrawingArea area = (DrawingArea) sender;
        Cairo.Context cr =  Gdk.CairoHelper.Create(area.GdkWindow);

        cr.LineWidth = 9;
        cr.SetSourceRGB(0.7, 0.2, 0.0);

        int width, height;
        width = Allocation.Width;
        height = Allocation.Height;

        cr.Translate(width/2, height/2);
        cr.Arc(0, 0, (width < height ? width : height) / 2 - 10, 0, 2*Math.PI);
        cr.StrokePreserve();

        cr.SetSourceRGB(0.3, 0.4, 0.6);
        cr.Fill();

        ((IDisposable) cr.Target).Dispose();                                      
        ((IDisposable) cr).Dispose();
    }

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

```

在我们的示例中,我们将绘制一个圆并将其用纯色绘制。

```
gmcs -pkg:gtk-sharp-2.0 -r:/usr/lib/mono/2.0/Mono.Cairo.dll  simple.cs

```

这是我们编译示例的方式。

```
DrawingArea darea = new DrawingArea();

```

我们将在`DrawingArea`小部件上进行绘制操作。

```
darea.ExposeEvent += OnExpose;

```

所有绘图都是通过我们插入`ExposeEvent`的方法完成的。

```
DrawingArea area = (DrawingArea) sender;
Cairo.Context cr =  Gdk.CairoHelper.Create(area.GdkWindow);

```

W
wizardforcel 已提交
98
我们从绘图区域的`GdkWindow`创建`Cairo.Context`对象。 上下文是用于在所有`Drawable`对象上绘制的对象。
W
wizardforcel 已提交
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

```
cr.LineWidth = 9;

```

我们将线条的宽度设置为 9 像素。

```
cr.SetSourceRGB(0.7, 0.2, 0.0);

```

我们将颜色设置为深红色。

```
int width, height;
width = Allocation.Width;
height = Allocation.Height;

cr.Translate(width/2, height/2);

```

我们得到绘图区域的宽度和高度。 我们将原点移动到窗口的中间。

```
cr.Arc(0, 0, (width < height ? width : height) / 2 - 10, 0, 2*Math.PI);
cr.StrokePreserve();

```

我们绘制一个圆形的外部形状。 `StrokePreserve()`根据当前的线宽,线连接,线帽和破折号设置描边当前路径。 与`Stroke()`不同,它在 cairo 上下文中保留路径。

```
cr.SetSourceRGB(0.3, 0.4, 0.6);
cr.Fill();

```

这会用一些蓝色填充圆圈的内部。

![Simple drawing](img/1c95af28ada4b0feb2050ad68ebd5fe7.jpg)

Figure: Simple drawing

## 基本形状

下一个示例将一些基本形状绘制到窗口上。

`basicshapes.cs`

```
using Gtk;
using Cairo;
using System;

class SharpApp : Window {

    public SharpApp() : base("Basic shapes")
    {
        SetDefaultSize(390, 240);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };

        DrawingArea darea = new DrawingArea();
        darea.ExposeEvent += OnExpose;

        Add(darea);
        ShowAll();
    }

    void OnExpose(object sender, ExposeEventArgs args)
    {
        DrawingArea area = (DrawingArea) sender;
        Cairo.Context cc =  Gdk.CairoHelper.Create(area.GdkWindow);

        cc.SetSourceRGB(0.2, 0.23, 0.9);
        cc.LineWidth = 1;

        cc.Rectangle(20, 20, 120, 80);
        cc.Rectangle(180, 20, 80, 80);
        cc.StrokePreserve();
        cc.SetSourceRGB(1, 1, 1);
        cc.Fill();

        cc.SetSourceRGB(0.2, 0.23, 0.9);
        cc.Arc(330, 60, 40, 0, 2*Math.PI);
        cc.StrokePreserve();
        cc.SetSourceRGB(1, 1, 1);
        cc.Fill();

        cc.SetSourceRGB(0.2, 0.23, 0.9);
        cc.Arc(90, 160, 40, Math.PI/4, Math.PI);
        cc.ClosePath();
        cc.StrokePreserve();
        cc.SetSourceRGB(1, 1, 1);
        cc.Fill();

        cc.SetSourceRGB(0.2, 0.23, 0.9);
        cc.Translate(220, 180);
        cc.Scale(1, 0.7);        
        cc.Arc(0, 0, 50, 0, 2*Math.PI);
        cc.StrokePreserve();
        cc.SetSourceRGB(1, 1, 1);
        cc.Fill();          

        ((IDisposable) cc.Target).Dispose ();                                      
        ((IDisposable) cc).Dispose ();
    }

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

```

在此示例中,我们将创建一个矩形,一个正方形,一个圆形,一个弧形和一个椭圆形。 我们用蓝色绘制轮廓,内部用白色绘制。

```
cc.Rectangle(20, 20, 120, 80);
cc.Rectangle(180, 20, 80, 80);
cc.StrokePreserve();
cc.SetSourceRGB(1, 1, 1);
cc.Fill();

```

这些线绘制一个矩形和一个正方形。

```
cc.Arc(330, 60, 40, 0, 2*Math.PI);

```

此处`Arc()`方法绘制一个完整的圆。

```
cc.Scale(1, 0.7);        
cc.Arc(0, 0, 50, 0, 2*Math.PI);

```

如果要绘制椭圆形,请先进行一些缩放。 在这里`Scale()`方法缩小 y 轴。

![Basic shapes](img/13e08cfe286f9b228a59d3094a1dafb8.jpg)

Figure: Basic shapes

## 色彩

W
wizardforcel 已提交
254
颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。 Cario 有效 RGB 值在 0 到 1 的范围内。
W
wizardforcel 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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 313 314 315 316

`colors.cs`

```
using Gtk;
using Cairo;
using System;

class SharpApp : Window {

    public SharpApp() : base("Colors")
    {
        SetDefaultSize(360, 100);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };

        DrawingArea darea = new DrawingArea();
        darea.ExposeEvent += OnExpose;

        Add(darea);

        ShowAll();
    }

    void OnExpose(object sender, ExposeEventArgs args)
    {
        DrawingArea area = (DrawingArea) sender;
        Cairo.Context cr =  Gdk.CairoHelper.Create(area.GdkWindow);

        cr.SetSourceRGB(0.2, 0.23, 0.9);
        cr.Rectangle(10, 15, 90, 60);
        cr.Fill();

        cr.SetSourceRGB(0.9, 0.1, 0.1);
        cr.Rectangle(130, 15, 90, 60);
        cr.Fill();

        cr.SetSourceRGB(0.4, 0.9, 0.4);
        cr.Rectangle(250, 15, 90, 60);
        cr.Fill();

        ((IDisposable) cr.Target).Dispose();                                      
        ((IDisposable) cr).Dispose();
    }

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

```

我们用三种不同的颜色绘制三个矩形。

```
cr.SetSourceRGB(0.2, 0.23, 0.9);

```

W
wizardforcel 已提交
317
`SetSourceRGB()`方法为 Cario 上下文设置颜色。 该方法的三个参数是颜色强度值。
W
wizardforcel 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

```
cr.Rectangle(10, 15, 90, 60);
cr.Fill();

```

我们创建一个矩形形状,并用先前指定的颜色填充它。

![Colors](img/1ebc3ac6b8e30933124b8046199dd531.jpg)

Figure: Colours

## 透明矩形

透明性是指能够透视材料的质量。 了解透明度的最简单方法是想象一块玻璃或水。 从技术上讲,光线可以穿过玻璃,这样我们就可以看到玻璃后面的物体。

在计算机图形学中,我们可以使用 alpha 合成来实现透明效果。 Alpha 合成是将图像与背景组合以创建部分透明外观的过程。 合成过程使用 Alpha 通道。 (wikipedia.org,answers.com)

`transparentrectangles.cs`

```
using Gtk;
using Cairo;
using System;

class SharpApp : Window {

    public SharpApp() : base("Transparent rectangles")
    {
        SetDefaultSize(590, 90);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); } ;

        DrawingArea darea = new DrawingArea();
        darea.ExposeEvent += OnExpose;

        Add(darea);

        ShowAll();
    }

    void OnExpose(object sender, ExposeEventArgs args)
    {
        DrawingArea area = (DrawingArea) sender;
        Cairo.Context cr =  Gdk.CairoHelper.Create(area.GdkWindow);

        for ( int i = 1; i <= 10; i++) {
            cr.SetSourceRGBA(0, 0, 1, i*0.1);
            cr.Rectangle(50*i, 20, 40, 40);
            cr.Fill();  
        }

        ((IDisposable) cr.Target).Dispose();                                      
        ((IDisposable) cr).Dispose();
    }

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

```

在示例中,我们将绘制十个具有不同透明度级别的矩形。

```
cr.SetSourceRGBA(0, 0, 1, i*0.1);

```

`SetSourceRGBA()`方法的最后一个参数是 alpha 透明度。

![Transparent rectangles](img/8a42b9ff5ee65e04d0269f9f41c31f99.jpg)

Figure: Transparent rectangles

## 灵魂伴侣

在下一个示例中,我们在窗口上绘制一些文本。

`soulmate.cs`

```
using Gtk;
using Cairo;
using System;

class SharpApp : Window {

    public SharpApp() : base("Soulmate")
    {
        SetDefaultSize(420, 250);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };

        DrawingArea darea = new DrawingArea();
        darea.ExposeEvent += OnExpose;

        Add(darea);

        ShowAll();
    }

    void OnExpose(object sender, ExposeEventArgs args)
    {
        DrawingArea area = (DrawingArea) sender;
        Cairo.Context cr =  Gdk.CairoHelper.Create(area.GdkWindow);

        cr.SetSourceRGB(0.1, 0.1, 0.1);

        cr.SelectFontFace("Purisa", FontSlant.Normal, FontWeight.Bold);
        cr.SetFontSize(13);

        cr.MoveTo(20, 30);
        cr.ShowText("Most relationships seem so transitory");
        cr.MoveTo(20, 60);
        cr.ShowText("They're all good but not the permanent one");
        cr.MoveTo(20, 120);
        cr.ShowText("Who doesn't long for someone to hold");
        cr.MoveTo(20, 150);
        cr.ShowText("Who knows how to love without being told");
        cr.MoveTo(20, 180);
        cr.ShowText("Somebody tell me why I'm on my own");
        cr.MoveTo(20, 210);
        cr.ShowText("If there's a soulmate for everyone");

        ((IDisposable) cr.Target).Dispose();                                      
        ((IDisposable) cr).Dispose();
    }

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

```

我们显示 Natasha Bedingfields Soulmate 歌曲的部分歌词。

```
cr.SelectFontFace("Purisa", FontSlant.Normal, FontWeight.Bold);

```

在这里,我们指定使用的字体-粗体的 Purisa。

```
cr.SetFontSize(13);

```

我们指定字体的大小。

```
cr.MoveTo(20, 30);

```

我们移动到要绘制文本的位置。

```
cr.ShowText("Most relationships seem so transitory");

```

`ShowText()`方法将文本绘制到窗口上。

![Soulmate](img/fe0ef3dbd86a5a669009bfe1092797bf.jpg)

Figure: Soulmate

W
wizardforcel 已提交
496
在 GTK# 编程库的这一章中,我们使用 Cario 库进行绘制。