ghost1372 / HandyControls

Contains some simple and commonly used WPF controls based on HandyControl
https://ghost1372.github.io/
MIT License
1.07k stars 102 forks source link

App sometimes gets cut when maximized #182

Closed NotroDev closed 1 year ago

NotroDev commented 1 year ago

Describe the bug

App sometimes gets cut in left top corner when maximized.

Steps to reproduce the bug

  1. Create window like this https://github.com/NotroDev/SkEditorPlus/blob/main/SkEditorPlus/MainWindow.xaml.cs
  2. Minimize it
  3. Maximize it by moving it to screen's top

Expected behavior

No response

Screenshots

image

NuGet package version

HandyControls (Custom version) 3.4.0

IDE

Visual Studio 2022

Framework type

.Net 6.0

Windows version

Windows 11 (22000)

Additional context

HandyControls' version is 3.4.4.

NotroDev commented 1 year ago

I think I fixed it with this code:

[DllImport("user32.dll")]
static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
const uint GW_HWNDNEXT = 2;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    FixCut();
}

private void FixCut()
{
    if (WindowState == WindowState.Maximized)
    {
        IntPtr hWnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;

        IntPtr hNext = hWnd;
        do
            hNext = GetWindow(hNext, GW_HWNDNEXT);
        while (!IsWindowVisible(hNext));

        SetForegroundWindow(hNext);

        Activate();
    }
}

private void Window_StateChanged(object sender, EventArgs e)
{
    FixCut();
}
ghost1372 commented 1 year ago

Tnx but i am not working with wpf anymore