如何在程序启动时正确恢复Gtk窗口大小?

发布于 2024-12-10 05:58:04 字数 919 浏览 3 评论 0原文

我正在使用 GTK# GUI 框架用 C# 编写应用程序,以使其跨平台。 许多事情是在 GTK# 中完成的,与 WPF/Windows.Forms 中不同,但其中大部分我都能自己解决。

有一个“小”问题,当我试图解决它时,它变得不那么“小”。 当应用程序关闭时,我将窗口位置和最大化标志保存到配置文件中。 但是当我尝试恢复窗口参数时,它们没有正确恢复。

我尝试使用的方法:

SetSizeRequest():

this.SetSizeRequest((int)config["windowwidth"], (int)config["windowheight"]);

SetDefaultSize():

this.SetDefaultSize((int)config["windowwidth"], (int)config["windowheight"]);

Resize():

this.Resize((int)config["windowwidth"], (int)config["windowheight"]);

问题是: 其中一些方法根本不起作用(什么也不改变)。其他人的行为很奇怪:将窗口大小设置为不是从配置中加载的窗口大小,使窗口不断扩展等。

我还添加了对一个特定小部件的大小的限制:

Gdk.Geometry geom = new Gdk.Geometry();
geom.MinWidth = 800;
geom.MinHeight = 400;
this.SetGeometryHints(BoxNotebook, geom, Gdk.WindowHints.MinSize);

当我将所有这些一起使用时,它变得一团糟。 在程序启动时恢复窗口大小的正确方法是什么?

I am writing application in C# using GTK# GUI framework to make it cross-platform.
Many things are being done in GTK# not like in WPF/Windows.Forms, but most of them I was able to solve on my own.

There is a "little" problem, what became not that "little" when I tried to solve it.
I am saving window position and maximized flag to config file when application is being closed.
But when I am trying to restore window parameters, they are being restored not correctly.

Methods I tried to use:

SetSizeRequest():

this.SetSizeRequest((int)config["windowwidth"], (int)config["windowheight"]);

SetDefaultSize():

this.SetDefaultSize((int)config["windowwidth"], (int)config["windowheight"]);

Resize():

this.Resize((int)config["windowwidth"], (int)config["windowheight"]);

The problem is:
Some of these methods does not work at all (change nothing). Others behave strangely: set window size to not the one was loaded from config, make window continuously expanding, etc.

I am also adding a constraint on size of one specific widget:

Gdk.Geometry geom = new Gdk.Geometry();
geom.MinWidth = 800;
geom.MinHeight = 400;
this.SetGeometryHints(BoxNotebook, geom, Gdk.WindowHints.MinSize);

When I use all this together, it is becoming a complete mess.
What is the correct way to restore window size at program startup ?

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

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

发布评论

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

评论(1

大姐,你呐 2024-12-17 05:58:04

Resize 适用于我的应用程序。您确定从配置文件加载了正确的值吗?

编辑

这是一个 Glade 文件,似乎可以满足您的要求:

<?xml version="1.0"?>
<interface>
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkHPaned" id="hpaned">
        <child>
          <object class="GtkNotebook" id="notebook">
            <property name="width_request">300</property>
            <child>
              <object class="GtkScrolledWindow" id="scrolledwindow">
                <child>
                  <object class="GtkTreeView" id="treeview"/>
                </child>
              </object>
            </child>
            <child type="tab">
              <object class="GtkLabel" id="label1">
                <property name="label">300 px wide</property>
              </object>
              <packing>
                <property name="tab_fill">False</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="resize">True</property>
            <property name="shrink">False</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="width_request">400</property>
            <property name="label">400 px wide</property>
          </object>
          <packing>
            <property name="resize">False</property>
            <property name="shrink">False</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Resize works in my application. Are you sure that the correct values are being loaded from the config file?

EDIT

Here is a Glade file that seems to do what you want:

<?xml version="1.0"?>
<interface>
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkHPaned" id="hpaned">
        <child>
          <object class="GtkNotebook" id="notebook">
            <property name="width_request">300</property>
            <child>
              <object class="GtkScrolledWindow" id="scrolledwindow">
                <child>
                  <object class="GtkTreeView" id="treeview"/>
                </child>
              </object>
            </child>
            <child type="tab">
              <object class="GtkLabel" id="label1">
                <property name="label">300 px wide</property>
              </object>
              <packing>
                <property name="tab_fill">False</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="resize">True</property>
            <property name="shrink">False</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="width_request">400</property>
            <property name="label">400 px wide</property>
          </object>
          <packing>
            <property name="resize">False</property>
            <property name="shrink">False</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文