如何处理Gtk::Window的窗口大小调整事件?
处理Gtk::Window窗口大小调整事件的方法
你可以通过连接Gtk::Widget的signal_size_allocate()信号来处理窗口大小调整事件,这个信号会在窗口大小分配完成后触发,传递包含宽高信息的当前分配区域。另外也可使用signal_configure_event(),前者更贴合大小调整的场景。
修改后的示例代码
#include <gtkmm.h> class Window : public Gtk::Window { public: Window() { // 将size_allocate信号绑定到自定义处理函数 signal_size_allocate().connect(sigc::mem_fun(*this, &Window::on_size_allocate)); }; void on_size_allocate(const Gtk::Allocation& allocation) { // 获取当前窗口的宽高 int width = allocation.get_width(); int height = allocation.get_height(); // 在此添加你的大小调整处理逻辑 }; }; int main(int argc, char **argv) { auto app = Gtk::Application::create("hu.azsn.window"); return app->make_window_and_run<Window>(argc, argv); }
补充说明
signal_size_allocate()会在窗口大小或位置变化时触发,Gtk::Allocation对象包含当前窗口的位置、宽度和高度数据。- 若需在窗口大小调整的事件阶段(而非分配完成后)处理,可使用
signal_configure_event(),它传递的GdkEventConfigure*同样包含宽高信息:// 在构造函数中绑定信号 signal_configure_event().connect(sigc::mem_fun(*this, &Window::on_configure_event)); // 自定义处理函数 bool on_configure_event(GdkEventConfigure* event) { int width = event->width; int height = event->height; // 处理逻辑,返回true表示事件已处理,false表示允许事件继续传递 return false; }
内容的提问来源于stack exchange,提问作者mltm
相关产品推荐
相关产品推荐

