11using System ;
2- using System . Runtime . InteropServices ;
32using System . Threading ;
43
54namespace FpsManager
65{
76 /// <summary>
8- /// A simple monitor checker that sets the main screen to the desired hz if there is a second monitor connected or not.
7+ /// Simple monitor checker that sets the main screen to the desired hz if there is a second monitor connected or not.
98 /// </summary>
109 internal static class Program
1110 {
12- private const int ENUM_CURRENT_SETTINGS = - 1 ;
13- private const int CDS_UPDATEREGISTRY = 0x01 ;
14- private const int DISP_CHANGE_SUCCESSFUL = 0 ;
11+ private const int DETECT_FOR_SCREEN_MINUTES = 3 ;
12+ private const int DETECT_SPEED = 10000 ;
13+ private const int READ_TIME = 5000 ;
1514
16- [ StructLayout ( LayoutKind . Sequential ) ]
17- public struct DevMode
15+ private static void Main ( )
1816 {
19- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 32 ) ]
20- public string dmDeviceName ;
21- public short dmSpecVersion ;
22- public short dmDriverVersion ;
23- public short dmSize ;
24- public short dmDriverExtra ;
25- public int dmFields ;
26- public int dmPositionX ;
27- public int dmPositionY ;
28- public int dmDisplayOrientation ;
29- public int dmDisplayFixedOutput ;
30- public short dmColor ;
31- public short dmDuplex ;
32- public short dmYResolution ;
33- public short dmTTOption ;
34- public short dmCollate ;
35- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 32 ) ]
36- public string dmFormName ;
37- public short dmLogPixels ;
38- public short dmBitsPerPel ;
39- public int dmPelsWidth ;
40- public int dmPelsHeight ;
41- public int dmDisplayFlags ;
42- public int dmDisplayFrequency ;
43- public int dmICMMethod ;
44- public int dmICMIntent ;
45- public int dmMediaType ;
46- public int dmDitherType ;
47- public int dmReserved1 ;
48- public int dmReserved2 ;
49- public int dmPanningWidth ;
50- public int dmPanningHeight ;
51- }
52-
53- [ DllImport ( "user32.dll" ) ]
54- public static extern bool EnumDisplayDevices ( string lpDevice , uint iDevNum , ref DisplayDevice lpDisplayDevice , uint dwFlags ) ;
55-
56- [ StructLayout ( LayoutKind . Sequential , CharSet = CharSet . Ansi ) ]
57- public struct DisplayDevice
58- {
59- [ MarshalAs ( UnmanagedType . U4 ) ]
60- public int cb ;
61- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 32 ) ]
62- public string DeviceName ;
63- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 128 ) ]
64- public string DeviceString ;
65- [ MarshalAs ( UnmanagedType . U4 ) ]
66- public int StateFlags ;
67- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 128 ) ]
68- public string DeviceID ;
69- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 128 ) ]
70- public string DeviceKey ;
71- }
72-
73- [ DllImport ( "user32.dll" ) ]
74- public static extern bool EnumDisplaySettings ( string lpszDeviceName , int iModeNum , ref DevMode lpDevMode ) ;
75-
76- [ DllImport ( "user32.dll" ) ]
77- public static extern int ChangeDisplaySettingsEx ( string lpszDeviceName , ref DevMode lpDevMode , IntPtr hwnd , uint dwflags , IntPtr lParam ) ;
78-
79- private static void Main ( string [ ] args )
80- {
81- int monitorCount = GetMonitorCount ( ) ;
82-
17+ HzData hz = Saving . GetRefreshRates ( ) ;
18+ int monitorCount = Screen . GetMonitorCount ( ) ;
19+
8320 if ( monitorCount > 1 )
8421 {
85- Console . WriteLine ( "Second monitor detected. Setting refresh rate to 60Hz." ) ;
86- SetRefreshRate ( 60 ) ;
22+ Screen . SetRefreshRate ( hz . StandaloneHz ) ;
23+ SendMessage ( $ "Second monitor detected. Setting refresh rate to { hz . StandaloneHz } Hz." ) ;
8724 return ;
8825 }
89-
90- Console . WriteLine ( "No second monitor detected. Setting refresh rate to dynamic ." ) ;
91- SetRefreshRate ( 120 ) ;
92- DateTime endTime = DateTime . Now . AddMinutes ( 3 ) ;
26+
27+ Console . WriteLine ( $ "No second monitor detected. Setting refresh rate to preferred hz: { hz . LowestHz } .") ;
28+ Screen . SetRefreshRate ( hz . LowestHz ) ;
29+ DateTime endTime = DateTime . Now . AddMinutes ( DETECT_FOR_SCREEN_MINUTES ) ;
9330
9431 while ( DateTime . Now < endTime )
9532 {
96- monitorCount = GetMonitorCount ( ) ;
33+ monitorCount = Screen . GetMonitorCount ( ) ;
9734
9835 if ( monitorCount > 1 )
9936 {
100- Console . WriteLine ( "Second monitor detected within waiting period. Setting refresh rate to 60Hz." ) ;
101- SetRefreshRate ( 60 ) ;
37+ Screen . SetRefreshRate ( hz . LowestHz ) ;
38+ SendMessage ( $ "Second monitor detected within waiting period. Setting refresh rate to { hz . LowestHz } Hz." ) ;
10239 return ;
10340 }
10441
105- Thread . Sleep ( 10000 ) ;
106- }
107- }
108-
109- private static int GetMonitorCount ( )
110- {
111- int monitorCount = 0 ;
112- DisplayDevice d = new DisplayDevice ( ) ;
113- d . cb = Marshal . SizeOf ( d ) ;
114-
115- for ( uint id = 0 ; EnumDisplayDevices ( null , id , ref d , 0 ) ; id ++ )
116- {
117- if ( ( d . StateFlags & 0x1 ) != 0 )
118- monitorCount ++ ;
42+ Thread . Sleep ( DETECT_SPEED ) ;
11943 }
120-
121- return monitorCount ;
12244 }
12345
124- private static void SetRefreshRate ( int hz )
46+ private static void SendMessage ( string msg )
12547 {
126- DevMode dm = new DevMode ( ) ;
127- dm . dmSize = ( short ) Marshal . SizeOf ( dm ) ;
128-
129- if ( ! EnumDisplaySettings ( null , ENUM_CURRENT_SETTINGS , ref dm ) )
130- return ;
131-
132- dm . dmDisplayFrequency = hz ;
133- int iRet = ChangeDisplaySettingsEx ( null , ref dm , IntPtr . Zero ,
134- CDS_UPDATEREGISTRY , IntPtr . Zero ) ;
135-
136- if ( iRet != DISP_CHANGE_SUCCESSFUL )
137- Console . WriteLine ( "Failed to change display settings." ) ;
48+ Console . Write ( msg ) ;
49+ Thread . Sleep ( READ_TIME ) ;
13850 }
13951 }
14052}
0 commit comments