fixed a bug of window
This commit is contained in:
1 parent
b84db8512f
commit
2c0573518b
38 files changed
+540
-592
No files matched your search
@@ -0,0 +1,9 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace HandyControl.Tools.Extension
|
||||
{
|
||||
public static class ValueExtension
|
||||
{
|
||||
public static Thickness Add(this Thickness a, Thickness b) => new Thickness(a.Left + b.Left, a.Top + b.Top, a.Right + b.Right, a.Bottom + b.Bottom);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
internal class DpiHelper
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static Matrix _transformToDip;
|
||||
|
||||
public static Point DevicePixelsToLogical(Point devicePoint, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
_transformToDip = Matrix.Identity;
|
||||
_transformToDip.Scale(1d / dpiScaleX, 1d / dpiScaleY);
|
||||
return _transformToDip.Transform(devicePoint);
|
||||
}
|
||||
|
||||
public static Size DeviceSizeToLogical(Size deviceSize, double dpiScaleX, double dpiScaleY)
|
||||
{
|
||||
Point pt = DevicePixelsToLogical(new Point(deviceSize.Width, deviceSize.Height), dpiScaleX, dpiScaleY);
|
||||
|
||||
return new Size(pt.X, pt.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,13 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Tools.Interop;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
@@ -50,5 +57,131 @@ namespace HandyControl.Tools
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Window GetActiveWindow() => Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
|
||||
|
||||
private static readonly BitArray _cacheValid = new BitArray((int)CacheSlot.NumSlots);
|
||||
|
||||
private static bool _setDpiX = true;
|
||||
|
||||
private static bool _dpiInitialized;
|
||||
|
||||
private static readonly object _dpiLock = new object();
|
||||
|
||||
private static int _dpi;
|
||||
|
||||
internal static int Dpi
|
||||
{
|
||||
[SecurityCritical, SecuritySafeCritical]
|
||||
get
|
||||
{
|
||||
if (!_dpiInitialized)
|
||||
{
|
||||
lock (_dpiLock)
|
||||
{
|
||||
if (!_dpiInitialized)
|
||||
{
|
||||
var desktopWnd = new HandleRef(null, IntPtr.Zero);
|
||||
|
||||
// Win32Exception will get the Win32 error code so we don't have to
|
||||
var dc = UnsafeNativeMethods.GetDC(desktopWnd);
|
||||
|
||||
// Detecting error case from unmanaged call, required by PREsharp to throw a Win32Exception
|
||||
if (dc == IntPtr.Zero)
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_dpi = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dc), NativeMethods.LOGPIXELSY);
|
||||
_dpiInitialized = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
UnsafeNativeMethods.ReleaseDC(desktopWnd, new HandleRef(null, dc));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _dpi;
|
||||
}
|
||||
}
|
||||
|
||||
private static int _dpiX;
|
||||
|
||||
internal static int DpiX
|
||||
{
|
||||
[SecurityCritical, SecuritySafeCritical]
|
||||
get
|
||||
{
|
||||
if (_setDpiX)
|
||||
{
|
||||
lock (_cacheValid)
|
||||
{
|
||||
if (_setDpiX)
|
||||
{
|
||||
_setDpiX = false;
|
||||
var desktopWnd = new HandleRef(null, IntPtr.Zero);
|
||||
var dc = UnsafeNativeMethods.GetDC(desktopWnd);
|
||||
if (dc == IntPtr.Zero)
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
try
|
||||
{
|
||||
_dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dc), NativeMethods.LOGPIXELSX);
|
||||
_cacheValid[(int)CacheSlot.DpiX] = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
UnsafeNativeMethods.ReleaseDC(desktopWnd, new HandleRef(null, dc));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _dpiX;
|
||||
}
|
||||
}
|
||||
|
||||
private static Thickness _windowResizeBorderThickness;
|
||||
|
||||
internal static Thickness WindowResizeBorderThickness
|
||||
{
|
||||
[SecurityCritical]
|
||||
get
|
||||
{
|
||||
lock (_cacheValid)
|
||||
{
|
||||
while (!_cacheValid[(int)CacheSlot.WindowResizeBorderThickness])
|
||||
{
|
||||
_cacheValid[(int)CacheSlot.WindowResizeBorderThickness] = true;
|
||||
|
||||
var frameSize = new Size(NativeMethods.GetSystemMetrics(SM.CXSIZEFRAME), NativeMethods.GetSystemMetrics(SM.CYSIZEFRAME));
|
||||
var frameSizeInDips = DpiHelper.DeviceSizeToLogical(frameSize, DpiX / 96.0, Dpi / 96.0);
|
||||
|
||||
_windowResizeBorderThickness = new Thickness(frameSizeInDips.Width, frameSizeInDips.Height, frameSizeInDips.Width, frameSizeInDips.Height);
|
||||
}
|
||||
}
|
||||
|
||||
return _windowResizeBorderThickness;
|
||||
}
|
||||
}
|
||||
|
||||
public static Thickness WindowMaximizedPadding
|
||||
{
|
||||
get
|
||||
{
|
||||
#if netle40
|
||||
return WindowResizeBorderThickness;
|
||||
#elif Core
|
||||
var hdc = UnsafeNativeMethods.GetDC(IntPtr.Zero);
|
||||
var scale = UnsafeNativeMethods.GetDeviceCaps(hdc, NativeMethods.DESKTOPVERTRES) / (float)UnsafeNativeMethods.GetDeviceCaps(hdc, NativeMethods.VERTRES);
|
||||
UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, hdc);
|
||||
return WindowResizeBorderThickness.Add(new Thickness(4 * scale));
|
||||
#else
|
||||
return WindowResizeBorderThickness.Add(new Thickness(4));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security;
|
||||
|
||||
namespace HandyControl.Tools.Interop
|
||||
{
|
||||
@@ -55,7 +56,15 @@ namespace HandyControl.Tools.Interop
|
||||
MEM_COMMIT = 0x1000,
|
||||
MEM_RELEASE = 0x8000,
|
||||
PAGE_READWRITE = 0x04,
|
||||
TBSTATE_HIDDEN = 0x08;
|
||||
TBSTATE_HIDDEN = 0x08,
|
||||
VERTRES = 10,
|
||||
HORZRES = 8,
|
||||
DESKTOPVERTRES = 117,
|
||||
DESKTOPHORZRES = 118,
|
||||
LOGPIXELSX = 88,
|
||||
LOGPIXELSY = 90,
|
||||
CXFRAME = 32,
|
||||
CXSIZEFRAME = CXFRAME;
|
||||
|
||||
[Flags]
|
||||
public enum ProcessAccess
|
||||
@@ -205,5 +214,10 @@ namespace HandyControl.Tools.Interop
|
||||
|
||||
[DllImport(ExternDll.User32, CharSet = CharSet.Auto)]
|
||||
public static extern bool GetCursorPos(out POINT pt);
|
||||
|
||||
[SecurityCritical]
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
[DllImport(ExternDll.User32)]
|
||||
public static extern int GetSystemMetrics(SM nIndex);
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,142 @@ namespace HandyControl.Tools.Interop
|
||||
TABLETPC = 86,
|
||||
MEDIACENTER = 87,
|
||||
REMOTESESSION = 0x1000,
|
||||
REMOTECONTROL = 0x2001,
|
||||
REMOTECONTROL = 0x2001
|
||||
}
|
||||
|
||||
internal enum CacheSlot
|
||||
{
|
||||
DpiX,
|
||||
|
||||
FocusBorderWidth,
|
||||
FocusBorderHeight,
|
||||
HighContrast,
|
||||
MouseVanish,
|
||||
|
||||
DropShadow,
|
||||
FlatMenu,
|
||||
WorkAreaInternal,
|
||||
WorkArea,
|
||||
|
||||
IconMetrics,
|
||||
|
||||
KeyboardCues,
|
||||
KeyboardDelay,
|
||||
KeyboardPreference,
|
||||
KeyboardSpeed,
|
||||
SnapToDefaultButton,
|
||||
WheelScrollLines,
|
||||
MouseHoverTime,
|
||||
MouseHoverHeight,
|
||||
MouseHoverWidth,
|
||||
|
||||
MenuDropAlignment,
|
||||
MenuFade,
|
||||
MenuShowDelay,
|
||||
|
||||
ComboBoxAnimation,
|
||||
ClientAreaAnimation,
|
||||
CursorShadow,
|
||||
GradientCaptions,
|
||||
HotTracking,
|
||||
ListBoxSmoothScrolling,
|
||||
MenuAnimation,
|
||||
SelectionFade,
|
||||
StylusHotTracking,
|
||||
ToolTipAnimation,
|
||||
ToolTipFade,
|
||||
UIEffects,
|
||||
|
||||
MinimizeAnimation,
|
||||
Border,
|
||||
CaretWidth,
|
||||
ForegroundFlashCount,
|
||||
DragFullWindows,
|
||||
NonClientMetrics,
|
||||
|
||||
ThinHorizontalBorderHeight,
|
||||
ThinVerticalBorderWidth,
|
||||
CursorWidth,
|
||||
CursorHeight,
|
||||
ThickHorizontalBorderHeight,
|
||||
ThickVerticalBorderWidth,
|
||||
MinimumHorizontalDragDistance,
|
||||
MinimumVerticalDragDistance,
|
||||
FixedFrameHorizontalBorderHeight,
|
||||
FixedFrameVerticalBorderWidth,
|
||||
FocusHorizontalBorderHeight,
|
||||
FocusVerticalBorderWidth,
|
||||
FullPrimaryScreenWidth,
|
||||
FullPrimaryScreenHeight,
|
||||
HorizontalScrollBarButtonWidth,
|
||||
HorizontalScrollBarHeight,
|
||||
HorizontalScrollBarThumbWidth,
|
||||
IconWidth,
|
||||
IconHeight,
|
||||
IconGridWidth,
|
||||
IconGridHeight,
|
||||
MaximizedPrimaryScreenWidth,
|
||||
MaximizedPrimaryScreenHeight,
|
||||
MaximumWindowTrackWidth,
|
||||
MaximumWindowTrackHeight,
|
||||
MenuCheckmarkWidth,
|
||||
MenuCheckmarkHeight,
|
||||
MenuButtonWidth,
|
||||
MenuButtonHeight,
|
||||
MinimumWindowWidth,
|
||||
MinimumWindowHeight,
|
||||
MinimizedWindowWidth,
|
||||
MinimizedWindowHeight,
|
||||
MinimizedGridWidth,
|
||||
MinimizedGridHeight,
|
||||
MinimumWindowTrackWidth,
|
||||
MinimumWindowTrackHeight,
|
||||
PrimaryScreenWidth,
|
||||
PrimaryScreenHeight,
|
||||
WindowCaptionButtonWidth,
|
||||
WindowCaptionButtonHeight,
|
||||
ResizeFrameHorizontalBorderHeight,
|
||||
ResizeFrameVerticalBorderWidth,
|
||||
SmallIconWidth,
|
||||
SmallIconHeight,
|
||||
SmallWindowCaptionButtonWidth,
|
||||
SmallWindowCaptionButtonHeight,
|
||||
VirtualScreenWidth,
|
||||
VirtualScreenHeight,
|
||||
VerticalScrollBarWidth,
|
||||
VerticalScrollBarButtonHeight,
|
||||
WindowCaptionHeight,
|
||||
KanjiWindowHeight,
|
||||
MenuBarHeight,
|
||||
VerticalScrollBarThumbHeight,
|
||||
IsImmEnabled,
|
||||
IsMediaCenter,
|
||||
IsMenuDropRightAligned,
|
||||
IsMiddleEastEnabled,
|
||||
IsMousePresent,
|
||||
IsMouseWheelPresent,
|
||||
IsPenWindows,
|
||||
IsRemotelyControlled,
|
||||
IsRemoteSession,
|
||||
ShowSounds,
|
||||
IsSlowMachine,
|
||||
SwapButtons,
|
||||
IsTabletPC,
|
||||
VirtualScreenLeft,
|
||||
VirtualScreenTop,
|
||||
|
||||
PowerLineStatus,
|
||||
|
||||
IsGlassEnabled,
|
||||
UxThemeName,
|
||||
UxThemeColor,
|
||||
WindowCornerRadius,
|
||||
WindowGlassColor,
|
||||
WindowGlassBrush,
|
||||
WindowNonClientFrameThickness,
|
||||
WindowResizeBorderThickness,
|
||||
|
||||
NumSlots
|
||||
}
|
||||
|
||||
internal static class Win32Constant
|
||||
|
||||
@@ -215,5 +215,11 @@ namespace HandyControl.Tools.Interop
|
||||
|
||||
[DllImport(ExternDll.User32, SetLastError = true)]
|
||||
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
|
||||
|
||||
[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
|
||||
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
|
||||
|
||||
[DllImport(ExternDll.User32, CharSet = CharSet.Auto)]
|
||||
public static extern IntPtr GetDC(IntPtr ptr);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user