Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a button which resizes window when dragged in WPF?

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:

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...

So how can I do this in WPF?

like image 327
Tibi Avatar asked Dec 17 '25 20:12

Tibi


1 Answers

There exists a Control for that: a Thumb:

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

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>
like image 75
fixagon Avatar answered Dec 19 '25 10:12

fixagon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!