Gtk:禁止 GtkWindow 垂直调整大小

发布于 2024-10-15 13:13:22 字数 82 浏览 1 评论 0原文

我找不到如何做到这一点:( 有 gtk_window_set_ressized,但它根本禁用调整大小,我仍然希望我的窗口水平调整大小。 有什么想法吗?

I can't find how to do that :( there's gtk_window_set_resizable, but it disables resizing at all and i still want my window to resize horizontally.
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪我终i 2024-10-22 13:13:22

我相信您可以尝试使用 gtk_window_set_geometry_hints 函数并指定窗口的最大和最小高度。在这种情况下,您仍然允许宽度变化,而高度保持不变。请检查下面的示例是否适合您:

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GdkGeometry hints;
    hints.min_width = 0;
    hints.max_width = gdk_screen_get_width(gtk_widget_get_screen(window));;
    hints.min_height = 300;
    hints.max_height = 300;

    gtk_window_set_geometry_hints(
        GTK_WINDOW(window),
        window,
        &hints,
        (GdkWindowHints)(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

希望这有帮助,问候

I believe you can try using gtk_window_set_geometry_hints function and specify max and min height for your window. In this case you would still allow width change whereas height will stay constant. Pls, check if an example below would work for you:

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GdkGeometry hints;
    hints.min_width = 0;
    hints.max_width = gdk_screen_get_width(gtk_widget_get_screen(window));;
    hints.min_height = 300;
    hints.max_height = 300;

    gtk_window_set_geometry_hints(
        GTK_WINDOW(window),
        window,
        &hints,
        (GdkWindowHints)(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

hope this helps, regards

塔塔猫 2024-10-22 13:13:22

这是我在 GTK 中的实现#

public static void SetFixedDimensions (
    Window window, bool vertical, bool horizontal)
{
    int width, height;
    window.GetSize(out width, out height);

    var hintGeometry = new Gdk.Geometry();

    hintGeometry.MaxHeight = vertical ? height : Int32.MaxValue;
    hintGeometry.MinHeight = vertical ? height : 0;

    hintGeometry.MaxWidth = horizontal ? width : Int32.MaxValue;
    hintGeometry.MinWidth = horizontal ? width : 0;

    window.SetGeometryHints(window, hintGeometry, Gdk.WindowHints.MaxSize);
}

Here's my implementation in GTK#

public static void SetFixedDimensions (
    Window window, bool vertical, bool horizontal)
{
    int width, height;
    window.GetSize(out width, out height);

    var hintGeometry = new Gdk.Geometry();

    hintGeometry.MaxHeight = vertical ? height : Int32.MaxValue;
    hintGeometry.MinHeight = vertical ? height : 0;

    hintGeometry.MaxWidth = horizontal ? width : Int32.MaxValue;
    hintGeometry.MinWidth = horizontal ? width : 0;

    window.SetGeometryHints(window, hintGeometry, Gdk.WindowHints.MaxSize);
}
榆西 2024-10-22 13:13:22

根据 @serge_gubenko 的答案,如果您想仅在初始布局后禁止垂直调整大小,则需要为信号 size-allocate 设置回调。

例子:

static gint signal_connect_id_cb_dialog_size_allocate;

static void
cb_dialog_size_allocate (GtkWidget    *window,
                         GdkRectangle *allocation,
                         gpointer      user_data)
{
        GdkGeometry hints;

        g_signal_handler_disconnect (G_OBJECT (dialog),
                                     signal_connect_id_cb_dialog_size_allocate);

        /* dummy values for min/max_width to not restrict horizontal resizing */
        hints.min_width = 0;
        hints.max_width = G_MAXINT;
        /* do not allow vertial resizing */
        hints.min_height = allocation->height;
        hints.max_height = allocation->height;
        gtk_window_set_geometry_hints (GTK_WINDOW (window), (GtkWidget *) NULL,
                                       &hints,
                                       (GdkWindowHints) (GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));
}

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    signal_connect_id_cb_dialog_size_allocate =
       g_signal_connect (G_OBJECT (state->dialog),
                         "size-allocate",
                         G_CALLBACK (cb_dialog_size_allocate),
                         (gpointer) NULL /* user_data */);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

Building upon @serge_gubenko's answer, if you want to forbid vertical resize only after the initial layout, you will need to setup a callback for signal size-allocate.

Example:

static gint signal_connect_id_cb_dialog_size_allocate;

static void
cb_dialog_size_allocate (GtkWidget    *window,
                         GdkRectangle *allocation,
                         gpointer      user_data)
{
        GdkGeometry hints;

        g_signal_handler_disconnect (G_OBJECT (dialog),
                                     signal_connect_id_cb_dialog_size_allocate);

        /* dummy values for min/max_width to not restrict horizontal resizing */
        hints.min_width = 0;
        hints.max_width = G_MAXINT;
        /* do not allow vertial resizing */
        hints.min_height = allocation->height;
        hints.max_height = allocation->height;
        gtk_window_set_geometry_hints (GTK_WINDOW (window), (GtkWidget *) NULL,
                                       &hints,
                                       (GdkWindowHints) (GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));
}

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    signal_connect_id_cb_dialog_size_allocate =
       g_signal_connect (G_OBJECT (state->dialog),
                         "size-allocate",
                         G_CALLBACK (cb_dialog_size_allocate),
                         (gpointer) NULL /* user_data */);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文