如何在WPF中拖动时创建一个调整窗口大小的按钮?

时间:2022-02-11 07:14:30

I'm trying to make a button which resizes the window when it is dragged, just like the bottom right corner of a window.

我正在尝试制作一个按钮,在拖动窗口时调整窗口大小,就像窗口的右下角一样。

I tried using the .MouseMove and .MouseDown events, but it doesn't work. Here is an example of my code:

我尝试使用.MouseMove和.MouseDown事件,但它不起作用。以下是我的代码示例:

void ButtonResize_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        Point pos = e.GetPosition(Window);
        Window.Width += (pos - MousePos_OLD).X;
        Window.Height += (pos - MousePos_OLD).Y;
    }

    MousePos_OLD = e.GetPosition(Window);
}

I also tried using the MouseDown event, and a MouseMove event was taking care of updating the mouse position, but nothing...

我也试过使用MouseDown事件,并且MouseMove事件正在处理更新鼠标位置,但没有...

So how can I do this in WPF?

那我怎么能在WPF中做到这一点?

1 个解决方案

#1


4  

There exists a Control for that: a Thumb:

存在一个控件:拇指:

It containes an event events DragDelta which is what you search for.

它包含一个事件事件DragDelta,这是您搜索的内容。

EDIT:

编辑:

to customize his visualization you can set him a Style

要自定义他的可视化,您可以为他设置样式

<Style x:Key="ThumbStyle" TargetType="{x:Type Thumb}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border BorderBrush="Black" BorderThickness="1" Background="Transparent"></Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

#1


4  

There exists a Control for that: a Thumb:

存在一个控件:拇指:

It containes an event events DragDelta which is what you search for.

它包含一个事件事件DragDelta,这是您搜索的内容。

EDIT:

编辑:

to customize his visualization you can set him a Style

要自定义他的可视化,您可以为他设置样式

<Style x:Key="ThumbStyle" TargetType="{x:Type Thumb}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border BorderBrush="Black" BorderThickness="1" Background="Transparent"></Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>