原文: http://zetcode.com/gui/gtksharp/customwidget/

工具箱通常仅提供最常见的窗口小部件,例如按钮,文本窗口小部件,滑块等。没有工具箱可以提供所有可能的窗口小部件。

客户端程序员可以创建更多专门的小部件。 他们使用工具箱提供的绘图工具来完成此任务。 有两种可能:程序员可以修改或增强现有的小部件,或者可以从头开始创建自定义小部件。

刻录小部件

这是我们从头开始创建的小部件的示例。 可以在各种媒体刻录应用(例如 Nero 刻录 ROM)中找到此小部件。

burning.cs

  1. using Gtk;
  2. using Cairo;
  3. using System;
  4. class Burning : DrawingArea
  5. {
  6. string[] num = new string[] { "75", "150", "225", "300",
  7. "375", "450", "525", "600", "675" };
  8. public Burning() : base()
  9. {
  10. SetSizeRequest(-1, 30);
  11. }
  12. protected override bool OnExposeEvent(Gdk.EventExpose args)
  13. {
  14. Cairo.Context cr = Gdk.CairoHelper.Create(args.Window);
  15. cr.LineWidth = 0.8;
  16. cr.SelectFontFace("Courier 10 Pitch",
  17. FontSlant.Normal, FontWeight.Normal);
  18. cr.SetFontSize(11);
  19. int width = Allocation.Width;
  20. SharpApp parent = (SharpApp) GetAncestor (Gtk.Window.GType);
  21. int cur_width = parent.CurValue;
  22. int step = (int) Math.Round(width / 10.0);
  23. int till = (int) ((width / 750.0) * cur_width);
  24. int full = (int) ((width / 750.0) * 700);
  25. if (cur_width >= 700) {
  26. cr.SetSourceRGB(1.0, 1.0, 0.72);
  27. cr.Rectangle(0, 0, full, 30);
  28. cr.Clip();
  29. cr.Paint();
  30. cr.ResetClip();
  31. cr.SetSourceRGB(1.0, 0.68, 0.68);
  32. cr.Rectangle(full, 0, till-full, 30);
  33. cr.Clip();
  34. cr.Paint();
  35. cr.ResetClip();
  36. } else {
  37. cr.SetSourceRGB(1.0, 1.0, 0.72);
  38. cr.Rectangle(0, 0, till, 30);
  39. cr.Clip();
  40. cr.Paint();
  41. cr.ResetClip();
  42. }
  43. cr.SetSourceRGB(0.35, 0.31, 0.24);
  44. for (int i=1; i<=num.Length; i++) {
  45. cr.MoveTo(i*step, 0);
  46. cr.LineTo(i*step, 5);
  47. cr.Stroke();
  48. TextExtents extents = cr.TextExtents(num[i-1]);
  49. cr.MoveTo(i*step-extents.Width/2, 15);
  50. cr.TextPath(num[i-1]);
  51. cr.Stroke();
  52. }
  53. ((IDisposable) cr.Target).Dispose();
  54. ((IDisposable) cr).Dispose();
  55. return true;
  56. }
  57. }
  58. class SharpApp : Window {
  59. int cur_value = 0;
  60. Burning burning;
  61. public SharpApp() : base("Burning")
  62. {
  63. SetDefaultSize(350, 200);
  64. SetPosition(WindowPosition.Center);
  65. DeleteEvent += delegate { Application.Quit(); };
  66. VBox vbox = new VBox(false, 2);
  67. HScale scale = new HScale(0, 750, 1);
  68. scale.SetSizeRequest(160, 35);
  69. scale.ValueChanged += OnChanged;
  70. Fixed fix = new Fixed();
  71. fix.Put(scale, 50, 50);
  72. vbox.PackStart(fix);
  73. burning = new Burning();
  74. vbox.PackStart(burning, false, false, 0);
  75. Add(vbox);
  76. ShowAll();
  77. }
  78. void OnChanged(object sender, EventArgs args)
  79. {
  80. Scale scale = (Scale) sender;
  81. cur_value = (int) scale.Value;
  82. burning.QueueDraw();
  83. }
  84. public int CurValue {
  85. get { return cur_value; }
  86. }
  87. public static void Main()
  88. {
  89. Application.Init();
  90. new SharpApp();
  91. Application.Run();
  92. }
  93. }

我们在窗口底部放置一个DrawingArea并手动绘制整个窗口小部件。 所有重要的代码都位于Burning类的OnExposeEvent()方法中。 此小部件以图形方式显示了介质的总容量和可供我们使用的可用空间。 该小部件由比例小部件控制。 自定义窗口小部件的最小值为 0,最大值为 750。如果值达到 700,则开始绘制红色。 这通常表示过度燃烧。

  1. string[] num = new string[] { "75", "150", "225", "300",
  2. "375", "450", "525", "600", "675" };

这些数字显示在刻录小部件上。 它们显示了介质的容量。

  1. SharpApp parent = (SharpApp) GetAncestor (Gtk.Window.GType);
  2. int cur_width = parent.CurValue;

这两行从刻度小部件获取当前数字。 我们获得父窗口小部件,并从父窗口小部件中获得当前值。

  1. int till = (int) ((width / 750.0) * cur_width);
  2. int full = (int) ((width / 750.0) * 700);

till参数确定要绘制的总大小。 该值来自滑块小部件。 它占整个面积的一部分。 full参数确定我们开始用红色绘制的点。

  1. cr.SetSourceRGB(1.0, 1.0, 0.72);
  2. cr.Rectangle(0, 0, full, 30);
  3. cr.Clip();
  4. cr.Paint();
  5. cr.ResetClip();

此代码在此处绘制了一个黄色矩形,直到介质充满为止。

  1. TextExtents extents = cr.TextExtents(num[i-1]);
  2. cr.MoveTo(i*step-extents.Width/2, 15);
  3. cr.TextPath(num[i-1]);
  4. cr.Stroke();

这里的代码在刻录小部件上绘制数字。 我们计算TextExtents来正确定位文本。

  1. void OnChanged(object sender, EventArgs args)
  2. {
  3. Scale scale = (Scale) sender;
  4. cur_value = (int) scale.Value;
  5. burning.QueueDraw();
  6. }

我们从小部件中获取值,并将其存储在cur_value变量中以备后用。 我们重新绘制刻录的小部件。

GTK# 中的自定义小部件 - 图1

图:刻录小部件

在本章中,我们在 GTK# 中创建了一个自定义窗口小部件。