C#C
C#2y ago
FaNim

WPF custom resizable window

c#
#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?
Was this page helpful?