Skip to content

Commit

Permalink
Fix hide border window size problem
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-akira committed Oct 18, 2023
1 parent c5ea76d commit 2bf43cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
35 changes: 22 additions & 13 deletions Assets/Scripts/ControlWPFWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,26 +1182,35 @@ private void SetBackgroundTransparent()
}

private bool lastHideWindowBorder = false;
private int? windowBorderWidth = null;
private int? windowBorderHeight = null;

void HideWindowBorder(bool enable)
{
if (lastHideWindowBorder == enable) return;
lastHideWindowBorder = enable;
Settings.Current.HideBorder = enable;
#if !UNITY_EDITOR // エディタ上では動きません。
var hwnd = GetUnityWindowHandle();
//var hwnd = GetActiveWindow();
if (enable)
{
var hwnd = GetUnityWindowHandle();
var clientrect = GetUnityWindowClientPosition();
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); //ウインドウ枠の削除
SetUnityWindowSize(clientrect.right - clientrect.left, clientrect.bottom - clientrect.top);
}
else
{
var windowrect = GetUnityWindowPosition();
SetWindowLong(hwnd, GWL_STYLE, defaultWindowStyle);
Screen.SetResolution(windowrect.right - windowrect.left, windowrect.bottom - windowrect.top, false);
}
if (windowBorderWidth.HasValue == false)
{
var windowrect = GetUnityWindowPosition();
windowBorderWidth = windowrect.width - clientrect.width;
windowBorderHeight = windowrect.height - clientrect.height;
}
if (enable)
{
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); //ウインドウ枠の削除
SetUnityWindowFrameChanged();
WaitOneFrameAction(() => SetUnityWindowSize(clientrect.width, clientrect.height));
}
else
{
SetWindowLong(hwnd, GWL_STYLE, defaultWindowStyle);
SetUnityWindowFrameChanged();
WaitOneFrameAction(() => SetUnityWindowSize(clientrect.width + windowBorderWidth.Value, clientrect.height + windowBorderHeight.Value));
}
#endif
}
void SetWindowTopMost(bool enable)
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/Utils/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public struct RECT
public int top;
public int right;
public int bottom;

public int width => right - left;
public int height => bottom - top;
}

[DllImport("user32.dll")]
Expand Down Expand Up @@ -104,6 +107,7 @@ public enum SetWindowPosFlags : uint
public static RECT GetUnityWindowClientPosition() { RECT r; GetClientRect(GetUnityWindowHandle(), out r); return r; }
public static void SetUnityWindowPosition(int x, int y) => SetWindowPos(GetUnityWindowHandle(), IntPtr.Zero, x, y, 0, 0, SetWindowPosFlags.IgnoreResize);
public static void SetUnityWindowSize(int width, int height) => SetWindowPos(GetUnityWindowHandle(), IntPtr.Zero, 0, 0, width, height, SetWindowPosFlags.IgnoreMove);
public static void SetUnityWindowFrameChanged() => SetWindowPos(GetUnityWindowHandle(), IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.IgnoreMoveAndResize | SetWindowPosFlags.IgnoreZOrder | SetWindowPosFlags.FrameChanged | SetWindowPosFlags.ShowWindow);
public static void SetUnityWindowTopMost(bool enable) => SetWindowPos(GetUnityWindowHandle(), enable ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.IgnoreMoveAndResize);
public static void SetUnityWindowTitle(string title) => SetWindowText(GetUnityWindowHandle(), title);

Expand Down

0 comments on commit 2bf43cd

Please sign in to comment.