原文: http://zetcode.com/gui/vbgtk/painting/

在 Visual Basic GTK# 教程的这一部分中,我们将使用 Cairo 库进行一些绘图。

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

色彩

在第一个示例中,我们将使用颜色。 颜色是代表红色,绿色和蓝色(RGB)强度值的组合的对象。 Cario 有效 RGB 值在 0 到 1 的范围内。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program draws three rectangles.
  4. ' The interiors are filled with
  5. ' different colors.
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports Gtk
  11. Public Class GtkVBApp
  12. Inherits Window
  13. Public Sub New
  14. MyBase.New("Colors")
  15. Me.InitUI
  16. Me.SetDefaultSize(360, 100)
  17. Me.SetPosition(WindowPosition.Center)
  18. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  19. Me.ShowAll
  20. End Sub
  21. Private Sub InitUI
  22. Dim darea As New DrawingArea
  23. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose
  24. Me.Add(darea)
  25. End Sub
  26. Private Sub OnExpose(ByVal sender As Object, ByVal args As ExposeEventArgs)
  27. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  28. Me.DrawColors(cc)
  29. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  30. disposeTarget.Dispose
  31. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  32. disposeContext.Dispose
  33. End Sub
  34. Private Sub DrawColors(ByVal cc As Cairo.Context)
  35. cc.SetSourceRGB(0.2, 0.23, 0.9)
  36. cc.Rectangle(10, 15, 90, 60)
  37. cc.Fill
  38. cc.SetSourceRGB(0.9, 0.1, 0.1)
  39. cc.Rectangle(130, 15, 90, 60)
  40. cc.Fill
  41. cc.SetSourceRGB(0.4, 0.9, 0.4)
  42. cc.Rectangle(250, 15, 90, 60)
  43. cc.Fill
  44. End Sub
  45. Private Sub OnDelete(ByVal sender As Object, _
  46. ByVal args As DeleteEventArgs)
  47. Application.Quit
  48. End Sub
  49. Public Shared Sub Main
  50. Application.Init
  51. Dim app As New GtkVBApp
  52. Application.Run
  53. End Sub
  54. End Class

在我们的示例中,我们将绘制三个矩形,并用三种不同的颜色填充它们。

  1. vbnc -r:/usr/lib/mono/gtk-sharp-2.0/gtk-sharp.dll
  2. -r:/usr/lib/mono/gtk-sharp-2.0/gdk-sharp.dll
  3. -r:/usr/lib/mono/2.0/Mono.Cairo.dll colors.vb

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

  1. Dim darea As New DrawingArea

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

  1. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose

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

  1. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)

我们从绘图区域的GdkWindow创建Cairo.Context对象。 上下文是我们绘制所有图纸的对象。

  1. Me.DrawColors(cc)

实际图形委托给DrawColors方法。

  1. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  2. disposeTarget.Dispose
  3. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  4. disposeContext.Dispose

在这里,我们处理在绘制过程中使用的资源。

  1. cc.SetSourceRGB(0.2, 0.23, 0.9)

SetSourceRGB方法为 Cario 上下文设置颜色。 该方法的三个参数是颜色强度值。

  1. cc.Rectangle(10, 15, 90, 60)

我们画一个矩形。 前两个参数是矩形左上角的 x,y 坐标。 最后两个参数是矩形的宽度和高度。

  1. cc.Fill

我们用当前颜色填充矩形的内部。

Cario 绘图 - 图1

图:颜色

