Skip to content

Commit 1eea1b6

Browse files
Improved tray icons for 175%+ scales
1 parent 968dfc1 commit 1eea1b6

File tree

7 files changed

+35
-228
lines changed

7 files changed

+35
-228
lines changed

CopyCopyDict.csproj

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@
4444
<Compile Include="KeyModifiers.cs" />
4545
<Compile Include="Program.cs" />
4646
<Compile Include="Properties\AssemblyInfo.cs" />
47-
<Compile Include="Resources\Resources.Designer.cs">
48-
<AutoGen>True</AutoGen>
49-
<DesignTime>True</DesignTime>
50-
<DependentUpon>Resources.resx</DependentUpon>
51-
</Compile>
52-
<EmbeddedResource Include="Resources\Resources.resx">
53-
<Generator>ResXFileCodeGenerator</Generator>
54-
<SubType>Designer</SubType>
55-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
56-
</EmbeddedResource>
5747
<None Include="LICENSE" />
5848
<None Include="Properties\Settings.settings">
5949
<Generator>SettingsSingleFileGenerator</Generator>
@@ -70,7 +60,7 @@
7060
<None Include="App.config" />
7161
</ItemGroup>
7262
<ItemGroup>
73-
<Content Include="Resources\Icon.ico" />
63+
<EmbeddedResource Include="Resources\Icon.ico" />
7464
</ItemGroup>
7565
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7666
</Project>

Program.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,9 @@ public static void Main()
3737
menu.Items.Add("-");
3838
menu.Items.Add("Exit", null, (e, a) => Application.Exit());
3939

40-
var icon = Resources.Resources.Icon;
41-
using (var g = Graphics.FromHwnd(IntPtr.Zero))
42-
{
43-
var desktop = g.GetHdc();
44-
var logicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
45-
var physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
46-
var dpiY = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);
47-
var scale = Math.Max(
48-
(double)physicalScreenHeight / logicalScreenHeight,
49-
(double)dpiY / 96);
50-
var iconSize = (int)Math.Round(16 * scale);
51-
if (Array.IndexOf(new[] { 16, 20, 24 }, iconSize) >= 0)
52-
{
53-
icon = new Icon(icon, iconSize, iconSize);
54-
}
55-
}
56-
5740
var trayIcon = new NotifyIcon
5841
{
59-
Icon = icon,
42+
Icon = GetTrayIcon(),
6043
ContextMenuStrip = menu,
6144
Visible = true
6245
};
@@ -100,6 +83,37 @@ private static void BrowseClipboardText()
10083
Browse(text);
10184
}
10285

86+
private static Icon GetTrayIcon()
87+
{
88+
using (var g = Graphics.FromHwnd(IntPtr.Zero))
89+
{
90+
var desktop = g.GetHdc();
91+
var logicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
92+
var physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
93+
var dpiY = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);
94+
var scale = Math.Max(
95+
(double)physicalScreenHeight / logicalScreenHeight,
96+
(double)dpiY / 96);
97+
var iconSize = (int)Math.Round(16 * scale);
98+
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
99+
var iconStream = assembly.GetManifestResourceStream("CopyCopyDict.Resources.Icon.ico");
100+
if (Array.IndexOf(new[] { 16, 20, 24 }, iconSize) < 0)
101+
{
102+
try
103+
{
104+
var bitmap = new Bitmap(iconStream);
105+
return Icon.FromHandle(bitmap.GetHicon());
106+
}
107+
catch
108+
{
109+
}
110+
111+
}
112+
iconStream.Position = 0;
113+
return new Icon(iconStream, iconSize, iconSize);
114+
}
115+
}
116+
103117
[DllImport("gdi32.dll")]
104118
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
105119
public enum DeviceCap

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.1.1.0")]
35-
[assembly: AssemblyFileVersion("1.1.1.0")]
34+
[assembly: AssemblyVersion("1.1.2.0")]
35+
[assembly: AssemblyFileVersion("1.1.2.0")]

Resources/Icon.ico

-2.83 KB
Binary file not shown.

Resources/Resources.Designer.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

Resources/Resources.resx

Lines changed: 0 additions & 124 deletions
This file was deleted.

bin/Release/CopyCopyDict.exe

-7 KB
Binary file not shown.

0 commit comments

Comments
 (0)