Skip to content

Commit

Permalink
When moving a window, it stays centered (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Sep 4, 2023
1 parent 5f5e439 commit 8956887
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions PermaTop/UserControls/WindowPropertyItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ private void MoveWindow(IntPtr windowHandle, int x, int y)
SetWindowPos(windowHandle, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out Global.RECT lpRect);

private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
try
Expand All @@ -163,7 +166,7 @@ private void MaxRestoreBtn_Click(object sender, RoutedEventArgs e)
MaxRestoreBtn.Content = "\uFA40";
MaxRestoreBtn.FontSize = 14;
}
catch { }
catch { }
}

private void MinBtn_Click(object sender, RoutedEventArgs e)
Expand All @@ -172,7 +175,7 @@ private void MinBtn_Click(object sender, RoutedEventArgs e)
{
SendMessage(WindowInfo.Hwnd, WM_SYSCOMMAND, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
}
catch { }
catch { }
}

private void MoreBtn_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -203,7 +206,19 @@ private void MoveBtn_Click(object sender, RoutedEventArgs e)
{
if (ScreenSelector.SelectedIndex >= 0)
{
MoveWindow(WindowInfo.Hwnd, _screens[ScreenSelector.SelectedIndex].Bounds.Left, _screens[ScreenSelector.SelectedIndex].Bounds.Top);
GetWindowRect(WindowInfo.Hwnd, out Global.RECT rect);
var screen = _screens[ScreenSelector.SelectedIndex];

int centerX = (screen.Bounds.Left + screen.Bounds.Right) / 2;
int centerY = (screen.Bounds.Top + screen.Bounds.Bottom) / 2;

int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
// Adjust the coordinates to account for the window size
int windowX = centerX - (int)(width / 2);
int windowY = centerY - (int)(height / 2);

MoveWindow(WindowInfo.Hwnd, windowX, windowY);
}
}

Expand Down

0 comments on commit 8956887

Please sign in to comment.