Gtk# MessageDialog,如何在消息足够长时显示滚动条(设置最大行数)?

huangapple go评论59阅读模式
英文:

Gtk# MessageDialog, How to show scrollbar when the message is long enough (setting max lines)?

问题

我正在使用 Gtk#,但原则上与常规的 GTK3 可能相同。我创建了默认的 GtkApplication,并将以下代码添加到脚手架以显示带有多行文本(在示例中为 50 行)的消息对话框。

问题是,当行数太多时,对话框窗口会增长超出屏幕高度,以至于下面的按钮变得不可见。是否有办法在消息过长时显示垂直滚动条,并确保对话框不会增长超出屏幕高度?

Gtk.MessageDialog 中是否有此功能,还是我必须使用自定义对话窗口?

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);

            DeleteEvent += Window_DeleteEvent;
            _button1.Clicked += Button1_Clicked;

            var lines = new List<string>();
            for (int i = 0; i < 50; i++)
            {
                lines.Add("Line " + i);
            }

            var m = new Gtk.MessageDialog(this,
                DialogFlags.Modal,
                MessageType.Warning,
                ButtonsType.Cancel,
                string.Join("\r\n", lines)
            );

            var res = m.Run();
            m.Destroy();
        }
英文:

I am using Gtk#, but the principle is probably the same as regular GTK3. I created the default GtkApplication, and added the following lines to the scaffolding to show a message dialogue with multi-line text (50 in the example).

The problem is that when the lines are too many, the dialogue window grows beyond the screen height, so that the button below becomes invisible. Is there a way to make it show a vertical scrollbar when the message is too long, and ensure that the dialogue does not grow beyond the screen height?

Is there such a feature in the Gtk.MessageDialog, or do I have to use a custom dialogue window?

<!-- language: c# -->

    private MainWindow(Builder builder) : base(builder.GetRawOwnedObject(&quot;MainWindow&quot;))
    {
        builder.Autoconnect(this);

        DeleteEvent += Window_DeleteEvent;
        _button1.Clicked += Button1_Clicked;

        var lines = new List&lt;string&gt;();
        for (int i = 0; i &lt; 50; i++)
        {
            lines.Add(&quot;Line &quot; + i);
        }
        
        var m = new Gtk.MessageDialog(this,
            DialogFlags.Modal,
            MessageType.Warning,
            ButtonsType.Cancel,
            string.Join(&quot;\r\n&quot;, lines)
        );

        var res = m.Run();
        m.Destroy();
    }

答案1

得分: 0

我不认为GtkMessageDialog支持此功能。如果您希望为您的对话框添加特殊行为,您应该使用GtkDialog,它更通用,并允许在其“内容区域”中指定小部件的类型。请查找Gtk#中的gtk_get_content_area等效函数。此函数返回一个GtkBox,您可以在其中添加任何小部件。您可以在其中添加带有滚动条的内容。

在Gtk3演示中查找“交互式对话框”以查看示例(运行gtk3-demo)。此示例还可在GNOME存储库(使用C语言编写)中找到。

英文:

I don't think this feature exists for GtkMessageDialog. If you want special behavior for your dialog, you should use a GtkDialog which is more generic and allows specifying the type of widget in its "Content area". Look for the
equivalent of gtk_get_content_area in Gtk#. This function returns a GtkBox in which you can pack whatever widget you want. You could pack something with scrollbars inside.

Look for the "interactive dialog" in the Gtk3 demos (launch gtk3-demo) to see an example. It is also available on the Gnome repository (in C).

huangapple
  • 本文由 发表于 2023年2月27日 13:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定