Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snapping / Sticky WPF Windows

I'm looking for solution to add snapping/sticky windows functionallity (winamp-like) to existing WPF application. Same thing as it was asked here, just I need it for WPF.

It doesn't have to have docking function, just to snap to border of other windows inside same application and edge of screen (including taskbar) if possible. Preferably open source solution.

Thanks

like image 291
Andrija Avatar asked Sep 08 '25 11:09

Andrija


1 Answers

Here is the Solution you actually asked for:

Let's say we have 2 Xaml windows named MainWindow and Window2:

MainWindow:

 Window2 windows2;

        public void RealodPos()
        {
            if (windows2 == null) { windows2 = new Window2(this); this.Top = 300; }

            windows2.Top = this.Top;
            windows2.Left = this.Width + this.Left - 15;
            windows2.Show();

        }

        private void Window_Activated(object sender, EventArgs e)
        {
            RealodPos();
        }

        private void SizeChenged(object sender, SizeChangedEventArgs e)
        {
            RealodPos();
        }

        private void LocationChange(object sender, EventArgs e)
        {
            RealodPos();
        }

Window2:

   public partial class Window2 : Window
    {

        MainWindow Firstwin;
        public Window2(MainWindow FirstWindow)
        {
            InitializeComponent();
            Firstwin = FirstWindow;
        }
        void RealodPos() 
        {
            this.Top = Firstwin.Top;
            this.Left = Firstwin.Width + Firstwin.Left - 15;
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            RealodPos();
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            RealodPos();
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RealodPos();
        }
    }

Result:

My suggestion as a Software engineer:

Hint1: I don't know where you will use this but it's better to convert it to a reusable component that is not hardcoded with only 2 windows.

Hint2: Convert the

public Window2(MainWindow FirstWindow)

's MainWindow argument to a Window class formant to have a more flexible pointer for reusing it in the other applications.

Here is my suggested Solution for pro-WPF developers:

instead of doing this in that way you can make your own customized windows on XAML and use UserControls instead of other windows that you need.

Thanks for reading, please ask me if you want anything else or if you need the code as a project file.

like image 77
Mohamad Mert Avatar answered Sep 11 '25 00:09

Mohamad Mert