C# 如何使用gtk发送事件#
标题真的说明了一切 我有一些定制的小部件,我希望在不进行太多丑陋的攻击的情况下对其进行响应——也就是说,我希望以与处理(比如)按钮相同的方式处理它们,并响应来自小部件本身之外的任何事件C# 如何使用gtk发送事件#,c#,gtk#,C#,Gtk#,标题真的说明了一切 我有一些定制的小部件,我希望在不进行太多丑陋的攻击的情况下对其进行响应——也就是说,我希望以与处理(比如)按钮相同的方式处理它们,并响应来自小部件本身之外的任何事件 更具体地说,其中一个小部件包含按钮列表;单击其中一个按钮时,我需要根据单击的按钮修改小部件外部的数据。我可以给小部件一个要修改的对象(在本例中为字符串),但我宁愿避免这样做,而只是正确地操作。不知道是否有一些带有Gtk的示例,这是一个用于mono和.Net的图形用户界面工具包。绑定gtk+工具包和各种GNOME库
更具体地说,其中一个小部件包含按钮列表;单击其中一个按钮时,我需要根据单击的按钮修改小部件外部的数据。我可以给小部件一个要修改的对象(在本例中为字符串),但我宁愿避免这样做,而只是正确地操作。不知道是否有一些带有
Gtk
的示例,这是一个用于mono
和.Net
的图形用户界面工具包。绑定gtk+工具包和各种GNOME库对您很有用
你可以学习几个例子。请看这个处理按钮的示例
/* Drawing Area
*
* GtkDrawingArea is a blank area where you can draw custom displays
* of various kinds.
*
* This demo has two drawing areas. The checkerboard area shows
* how you can just draw something; all you have to do is write
* a signal handler for the Drawn event, as shown here.
*
* The "scribble" area is a bit more advanced, and shows how to handle
* events such as button presses and mouse motion. Click the mouse
* and drag in the scribble area to draw squiggles. Resize the window
* to clear the area.
*/
using System;
using Gtk;
using Gdk;
namespace GtkDemo
{
[Demo ("Drawing Area", "DemoDrawingArea.cs")]
public class DemoDrawingArea : Gtk.Window
{
private Cairo.Surface surface = null;
public DemoDrawingArea () : base ("Drawing Area")
{
BorderWidth = 8;
VBox vbox = new VBox (false, 8);
vbox.BorderWidth = 8;
Add (vbox);
// Create the checkerboard area
Label label = new Label ("<u>Checkerboard pattern</u>");
label.UseMarkup = true;
vbox.PackStart (label, false, false, 0);
Frame frame = new Frame ();
frame.ShadowType = ShadowType.In;
vbox.PackStart (frame, true, true, 0);
DrawingArea da = new DrawingArea ();
// set a minimum size
da.SetSizeRequest (100,100);
frame.Add (da);
da.Drawn += new DrawnHandler (CheckerboardDrawn);
// Create the scribble area
label = new Label ("<u>Scribble area</u>");
label.UseMarkup = true;
vbox.PackStart (label, false, false, 0);
frame = new Frame ();
frame.ShadowType = ShadowType.In;
vbox.PackStart (frame, true, true, 0);
da = new DrawingArea ();
// set a minimum size
da.SetSizeRequest (100, 100);
frame.Add (da);
// Signals used to handle backing pixmap
da.Drawn += new DrawnHandler (ScribbleDrawn);
da.ConfigureEvent += new ConfigureEventHandler (ScribbleConfigure);
// Event signals
da.MotionNotifyEvent += new MotionNotifyEventHandler (ScribbleMotionNotify);
da.ButtonPressEvent += new ButtonPressEventHandler (ScribbleButtonPress);
// Ask to receive events the drawing area doesn't normally
// subscribe to
da.Events |= EventMask.LeaveNotifyMask | EventMask.ButtonPressMask |
EventMask.PointerMotionMask | EventMask.PointerMotionHintMask;
ShowAll ();
}
protected override bool OnDeleteEvent (Gdk.Event evt)
{
Destroy ();
return true;
}
private void CheckerboardDrawn (object o, DrawnArgs args)
{
const int CheckSize = 10;
const int Spacing = 2;
Widget widget = o as Widget;
Cairo.Context cr = args.Cr;
int i, j, xcount, ycount;
// At the start of a draw handler, a clip region has been set on
// the Cairo context, and the contents have been cleared to the
// widget's background color.
Rectangle alloc = widget.Allocation;
// Start redrawing the Checkerboard
xcount = 0;
i = Spacing;
while (i < alloc.Width) {
j = Spacing;
ycount = xcount % 2; // start with even/odd depending on row
while (j < alloc.Height) {
if (ycount % 2 != 0)
cr.SetSourceRGB (0.45777, 0, 0.45777);
else
cr.SetSourceRGB (1, 1, 1);
// If we're outside the clip, this will do nothing.
cr.Rectangle (i, j, CheckSize, CheckSize);
cr.Fill ();
j += CheckSize + Spacing;
++ycount;
}
i += CheckSize + Spacing;
++xcount;
}
// return true because we've handled this event, so no
// further processing is required.
args.RetVal = true;
}
private void ScribbleDrawn (object o, DrawnArgs args)
{
Cairo.Context cr = args.Cr;
cr.SetSourceSurface (surface, 0, 0);
cr.Paint ();
}
// Create a new surface of the appropriate size to store our scribbles
private void ScribbleConfigure (object o, ConfigureEventArgs args)
{
Widget widget = o as Widget;
if (surface != null)
surface.Dispose ();
var allocation = widget.Allocation;
surface = widget.Window.CreateSimilarSurface (Cairo.Content.Color, allocation.Width, allocation.Height);
var cr = new Cairo.Context (surface);
cr.SetSourceRGB (1, 1, 1);
cr.Paint ();
((IDisposable)cr).Dispose ();
// We've handled the configure event, no need for further processing.
args.RetVal = true;
}
private void ScribbleMotionNotify (object o, MotionNotifyEventArgs args)
{
// paranoia check, in case we haven't gotten a configure event
if (surface == null)
return;
// This call is very important; it requests the next motion event.
// If you don't call Window.GetPointer() you'll only get a single
// motion event. The reason is that we specified PointerMotionHintMask
// in widget.Events. If we hadn't specified that, we could just use
// args.Event.X, args.Event.Y as the pointer location. But we'd also
// get deluged in events. By requesting the next event as we handle
// the current one, we avoid getting a huge number of events faster
// than we can cope.
int x, y;
ModifierType state;
args.Event.Window.GetPointer (out x, out y, out state);
if ((state & ModifierType.Button1Mask) != 0)
DrawBrush (o as Widget, x, y);
// We've handled it, stop processing
args.RetVal = true;
}
// Draw a rectangle on the screen
private void DrawBrush (Widget widget, double x, double y)
{
var update_rect = new Gdk.Rectangle ((int)x - 3, (int)y - 3, 6, 6);
var cr = new Cairo.Context (surface);
Gdk.CairoHelper.Rectangle (cr, update_rect);
cr.Fill ();
((IDisposable)cr).Dispose ();
widget.Window.InvalidateRect (update_rect, false);
}
private void ScribbleButtonPress (object o, ButtonPressEventArgs args)
{
// paranoia check, in case we haven't gotten a configure event
if (surface == null)
return;
EventButton ev = args.Event;
if (ev.Button == 1)
DrawBrush (o as Widget, ev.X, ev.Y);
// We've handled the event, stop processing
args.RetVal = true;
}
}
}
/*绘图区域
*
*GtkDrawingArea是一个空白区域,您可以在其中绘制自定义显示
*各种各样的。
*
*此演示有两个绘图区域。棋盘区域显示
*你怎么能画一些东西;你所要做的就是写作
*绘制事件的信号处理程序,如下所示。
*
*“scribble”区域稍微高级一点,显示了如何处理
*按钮按下和鼠标移动等事件。点击鼠标
*并在涂鸦区域中拖动以绘制曲线。调整窗口大小
*清理该区域。
*/
使用制度;
使用Gtk;
使用Gdk;
名称空间GtkDemo
{
[演示(“绘图区域”、“DegrawingArea.cs”)]
公共类解调区域:Gtk.Window
{
private Cairo.Surface=null;
公共绘图区域():基础(“绘图区域”)
{
边界宽度=8;
VBox VBox=新的VBox(假,8);
vbox.BorderWidth=8;
添加(vbox);
//创建棋盘格区域
标签标签=新标签(“棋盘格图案”);
label.UseMarkup=true;
vbox.PackStart(标签,false,false,0);
框架=新框架();
frame.ShadowType=ShadowType.In;
vbox.PackStart(frame,true,true,0);
DrawingArea da=新DrawingArea();
//设定最小尺寸
da.SetSizeRequest(100100);
帧。添加(da);
da.Drawed+=新DrawnHandler(棋盘画);
//创建涂鸦区域
标签=新标签(“涂鸦区”);
label.UseMarkup=true;
vbox.PackStart(标签,false,false,0);
帧=新帧();
frame.ShadowType=ShadowType.In;
vbox.PackStart(frame,true,true,0);
da=新绘图区域();
//设定最小尺寸
da.SetSizeRequest(100100);
帧。添加(da);
//用于处理支持pixmap的信号
da.Drawed+=新DrawnHandler(ScribbleDrawed);
da.ConfigureEvent+=新的ConfigureEventHandler(ScribbleConfigure);
//事件信号
da.MotionNotifyEvent+=新的MotionNotifyEventHandler(ScribbleMotionNotify);
da.ButtonPressEvent+=新的ButtonPressEventHandler(ScribbleButtonPress);
//请求接收绘图区域不正常的事件
//订阅
da.Events |=EventMask.LeaveNotifyMask | EventMask.buttonpressak|
EventMask.PointerMotionMask | EventMask.PointerMotionHintMask;
ShowAll();
}
受保护的覆盖布尔OnDeleteEvent(Gdk.Event evt)
{
销毁();
返回true;
}
绘制的专用空心棋盘(对象o、绘图参数)
{
常量int CheckSize=10;
常数int间距=2;
Widget-Widget=o作为Widget;
Cairo.Context cr=args.cr;
int i,j,xcount,ycount;
//在绘图处理程序的开始处,已在上设置剪辑区域
//Cairo上下文,并且内容已清除到
//控件的背景色。
矩形alloc=widget.Allocation;
//开始重新绘制棋盘
xcount=0;
i=间距;
而(i[Glib.Signal("Activated")]
public event EventHandler Activated;