基本形状

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

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program draws basic shapes
  4. ' available in Cairo
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("Basic Shapes")
  14. Me.InitUI
  15. Me.SetDefaultSize(400, 250)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Dim darea As New DrawingArea
  22. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose
  23. Me.Add(darea)
  24. End Sub
  25. Private Sub OnExpose(ByVal sender As Object, ByVal args As ExposeEventArgs)
  26. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  27. Me.DrawShapes(cc)
  28. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  29. disposeTarget.Dispose
  30. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  31. disposeContext.Dispose
  32. End Sub
  33. Private Sub DrawShapes(ByVal cc As Cairo.Context)
  34. cc.SetSourceRGB(0.5, 0.5, 0.5)
  35. cc.Rectangle(20, 20, 120, 80)
  36. cc.Rectangle(180, 20, 80, 80)
  37. cc.Fill
  38. cc.Arc(330, 60, 40, 0, 2*Math.PI)
  39. cc.Fill
  40. cc.Arc(90, 160, 40, Math.PI/4, Math.PI)
  41. cc.ClosePath
  42. cc.Fill
  43. cc.Translate(220, 180)
  44. cc.Scale(1, 0.7)
  45. cc.Arc(0, 0, 50, 0, 2*Math.PI)
  46. cc.Fill
  47. End Sub
  48. Private Sub OnDelete(ByVal sender As Object, _
  49. ByVal args As DeleteEventArgs)
  50. Application.Quit
  51. End Sub
  52. Public Shared Sub Main
  53. Application.Init
  54. Dim app As New GtkVBApp
  55. Application.Run
  56. End Sub
  57. End Class

在此示例中,我们将创建一个矩形,正方形,圆形,弧形和椭圆形。

  1. cc.Rectangle(20, 20, 120, 80)
  2. cc.Rectangle(180, 20, 80, 80)
  3. cc.Fill

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

  1. cc.Arc(330, 60, 40, 0, 2*Math.PI)
  2. cc.Fill

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

  1. cc.Translate(220, 180)
  2. cc.Scale(1, 0.7)
  3. cc.Arc(0, 0, 50, 0, 2*Math.PI)
  4. cc.Fill

Translate方法将对象移动到特定点。 如果要绘制椭圆形,请先进行一些缩放。 在这里Scale方法缩小 y 轴。

Cario 绘图 - 图2

图:基本形状

透明矩形

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

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

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program draws ten
  4. ' rectangles with different
  5. ' levels of transparency
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports Gtk
  11. Public Class GtkVBApp
  12. Inherits Window
  13. Public Sub New
  14. MyBase.New("Transparent rectangles")
  15. Me.InitUI
  16. Me.SetDefaultSize(590, 90)
  17. Me.SetPosition(WindowPosition.Center)
  18. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  19. Me.ShowAll
  20. End Sub
  21. Private Sub InitUI
  22. Dim darea As New DrawingArea
  23. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose
  24. Me.Add(darea)
  25. End Sub
  26. Private Sub OnExpose(ByVal sender As Object, ByVal args As ExposeEventArgs)
  27. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  28. Me.DrawRectangles(cc)
  29. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  30. disposeTarget.Dispose()
  31. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  32. disposeContext.Dispose()
  33. End Sub
  34. Private Sub DrawRectangles(ByVal cc As Cairo.Context)
  35. For i As Integer = 1 To 10
  36. cc.SetSourceRGBA(0, 0, 1, i*0.1)
  37. cc.Rectangle(50*i, 20, 40, 40)
  38. cc.Fill
  39. Next
  40. End Sub
  41. Private Sub OnDelete(ByVal sender As Object, _
  42. ByVal args As DeleteEventArgs)
  43. Application.Quit
  44. End Sub
  45. Public Shared Sub Main
  46. Application.Init
  47. Dim app As New GtkVBApp
  48. Application.Run
  49. End Sub
  50. End Class

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

  1. cc.SetSourceRGBA(0, 0, 1, i*0.1)

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

Cario 绘图 - 图3

图:透明矩形

甜甜圈

