在gtk中捕获滚动事件#

时间:2022-06-04 00:00:08

Which event from which widget should I catch when I need to run some code when ScrolledWindow is scrolled?

当滚动ScrolledWindow时我需要运行一些代码时,我应该捕获哪个小部件?

Ths widgets tree I am using is: (my widget : Gtk.Container) > Viewport > ScrolledWindow

我正在使用的小部件树是:(我的小部件:Gtk.Container)>视口> ScrolledWindow

I tried many combinations of ScrollEvent, ScrollChild, etc. event handlers connected to all of them, but the only one that runs anything is an event from Viewport that about SetScrollAdjutstments being changed to (x=0,y=0) when the application starts.

我尝试了连接到所有这些事件的ScrollEvent,ScrollChild等事件处理程序的许多组合,但是唯一运行任何东西的是来自Viewport的事件,当应用程序启动时,SetScrollAdjutstments被更改为(x = 0,y = 0) 。

2 个解决方案

#1


You should attach to the GtkAdjustment living in the relevant scrollbar, and react to its "changed" event. Since Scrollbars are Ranges, you use the gtk_range_get_adjustment() call to do this.

您应该附加到相关滚动条中的GtkAdjustment,并对其“已更改”事件做出反应。由于Scrollbars是Ranges,因此您可以使用gtk_range_get_adjustment()调用来执行此操作。

#2


unwind's answer was correct. Just posting my code in case someone needs a full solution:

放松的答案是正确的。只需发布我的代码,以防有人需要完整的解决方案:

// in the xxx : Gtk.Container class:

protected override void OnParentSet(Widget previous_parent) {
    Parent.ParentSet += HandleParentParentSet;
}

void HandleParentParentSet(object o, ParentSetArgs args) {
    ScrolledWindow swn = (o as Widget).Parent as ScrolledWindow;
    swn.Vadjustment.ValueChanged += HandleScrollChanged;
}

void HandleScrollChanged(object sender, EventArgs e) {
    // vertical value changed
}

If you need to change the parent of any of those widgets, or may need to change the types and change the hardcoded types and handle disconnecting from the previous parent.

如果您需要更改任何这些小部件的父级,或者可能需要更改类型并更改硬编码类型并处理与上一个父级的断开连接。

#1


You should attach to the GtkAdjustment living in the relevant scrollbar, and react to its "changed" event. Since Scrollbars are Ranges, you use the gtk_range_get_adjustment() call to do this.

您应该附加到相关滚动条中的GtkAdjustment,并对其“已更改”事件做出反应。由于Scrollbars是Ranges,因此您可以使用gtk_range_get_adjustment()调用来执行此操作。

#2


unwind's answer was correct. Just posting my code in case someone needs a full solution:

放松的答案是正确的。只需发布我的代码,以防有人需要完整的解决方案:

// in the xxx : Gtk.Container class:

protected override void OnParentSet(Widget previous_parent) {
    Parent.ParentSet += HandleParentParentSet;
}

void HandleParentParentSet(object o, ParentSetArgs args) {
    ScrolledWindow swn = (o as Widget).Parent as ScrolledWindow;
    swn.Vadjustment.ValueChanged += HandleScrollChanged;
}

void HandleScrollChanged(object sender, EventArgs e) {
    // vertical value changed
}

If you need to change the parent of any of those widgets, or may need to change the types and change the hardcoded types and handle disconnecting from the previous parent.

如果您需要更改任何这些小部件的父级,或者可能需要更改类型并更改硬编码类型并处理与上一个父级的断开连接。