窗口拖动时的动态边距 [英] Dynamic Margin on Window Drag

查看:118
本文介绍了窗口拖动时的动态边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,由于缺少答复,请完全重写问题.我想要一个可拖动的窗口,但是当它被拖动时,请更改边距以扩展到窗口的旧位置. IE.窗口向右移动X,向左扩展边界.现在,我遇到了一些障碍,例如由于某种原因而将其边缘切掉的窗口.这是我的代码,让我知道您是否能发现任何东西!

Ok so complete rewrite of the question due to lack of replies. I want a window that is drag-able but as it's being dragged, alter the margin to extend as far as the old position of the window. I.e. Window moves right X, extend margin left X. Now I've hit a few snags such as the window having it's edges cut off for some reason. Here's my code, let me know if you can spot anything!

private void Window_LocationChanged(object sender, EventArgs e)
        {
            double TmpLeft = Math.Abs(this.Left - WinLeft);
            double TmpTop = Math.Abs(this.Top - WinTop);

        if (this.IsLoaded)
        {//depending on whether the window is moved left, right
            if (this.Left > WinLeft)
            {//depending on whether the window is moved up, down
                if (this.Top > WinTop)
                    bdr.Margin = new Thickness(TmpLeft, TmpTop, 0, 0);
                else
                    bdr.Margin = new Thickness(TmpLeft, 0, 0, TmpTop);
            }
            else
            {
                if (this.Top > WinTop)
                    bdr.Margin = new Thickness(0, TmpTop, TmpLeft+ 40, 0);
                else
                    bdr.Margin = new Thickness(0, 0, TmpLeft, TmpTop);
            }
        }
    }

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WinLeft = this.Left;
            WinTop = this.Top;
            bdr.Height = this.ActualHeight;//I set these because they are auto
            bdr.Width = this.ActualWidth;  //before the window opens
        }

此刻,整个窗口(将窗口背景设置为黄色,以便可以看到页边距)正在移动,在此情况下,我希望页边距的左上角保持不变.我还注意到,单击以拖动窗口的区域(border)也会移动,因此在移动窗口时不再位于单击之下.希望一切都清楚,对任何其他问题发表评论.

At the moment the whole window (set window background to yellow so I can see the margin) is moving, where as I want the top left corner of the margin to remain in place. I've also noticed that the area I click on to drag the window (a border) tends to move around as well so is no longer under my click as I move the window. Hope that's clear, comment any further questions.

旧-仅阅读了解我想做的事情

因此,我正在尝试创建一个具有弹出窗口的应用程序,其指针/线从子窗口指向父窗口中的特定位置.我是这样实现的;

So I'm trying to create an application that has a pop up window with a pointer/line coming from the child window to a particular place in the parent window. I achieved this like so;

<Border Name="brdr" Margin="40,0,0,0" >
    //Content
</Border>

<Line
        Name="Pointer"
        X1="0"
        X2="40"
        Y1="55"
        Y2="50"
        StrokeThickness="2"
        Stroke="Black"
></Line>

请注意边框上的左40个Margin会使窗口大于其显示的大小,以便Polygon突出到左侧并指向父窗口(如果有更好/更凉/更优雅的方式,这样做,我很高兴.

Notice the 40 left Margin on the border that makes the window larger than it appears so that the Polygon sticks out to the left and points to the parent window (If there's a better/cooler/more elegant way of doing this, I'm all ears).

所以工作正常,但现在我希望指针是动态的.如图所示,如果子窗口被拖动,则指针必须相对于父窗口的位置进行缩放. IE.它必须看起来像两个窗口都通过该行连接在一起.现在我的计划是记录子窗口在其上打开的点(因为它相对于父窗口打开,因此在初始化时是正确的),然后使用该位置与新位置点之间的差值(拖动后)来查找新点该行应该去.我的代码可能说得比以往任何时候都好...

So that worked fine but now I want the pointer to be dynamic. As in, if the child window gets dragged the pointer must scale relatively to the parent window's location. I.e. it must appear as if the two windows are attached via the line. Now my plan was to record the point the child window opens on (because it opens relative to the parent window, it's correct when it initialises) and then use the difference between this and the new location point (after dragging) to find the new points that the line should be going to. My code probably says it better than I ever could...

    private void Window_LocationChanged(object sender, EventArgs e)
    {
        if (this.IsLoaded)
        {
            brdr.Margin = new Thickness(Math.Abs(this.Left - WinLeft) + 40, Math.Abs(this.Top - WinTop), 0, 0);
            Pointer.X1 = Math.Abs(this.Left - WinLeft);
            Pointer.Y1 = Math.Abs(this.Top - WinTop) + 55;
            Pointer.X2 = Math.Abs(this.Left - WinLeft) + 40;
            Pointer.Y2 = Math.Abs(this.Top - WinTop) + 50;
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        WinLeft = this.Left;
        WinTop = this.Top;
    }

如您所见,我必须设置窗口边距,以使其延伸到旧位置.然后,将Line坐标重置为新值.如我所说,所有这些值都是通过将打开窗口的坐标与当前坐标进行比较来计算的.

As you can see I have to set the window margin so that it extends to the old position. Then I reset the Line coords to the new values. All these values are calculated, like I said, by comparing the opening window coords to the current coords.

我的问题是,这是不对的.看到有人能够弄清楚这一点,将会给我留下深刻的印象.

My problem is, this isn't right. Would be very impressed to see someone able to figure this out.

推荐答案

实际上找到了一种很好的方法.我要做的是设置子窗口的WindowState="Maximized",然后将除Line之外的所有内容都放置在Canvas中,并遵循

Actually found a great way of doing this. What I did was I set the child window's WindowState="Maximized" then placed everything except the Line in a Canvas and by following http://denismorozov.blogspot.ie/2008/01/drag-controls-in-wpf-using.html (great article) I could make it so that I could move around my window. I had hard coded the line so that when the window opens it's in the correct position. Since one of the points of the Line shouldn't move, it was just a matter of updating the other point in the Canvas_MouseMove event like so;

Point p = border.TransformToAncestor(this).Transform(new Point(0, 0));
Line.X2 = p.X;
Line.Y2 = p.Y + 50;

如果要使两者连接的线,则是一种非常简单的方法来使窗口相对于父窗口显示.它具有移动窗口的外观,但实际上是在最大化的透明窗口周围移动控件.

Very simple way of getting your window to display relative to the parent window if you want a line joining the two. It gives the appearance of moving the window, but your actually moving a control around a maximised transparent window.

这篇关于窗口拖动时的动态边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