Gtk:禁止GtkWindow的垂直大小调整

时间:2011-02-03 12:56:46

标签: c gtk window-resize

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

3 个答案:

答案 0 :(得分:5)

我相信您可以尝试使用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;
}

希望这有帮助,尊重

答案 1 :(得分:0)

这是我在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);
}

答案 2 :(得分:0)

根据@ 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;
}
相关问题