@@ -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
0 commit comments