在下面的示例中,我们通过旋转一堆椭圆来创建 n 个复杂形状。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program draws basic shapes
  4. ' available in Cairo
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("Donut")
  14. Me.InitUI
  15. Me.SetDefaultSize(400, 250)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Dim darea As New DrawingArea
  22. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose
  23. Me.Add(darea)
  24. End Sub
  25. Private Sub OnExpose(ByVal sender As Object, ByVal args As ExposeEventArgs)
  26. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  27. Me.DrawDonut(cc)
  28. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  29. disposeTarget.Dispose
  30. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  31. disposeContext.Dispose
  32. End Sub
  33. Private Sub DrawDonut(ByVal cc As Cairo.Context)
  34. cc.LineWidth = 0.5
  35. Dim width, height As Integer
  36. width = Allocation.Width
  37. height = Allocation.Height
  38. cc.Translate(width/2, height/2)
  39. cc.Arc(0, 0, 120, 0, 2*Math.PI)
  40. cc.Stroke
  41. cc.Save
  42. For i As Integer = 0 To 35
  43. cc.Rotate( i*Math.PI/36)
  44. cc.Scale(0.3, 1)
  45. cc.Arc(0, 0, 120, 0, 2*Math.PI)
  46. cc.Restore
  47. cc.Stroke
  48. cc.Save
  49. Next
  50. End Sub
  51. Private Sub OnDelete(ByVal sender As Object, _
  52. ByVal args As DeleteEventArgs)
  53. Application.Quit
  54. End Sub
  55. Public Shared Sub Main
  56. Application.Init
  57. Dim app As New GtkVBApp
  58. Application.Run
  59. End Sub
  60. End Class

在此示例中,我们创建一个甜甜圈。 形状类似于曲奇,因此得名“甜甜圈”。

  1. cc.Translate(width/2, height/2)
  2. cc.Arc(0, 0, 120, 0, 2*Math.PI)
  3. cc.Stroke

刚开始时有一个椭圆。

  1. For i As Integer = 0 To 35
  2. cc.Rotate( i*Math.PI/36)
  3. cc.Scale(0.3, 1)
  4. cc.Arc(0, 0, 120, 0, 2*Math.PI)
  5. cc.Restore
  6. cc.Stroke
  7. cc.Save
  8. Next

旋转几圈后,有一个甜甜圈。

Cario 绘图 - 图4

图:多纳圈

绘制文字

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

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This program draws text
  4. ' on the window
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("Soulmate")
  14. Me.InitUI
  15. Me.SetDefaultSize(400, 250)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Dim darea As New DrawingArea
  22. AddHandler darea.ExposeEvent, AddressOf Me.OnExpose
  23. Me.Add(darea)
  24. End Sub
  25. Private Sub OnExpose(ByVal sender As Object, ByVal args As ExposeEventArgs)
  26. Dim cc As Cairo.Context = Gdk.CairoHelper.Create(sender.GdkWindow)
  27. Me.DrawLyrics(cc)
  28. Dim disposeTarget As IDisposable = CType(cc.Target, IDisposable)
  29. disposeTarget.Dispose
  30. Dim disposeContext As IDisposable = CType(cc, IDisposable)
  31. disposeContext.Dispose
  32. End Sub
  33. Private Sub DrawLyrics(ByVal cc As Cairo.Context)
  34. cc.SetSourceRGB(0.1, 0.1, 0.1)
  35. cc.SelectFontFace("Purisa", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold)
  36. cc.SetFontSize(13)
  37. cc.MoveTo(20, 30)
  38. cc.ShowText("Most relationships seem so transitory")
  39. cc.MoveTo(20, 60)
  40. cc.ShowText("They're all good but not the permanent one")
  41. cc.MoveTo(20, 120)
  42. cc.ShowText("Who doesn't long for someone to hold")
  43. cc.MoveTo(20, 150)
  44. cc.ShowText("Who knows how to love without being told")
  45. cc.MoveTo(20, 180)
  46. cc.ShowText("Somebody tell me why I'm on my own")
  47. cc.MoveTo(20, 210)
  48. cc.ShowText("If there's a soulmate for everyone")
  49. End Sub
  50. Private Sub OnDelete(ByVal sender As Object, _
  51. ByVal args As DeleteEventArgs)
  52. Application.Quit
  53. End Sub
  54. Public Shared Sub Main
  55. Application.Init
  56. Dim app As New GtkVBApp
  57. Application.Run
  58. End Sub
  59. End Class

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

  1. cc.SelectFontFace("Purisa", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold)

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

  1. cc.SetFontSize(13)

我们指定字体的大小。

  1. cc.MoveTo(20, 30)

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

  1. cc.ShowText("Most relationships seem so transitory")

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

Cario 绘图 - 图5

图:灵魂伴侣

在 Visual Basic GTK# 教程的这一章中,我们使用 Cario 库进行绘图。