© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
2 replies
FaNim

WPF custom resizable window

#region ResizeWindows
private void Resize_Init(object sender, MouseButtonEventArgs e)
{
    if (sender is not Rectangle senderRect) return;

    ResizeInProcess = true;
    senderRect.CaptureMouse();
}

private void Resize_End(object sender, MouseButtonEventArgs e)
{
    if (sender is not Rectangle senderRect) return;

    ResizeInProcess = false; ;
    senderRect.ReleaseMouseCapture();
}

private void Resizeing_Form(object sender, MouseEventArgs e)
{
    if (!ResizeInProcess) return;
    if (sender is not Rectangle senderRect) return;

    Window mainWindow = senderRect.Tag as Window;
    double width = e.GetPosition(mainWindow).X;
    double height = e.GetPosition(mainWindow).Y;
    double temp = 0;
    senderRect.CaptureMouse();

    if (senderRect.Name.Contains("right", StringComparison.OrdinalIgnoreCase))
    {
        width += 5;
        if (width > 0)
            mainWindow.Width = width;
    }
    if (senderRect.Name.Contains("left", StringComparison.OrdinalIgnoreCase))
    {
        width -= 5;
        temp = mainWindow.Width - width;
        if ((temp > mainWindow.MinWidth) && (temp < mainWindow.MaxWidth))
        {
            mainWindow.Width = temp;
            mainWindow.Left += width;
        }
    }
    if (senderRect.Name.Contains("bottom", StringComparison.OrdinalIgnoreCase))
    {
        height += 5;
        if (height > 0)
            mainWindow.Height = height;
    }
    if (senderRect.Name.ToLower().Contains("top", StringComparison.OrdinalIgnoreCase))
    {
        height -= 5;
        temp = mainWindow.Height - height;
        if ((temp > mainWindow.MinHeight) && (temp < mainWindow.MaxHeight))
        {
            mainWindow.Height = temp;
            mainWindow.Top += height;
        }
    }
}
#endregion
#region ResizeWindows
private void Resize_Init(object sender, MouseButtonEventArgs e)
{
    if (sender is not Rectangle senderRect) return;

    ResizeInProcess = true;
    senderRect.CaptureMouse();
}

private void Resize_End(object sender, MouseButtonEventArgs e)
{
    if (sender is not Rectangle senderRect) return;

    ResizeInProcess = false; ;
    senderRect.ReleaseMouseCapture();
}

private void Resizeing_Form(object sender, MouseEventArgs e)
{
    if (!ResizeInProcess) return;
    if (sender is not Rectangle senderRect) return;

    Window mainWindow = senderRect.Tag as Window;
    double width = e.GetPosition(mainWindow).X;
    double height = e.GetPosition(mainWindow).Y;
    double temp = 0;
    senderRect.CaptureMouse();

    if (senderRect.Name.Contains("right", StringComparison.OrdinalIgnoreCase))
    {
        width += 5;
        if (width > 0)
            mainWindow.Width = width;
    }
    if (senderRect.Name.Contains("left", StringComparison.OrdinalIgnoreCase))
    {
        width -= 5;
        temp = mainWindow.Width - width;
        if ((temp > mainWindow.MinWidth) && (temp < mainWindow.MaxWidth))
        {
            mainWindow.Width = temp;
            mainWindow.Left += width;
        }
    }
    if (senderRect.Name.Contains("bottom", StringComparison.OrdinalIgnoreCase))
    {
        height += 5;
        if (height > 0)
            mainWindow.Height = height;
    }
    if (senderRect.Name.ToLower().Contains("top", StringComparison.OrdinalIgnoreCase))
    {
        height -= 5;
        temp = mainWindow.Height - height;
        if ((temp > mainWindow.MinHeight) && (temp < mainWindow.MaxHeight))
        {
            mainWindow.Height = temp;
            mainWindow.Top += height;
        }
    }
}
#endregion


hello is there a better way to make custom resizeable window because it cause more memory use and its not smooth at all?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ wpf custom window
C#CC# / help
3y ago
WPF Window Navigation
C#CC# / help
12mo ago
Make window only resizable in the diagonal axis
C#CC# / help
2y ago
Find popup window in Wpf
C#CC# / help
2y ago