Snapping / Sticky WPF Windows
Asked Answered
C

2

27

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

Chun answered 22/6, 2009 at 16:3 Comment(4)
This works great. How can we modify this sample to disable aero snap feature for the window ?Pigg
The solution link is dead... Could you reupload it?Elsie
@ErikBRB yeah, that hosting is dead now. Look in accepted answer below for other solution which I found better later on. https://mcmap.net/q/512280/-snapping-sticky-wpf-windowsChun
@Chun Which answer is the accepted one? I don't see either answer marked as accepted.Perjure
O
2

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.

Orang answered 4/3, 2022 at 17:44 Comment(0)
E
0

The WPF Docking Library may provide some of what you are looking for, but I'm unsure if it works on the entire screen or just on your application window.

Enthronement answered 23/6, 2009 at 17:25 Comment(1)
I have tried project demo. Snapping windows functionality does not work between windows inside application. Just docking for windows on main window.Chun

© 2022 - 2024 — McMap. All rights reserved.