mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
278 lines
8.5 KiB
C#
278 lines
8.5 KiB
C#
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
|
|
public class NativeMethods
|
|
{
|
|
|
|
public const int KEYEVENTF_EXTENDEDKEY = 1;
|
|
public const int KEYEVENTF_KEYUP = 2;
|
|
|
|
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
|
|
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
|
|
public const int VK_MEDIA_PREV_TRACK = 0xB1;
|
|
public const int VK_VOLUME_MUTE = 0xAD;
|
|
public const int VK_SNAPSHOT = 0x2C;
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
|
|
|
|
public static void KeyPress(int key = VK_MEDIA_PLAY_PAUSE)
|
|
{
|
|
keybd_event((byte)key, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
|
|
}
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
|
|
public const int SW_RESTORE = 9;
|
|
|
|
public static bool SwitchToCurrent()
|
|
{
|
|
IntPtr hWnd = IntPtr.Zero;
|
|
Process process = Process.GetCurrentProcess();
|
|
Process[] processes = Process.GetProcessesByName(process.ProcessName);
|
|
foreach (Process _process in processes)
|
|
{
|
|
if (_process.Id != process.Id)
|
|
{
|
|
|
|
if (_process.MainWindowHandle != IntPtr.Zero)
|
|
{
|
|
Debug.WriteLine(_process.Id);
|
|
Debug.WriteLine(process.Id);
|
|
|
|
hWnd = _process.MainWindowHandle;
|
|
ShowWindowAsync(hWnd, SW_RESTORE);
|
|
}
|
|
|
|
return true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
|
|
int AcValueIndex);
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerWriteACValueIndex(IntPtr RootPowerKey,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
|
|
int AcValueIndex);
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerReadACValueIndex(IntPtr RootPowerKey,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
|
|
out IntPtr AcValueIndex
|
|
);
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerReadDCValueIndex(IntPtr RootPowerKey,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
|
|
out IntPtr AcValueIndex
|
|
);
|
|
|
|
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerSetActiveScheme(IntPtr RootPowerKey,
|
|
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid);
|
|
|
|
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
|
|
static extern UInt32 PowerGetActiveScheme(IntPtr UserPowerKey, out IntPtr ActivePolicyGuid);
|
|
|
|
static readonly Guid GUID_CPU = new Guid("54533251-82be-4824-96c1-47b60b740d00");
|
|
static readonly Guid GUID_BOOST = new Guid("be337238-0d82-4146-a960-4f3749d470c7");
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
public struct DEVMODE
|
|
{
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
|
public string dmDeviceName;
|
|
|
|
public short dmSpecVersion;
|
|
public short dmDriverVersion;
|
|
public short dmSize;
|
|
public short dmDriverExtra;
|
|
public int dmFields;
|
|
public int dmPositionX;
|
|
public int dmPositionY;
|
|
public int dmDisplayOrientation;
|
|
public int dmDisplayFixedOutput;
|
|
public short dmColor;
|
|
public short dmDuplex;
|
|
public short dmYResolution;
|
|
public short dmTTOption;
|
|
public short dmCollate;
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
|
public string dmFormName;
|
|
|
|
public short dmLogPixels;
|
|
public short dmBitsPerPel;
|
|
public int dmPelsWidth;
|
|
public int dmPelsHeight;
|
|
public int dmDisplayFlags;
|
|
public int dmDisplayFrequency;
|
|
public int dmICMMethod;
|
|
public int dmICMIntent;
|
|
public int dmMediaType;
|
|
public int dmDitherType;
|
|
public int dmReserved1;
|
|
public int dmReserved2;
|
|
public int dmPanningWidth;
|
|
public int dmPanningHeight;
|
|
};
|
|
|
|
[Flags()]
|
|
public enum DisplaySettingsFlags : int
|
|
{
|
|
CDS_UPDATEREGISTRY = 1,
|
|
CDS_TEST = 2,
|
|
CDS_FULLSCREEN = 4,
|
|
CDS_GLOBAL = 8,
|
|
CDS_SET_PRIMARY = 0x10,
|
|
CDS_RESET = 0x40000000,
|
|
CDS_NORESET = 0x10000000
|
|
}
|
|
|
|
// PInvoke declaration for EnumDisplaySettings Win32 API
|
|
[DllImport("user32.dll")]
|
|
public static extern int EnumDisplaySettingsEx(
|
|
string lpszDeviceName,
|
|
int iModeNum,
|
|
ref DEVMODE lpDevMode);
|
|
|
|
// PInvoke declaration for ChangeDisplaySettings Win32 API
|
|
[DllImport("user32.dll")]
|
|
public static extern int ChangeDisplaySettingsEx(
|
|
string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd,
|
|
DisplaySettingsFlags dwflags, IntPtr lParam);
|
|
|
|
|
|
public const int ENUM_CURRENT_SETTINGS = -1;
|
|
|
|
public const string laptopScreenName = "\\\\.\\DISPLAY1";
|
|
|
|
public static DEVMODE CreateDevmode()
|
|
{
|
|
DEVMODE dm = new DEVMODE();
|
|
dm.dmDeviceName = new String(new char[32]);
|
|
dm.dmFormName = new String(new char[32]);
|
|
dm.dmSize = (short)Marshal.SizeOf(dm);
|
|
return dm;
|
|
}
|
|
|
|
public static Screen FindLaptopScreen()
|
|
{
|
|
var screens = Screen.AllScreens;
|
|
Screen laptopScreen = null;
|
|
|
|
foreach (var screen in screens)
|
|
{
|
|
if (screen.DeviceName == laptopScreenName)
|
|
{
|
|
laptopScreen = screen;
|
|
}
|
|
}
|
|
|
|
if (laptopScreen is null) return null;
|
|
else return laptopScreen;
|
|
}
|
|
|
|
public static int GetRefreshRate()
|
|
{
|
|
DEVMODE dm = CreateDevmode();
|
|
|
|
Screen laptopScreen = FindLaptopScreen();
|
|
int frequency = -1;
|
|
|
|
if (laptopScreen is null)
|
|
return -1;
|
|
|
|
if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
|
|
{
|
|
frequency = dm.dmDisplayFrequency;
|
|
}
|
|
|
|
return frequency;
|
|
}
|
|
|
|
public static int SetRefreshRate(int frequency = 120)
|
|
{
|
|
DEVMODE dm = CreateDevmode();
|
|
Screen laptopScreen = FindLaptopScreen();
|
|
|
|
if (laptopScreen is null)
|
|
return -1;
|
|
|
|
if (0 != NativeMethods.EnumDisplaySettingsEx(laptopScreen.DeviceName, NativeMethods.ENUM_CURRENT_SETTINGS, ref dm))
|
|
{
|
|
dm.dmDisplayFrequency = frequency;
|
|
int iRet = NativeMethods.ChangeDisplaySettingsEx(laptopScreen.DeviceName, ref dm, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
|
|
return iRet;
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static Guid GetActiveScheme()
|
|
{
|
|
IntPtr pActiveSchemeGuid;
|
|
var hr = PowerGetActiveScheme(IntPtr.Zero, out pActiveSchemeGuid);
|
|
Guid activeSchemeGuid = (Guid)Marshal.PtrToStructure(pActiveSchemeGuid, typeof(Guid));
|
|
return activeSchemeGuid;
|
|
}
|
|
|
|
public static int GetCPUBoost()
|
|
{
|
|
IntPtr AcValueIndex;
|
|
Guid activeSchemeGuid = GetActiveScheme();
|
|
|
|
UInt32 value = PowerReadACValueIndex(IntPtr.Zero,
|
|
activeSchemeGuid,
|
|
GUID_CPU,
|
|
GUID_BOOST, out AcValueIndex);
|
|
|
|
return AcValueIndex.ToInt32();
|
|
|
|
}
|
|
|
|
public static void SetCPUBoost(int boost = 0)
|
|
{
|
|
Guid activeSchemeGuid = GetActiveScheme();
|
|
|
|
var hrAC = PowerWriteACValueIndex(
|
|
IntPtr.Zero,
|
|
activeSchemeGuid,
|
|
GUID_CPU,
|
|
GUID_BOOST,
|
|
boost);
|
|
|
|
PowerSetActiveScheme(IntPtr.Zero, activeSchemeGuid);
|
|
|
|
var hrDC = PowerWriteDCValueIndex(
|
|
IntPtr.Zero,
|
|
activeSchemeGuid,
|
|
GUID_CPU,
|
|
GUID_BOOST,
|
|
boost);
|
|
|
|
PowerSetActiveScheme(IntPtr.Zero, activeSchemeGuid);
|
|
|
|
|
|
}
|
|
}
|