From a19439ef333e1a4c0b9f08cb7ccbf24527239b6c Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 10 Sep 2020 13:18:00 +0300 Subject: [PATCH] Code refactor --- ShareX.HelpersLib/Native/NativeEnums.cs | 167 +++++++++++++++++- ShareX.HelpersLib/Native/NativeMethods.cs | 10 +- .../Native/NativeMethods_Helpers.cs | 24 ++- .../RegionCaptureOptions.cs | 2 +- .../Shapes/ShapeManagerMenu.cs | 5 + 5 files changed, 190 insertions(+), 18 deletions(-) diff --git a/ShareX.HelpersLib/Native/NativeEnums.cs b/ShareX.HelpersLib/Native/NativeEnums.cs index 174004be8..ff32c91b4 100644 --- a/ShareX.HelpersLib/Native/NativeEnums.cs +++ b/ShareX.HelpersLib/Native/NativeEnums.cs @@ -3224,7 +3224,7 @@ public enum EndSessionReasons : uint } [Flags] - enum SHGFI : uint + public enum SHGFI : uint { /// get icon Icon = 0x000000100, @@ -3263,4 +3263,169 @@ enum SHGFI : uint /// Get the index of the overlay in the upper 8 bits of the iIcon OverlayIndex = 0x000000040 } + + public enum DeviceCap + { + /// + /// Device driver version + /// + DRIVERVERSION = 0, + /// + /// Device classification + /// + TECHNOLOGY = 2, + /// + /// Horizontal size in millimeters + /// + HORZSIZE = 4, + /// + /// Vertical size in millimeters + /// + VERTSIZE = 6, + /// + /// Horizontal width in pixels + /// + HORZRES = 8, + /// + /// Vertical height in pixels + /// + VERTRES = 10, + /// + /// Number of bits per pixel + /// + BITSPIXEL = 12, + /// + /// Number of planes + /// + PLANES = 14, + /// + /// Number of brushes the device has + /// + NUMBRUSHES = 16, + /// + /// Number of pens the device has + /// + NUMPENS = 18, + /// + /// Number of markers the device has + /// + NUMMARKERS = 20, + /// + /// Number of fonts the device has + /// + NUMFONTS = 22, + /// + /// Number of colors the device supports + /// + NUMCOLORS = 24, + /// + /// Size required for device descriptor + /// + PDEVICESIZE = 26, + /// + /// Curve capabilities + /// + CURVECAPS = 28, + /// + /// Line capabilities + /// + LINECAPS = 30, + /// + /// Polygonal capabilities + /// + POLYGONALCAPS = 32, + /// + /// Text capabilities + /// + TEXTCAPS = 34, + /// + /// Clipping capabilities + /// + CLIPCAPS = 36, + /// + /// Bitblt capabilities + /// + RASTERCAPS = 38, + /// + /// Length of the X leg + /// + ASPECTX = 40, + /// + /// Length of the Y leg + /// + ASPECTY = 42, + /// + /// Length of the hypotenuse + /// + ASPECTXY = 44, + /// + /// Shading and Blending caps + /// + SHADEBLENDCAPS = 45, + + /// + /// Logical pixels inch in X + /// + LOGPIXELSX = 88, + /// + /// Logical pixels inch in Y + /// + LOGPIXELSY = 90, + + /// + /// Number of entries in physical palette + /// + SIZEPALETTE = 104, + /// + /// Number of reserved entries in palette + /// + NUMRESERVED = 106, + /// + /// Actual color resolution + /// + COLORRES = 108, + + // Printing related DeviceCaps. These replace the appropriate Escapes + /// + /// Physical Width in device units + /// + PHYSICALWIDTH = 110, + /// + /// Physical Height in device units + /// + PHYSICALHEIGHT = 111, + /// + /// Physical Printable Area x margin + /// + PHYSICALOFFSETX = 112, + /// + /// Physical Printable Area y margin + /// + PHYSICALOFFSETY = 113, + /// + /// Scaling factor x + /// + SCALINGFACTORX = 114, + /// + /// Scaling factor y + /// + SCALINGFACTORY = 115, + + /// + /// Current vertical refresh rate of the display device (for displays only) in Hz + /// + VREFRESH = 116, + /// + /// Vertical height of entire desktop in pixels + /// + DESKTOPVERTRES = 117, + /// + /// Horizontal width of entire desktop in pixels + /// + DESKTOPHORZRES = 118, + /// + /// Preferred blt alignment + /// + BLTALIGNMENT = 119 + } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 275af1be1..f1c231ebf 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -354,16 +354,10 @@ public static partial class NativeMethods public static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFOHEADER pbmi, uint pila, out IntPtr ppvBits, IntPtr hSection, uint dwOffset); [DllImport("gdi32.dll")] - public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); + public static extern int GetDeviceCaps(IntPtr hdc, int nIndex); [DllImport("gdi32.dll")] - static extern int GetDeviceCaps(IntPtr hdc, int nIndex); - public enum DeviceCap - { - VERTRES = 10, - DESKTOPVERTRES = 117, - LOGPIXELSY = 90 - } + public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); #endregion gdi32.dll diff --git a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs index 87346ebe4..8295172fc 100644 --- a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs +++ b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs @@ -568,16 +568,24 @@ public static Icon GetJumboFileIcon(string filePath, bool jumboSize = true) DestroyIcon(hIcon); return icon; } + public static float GetScreenScalingFactor() { - Graphics g = Graphics.FromHwnd(IntPtr.Zero); - IntPtr desktop = g.GetHdc(); - int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES); - int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); - int logpixelsy = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY); - float screenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; - float dpiScalingFactor = (float)logpixelsy / (float)96; - return Math.Max(screenScalingFactor, dpiScalingFactor); + float scalingFactor; + + using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) + { + IntPtr desktop = g.GetHdc(); + int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES); + int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); + int logpixelsy = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY); + float screenScalingFactor = (float)PhysicalScreenHeight / LogicalScreenHeight; + float dpiScalingFactor = logpixelsy / 96f; + scalingFactor = Math.Max(screenScalingFactor, dpiScalingFactor); + g.ReleaseHdc(desktop); + } + + return scalingFactor; } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs b/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs index 5a2ef80e4..ade24fc79 100644 --- a/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs +++ b/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs @@ -71,7 +71,7 @@ public class RegionCaptureOptions public bool IsFixedSize = false; public Size FixedSize = new Size(250, 250); public bool ShowFPS = false; - public int MenuIconSize = (int)(16 * NativeMethods.GetScreenScalingFactor()); + public int MenuIconSize = 0; public bool RememberMenuState = false; public bool MenuCollapsed = false; public Point MenuPosition = Point.Empty; diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs index 54a7b1e36..c74103e82 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs @@ -81,6 +81,11 @@ internal void CreateToolbar() menuForm.SuspendLayout(); + if (Options.MenuIconSize == 0) + { + Options.MenuIconSize = (int)(16 * NativeMethods.GetScreenScalingFactor()); + } + int imageScalingSize = Options.MenuIconSize.Clamp(16, 64); tsMain = new ToolStripEx()