Jimbo Jones
Jimbo Jones

Reputation: 1002

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

I am wondering if anyone can help me I am new to GTK# and MonoDevelop and I can not seem to figure out how to place a splash window in front of another window. When the splash window loads it is positioned to right hand side of the screen where the main window is positioned in the center and I want both of them to be center. I have used to use the GUI builder and have made sure that both have a position of center in the Designer->Properties

Main 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;
   }
}

Splash screen window

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

Main To show and Hide the splash 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 ();
    }
}

Upvotes: 0

Views: 1063

Answers (2)

Jimbo Jones
Jimbo Jones

Reputation: 1002

In the end this worked the best for me

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();

}

And then in Main

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

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74124

Get the size (width/height) of your primary screen via GdkWindow.Screen and perform a Move on the window adjusting the move by its half of its size.

Example:

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();

Upvotes: 1

Related Questions