使用 MonoDevelop 和 GTK#、C# 将一个 window 放在另一个之上

Position one window on top of another using MonoDevelop and GTK#, C#

我想知道是否有人可以帮助我我是 GTK# 和 MonoDevelop 的新手,我似乎无法弄清楚如何在另一个 window 前面放置一个飞溅 window。当启动画面 window 加载时,它位于屏幕的右侧,主 window 位于中心,我希望它们都位于中心。我曾经使用过 GUI 生成器,并确保两者在 Designer->Properties

中都有一个中心位置

主要Window

public partial class MainWindow: Gtk.Window
{
   public MainWindow () : base (Gtk.WindowType.Toplevel)
   {
      Build ();
   }

   protected void OnDeleteEvent (object sender, DeleteEventArgs a)
   {
      Application.Quit ();
      a.RetVal = true;
   }
}

启动画面window

public partial class SplashScreenWindow : Gtk.Window
{
    public SplashScreenWindow () :
        base (Gtk.WindowType.Toplevel)
    {
        this.Build ();
    }
}

主要用于显示和隐藏启动画面window

    public static void Main (string[] args)
    {
        Application.Init ();
        SplashScreenWindow s = new SplashScreenWindow ();
        s.Title = @"I am a Splash Screen";
        MainWindow win = new MainWindow ( );
        System.Threading.Thread.Sleep (1000);
        win.Title = @"I am a Menu";
        win.Visible = false;
        s.Show ();
        s.Visible = false;
        s.Dispose ();

        win.Show ();
        Application.Run ();
    }
}

通过 GdkWindow.Screen 获取主屏幕的尺寸 (width/height) 并在 window 上执行 Move 将移动调整为其尺寸的一半。

示例:

MainWindow win = new MainWindow();
var screen = win.GdkWindow.Screen;
win.GdkWindow.GetSize(out var winWidth, out var winHeight);
win.Move((screen.Width / 2) - (winWidth / 2), (screen.Height / 2) - (winHeight / 2));
Application.Run();
win.Show();

最后这个对我来说效果最好

public SplashWindow()
    : base(Gtk.WindowType.Toplevel)
{
    this.Build();
    this.SetDefaultSize(250, 250);
    this.SetPosition(WindowPosition.Center);

    ThreadStart tStart = new ThreadStart(this.EndSplash);
    Thread t = new Thread(tStart);
    t.Start();
    Build();
}

public void EndSplash()
{
    Thread.Sleep(1000);
    Gtk.Application.Invoke(
        delegate (object sender, EventArgs args)
        {
            StartApplication();
        }
    );
}

private void StartApplication()
{
    this.Destroy();
    FSD.WelcomeWindow welcome = new FSD.WelcomeWindow();
    welcome.Show();

}

然后在 Main

class MainClass
{
    public static void Main(string[] args)
    {
        Application.Init();
        SplashWindow splash = new SplashWindow();
        splash.Show();
        Application.Run();
    }
}