From e7262df5f0448c41ae6ee35027a14064b1f5b728 Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 8 Sep 2020 20:16:19 +0300 Subject: [PATCH 1/5] Added menu icon size option to region capture --- .../RegionCaptureOptions.cs | 1 + .../Shapes/ShapeManagerMenu.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs b/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs index 88ff7b763..90d76d453 100644 --- a/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs +++ b/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs @@ -71,6 +71,7 @@ public class RegionCaptureOptions public bool IsFixedSize = false; public Size FixedSize = new Size(250, 250); public bool ShowFPS = false; + public int MenuIconSize = 16; 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 afe895a7e..54a7b1e36 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs @@ -81,6 +81,8 @@ internal void CreateToolbar() menuForm.SuspendLayout(); + int imageScalingSize = Options.MenuIconSize.Clamp(16, 64); + tsMain = new ToolStripEx() { AutoSize = true, @@ -88,6 +90,7 @@ internal void CreateToolbar() ClickThrough = true, Dock = DockStyle.None, GripStyle = ToolStripGripStyle.Hidden, + ImageScalingSize = new Size(imageScalingSize, imageScalingSize), Location = new Point(0, 0), MinimumSize = new Size(10, 30), Padding = Form.IsEditorMode ? new Padding(5, 1, 3, 0) : new Padding(0, 1, 0, 0), @@ -976,6 +979,24 @@ internal void CreateToolbar() }; tsddbOptions.DropDownItems.Add(tsmiSwitchToSelectionToolAfterDrawing); + // TODO: Translate + ToolStripLabeledNumericUpDown tslnudMenuIconSize = new ToolStripLabeledNumericUpDown("Menu icon size:"); + tslnudMenuIconSize.Content.Minimum = 16; + tslnudMenuIconSize.Content.Maximum = 64; + tslnudMenuIconSize.Content.Increment = 16; + tslnudMenuIconSize.Content.Value = Options.MenuIconSize; + tslnudMenuIconSize.Content.ValueChanged = (sender, e) => + { + Options.MenuIconSize = (int)tslnudMenuIconSize.Content.Value; + tsMain.SuspendLayout(); + tsMain.AutoSize = false; + tsMain.ImageScalingSize = new Size(Options.MenuIconSize, Options.MenuIconSize); + tsMain.AutoSize = true; + tsMain.ResumeLayout(); + UpdateMenuPosition(); + }; + tsddbOptions.DropDownItems.Add(tslnudMenuIconSize); + if (!Form.IsEditorMode) { ToolStripMenuItem tsmiRememberMenuState = new ToolStripMenuItem(Resources.ShapeManager_CreateContextMenu_RememberMenuState); From d72c4751e733f120c633b6ecd7f6ee1b3b80d7bc Mon Sep 17 00:00:00 2001 From: Michael Delpach Date: Wed, 9 Sep 2020 08:12:50 +0800 Subject: [PATCH 2/5] Autoscale Region Capture toolbar based on DPI --- ShareX.HelpersLib/Native/NativeMethods.cs | 9 +++++++++ ShareX.HelpersLib/Native/NativeMethods_Helpers.cs | 11 +++++++++++ ShareX.ScreenCaptureLib/RegionCaptureOptions.cs | 2 +- ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 5a4040881..275af1be1 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -356,6 +356,15 @@ public static extern bool CreateProcess(string lpApplicationName, string lpComma [DllImport("gdi32.dll")] public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); + [DllImport("gdi32.dll")] + static extern int GetDeviceCaps(IntPtr hdc, int nIndex); + public enum DeviceCap + { + VERTRES = 10, + DESKTOPVERTRES = 117, + LOGPIXELSY = 90 + } + #endregion gdi32.dll #region gdiplus.dll diff --git a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs index 9af6f6c61..87346ebe4 100644 --- a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs +++ b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs @@ -568,5 +568,16 @@ 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); + } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs b/ShareX.ScreenCaptureLib/RegionCaptureOptions.cs index 90d76d453..5a2ef80e4 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 = 16; + public int MenuIconSize = (int)(16 * NativeMethods.GetScreenScalingFactor()); 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..8c33218d9 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs @@ -981,7 +981,7 @@ internal void CreateToolbar() // TODO: Translate ToolStripLabeledNumericUpDown tslnudMenuIconSize = new ToolStripLabeledNumericUpDown("Menu icon size:"); - tslnudMenuIconSize.Content.Minimum = 16; + tslnudMenuIconSize.Content.Minimum = (int)(16 * NativeMethods.GetScreenScalingFactor()); tslnudMenuIconSize.Content.Maximum = 64; tslnudMenuIconSize.Content.Increment = 16; tslnudMenuIconSize.Content.Value = Options.MenuIconSize; From 27b398230b2e08c925ed7677cfe2baa5476f438a Mon Sep 17 00:00:00 2001 From: Michael Delpach Date: Wed, 9 Sep 2020 08:39:58 +0800 Subject: [PATCH 3/5] Update ShapeManagerMenu.cs --- ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs index 8c33218d9..07b6f66b3 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs @@ -81,7 +81,7 @@ internal void CreateToolbar() menuForm.SuspendLayout(); - int imageScalingSize = Options.MenuIconSize.Clamp(16, 64); + int imageScalingSize = Options.MenuIconSize.Clamp((int)(16 * NativeMethods.GetScreenScalingFactor()), 64); tsMain = new ToolStripEx() { From ce35af243ccf0d147ad6fc9f2e33dbf548254f93 Mon Sep 17 00:00:00 2001 From: Michael Delpach Date: Thu, 10 Sep 2020 07:36:55 +0800 Subject: [PATCH 4/5] Update ShapeManagerMenu.cs --- ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs index 07b6f66b3..54a7b1e36 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManagerMenu.cs @@ -81,7 +81,7 @@ internal void CreateToolbar() menuForm.SuspendLayout(); - int imageScalingSize = Options.MenuIconSize.Clamp((int)(16 * NativeMethods.GetScreenScalingFactor()), 64); + int imageScalingSize = Options.MenuIconSize.Clamp(16, 64); tsMain = new ToolStripEx() { @@ -981,7 +981,7 @@ internal void CreateToolbar() // TODO: Translate ToolStripLabeledNumericUpDown tslnudMenuIconSize = new ToolStripLabeledNumericUpDown("Menu icon size:"); - tslnudMenuIconSize.Content.Minimum = (int)(16 * NativeMethods.GetScreenScalingFactor()); + tslnudMenuIconSize.Content.Minimum = 16; tslnudMenuIconSize.Content.Maximum = 64; tslnudMenuIconSize.Content.Increment = 16; tslnudMenuIconSize.Content.Value = Options.MenuIconSize; From a19439ef333e1a4c0b9f08cb7ccbf24527239b6c Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 10 Sep 2020 13:18:00 +0300 Subject: [PATCH 5/5] 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 extern bool CreateProcess(string lpApplicationName, string lpComma 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()