Skip to content

Commit

Permalink
SetDpiAwareness() and Program.cs cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnq committed Sep 22, 2024
1 parent a5c3bcf commit ef68c76
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,95 @@ namespace RED.mbnq
{
static class Program
{
#region variables and constants
public static mbCrosshair mainCrosshair;
public const int mbFrameDelay = 16; // in ms, for glass hud, default 60fps
public const string mbVersion = "0.1.0.6";
#endregion

#region DPI
[DllImport("user32.dll")]
static extern bool SetProcessDPIAware();

[DllImport("user32.dll")]
private static extern bool SetProcessDpiAwarenessContext(IntPtr dpiFlag);
private static readonly IntPtr DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = new IntPtr(-4);
private static readonly IntPtr DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new IntPtr(-2);
#endregion

[STAThread]
static void Main()
{
SetProcessDPIAware(); // this is co crucial to deal with windows DPI desktop scaling...
SetDpiAwareness(); // this is an alternative

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// Initialize MainDisplay
#region init sequence

// Crosshair init
mainCrosshair = new mbCrosshair();

// Initialize ControlPanel
ControlPanel controlPanel = new ControlPanel
{
mbCrosshairOverlay = mainCrosshair
};
// ControlPanel init
ControlPanel controlPanel = new ControlPanel { mbCrosshairOverlay = mainCrosshair };

// SniperMode init
ZoomMode.InitializeZoomMode(controlPanel);
GlobalMouseHook.SetHook();

// Load settings and update display
// Load settings
SaveLoad.mbLoadSettings(controlPanel);

// Check for a custom overlay file
// Check for a custom crosshair overlay file
var customFilePath = Path.Combine(ControlPanel.mbUserFilesPath, "RED.custom.png");
if (File.Exists(customFilePath))
{
mainCrosshair.SetCustomPNG();
}
if (File.Exists(customFilePath)) { mainCrosshair.SetCustomPNG(); }

// Update the main display after settings have loaded
controlPanel.UpdateMainCrosshair();
// Update all after loading settings
controlPanel.UpdateAllUI();

// trigger autosave
// autoSave on exist eventHandler
controlPanel.FormClosing += (sender, e) =>
{
if (controlPanel.mbAutoSaveCheckbox.Checked)
{
SaveLoad.mbSaveSettings(controlPanel);
}
if (controlPanel.mbAutoSaveCheckbox.Checked) { SaveLoad.mbSaveSettings(controlPanel); }
};

// hiding controlPanel before showing splashScreen
controlPanel.Visible = false;
controlPanel.Size = new Size(0, 0);
Application.Run(controlPanel);

#endregion

#region DpiAwereness helper fnc
static void SetDpiAwareness()
{
try
{
// Try to use the latest DPI awareness method available in Windows 10
if (Environment.OSVersion.Version.Major >= 10)
{
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
{
Console.WriteLine("DPI Awareness set to Per-Monitor Aware (V2).");
return;
}
}

// If running on older versions, fallback to the older DPI aware method
if (!SetProcessDPIAware())
{
throw new Exception("Failed to set DPI Awareness.");
}

Console.WriteLine("DPI Awareness set to System DPI Aware.");
}
catch (Exception ex)
{
MessageBox.Show($"DPI Awareness could not be set: {ex.Message}");
}
}
#endregion
}
}
}

0 comments on commit ef68c76

Please sign in to comment.