using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CS_TEST
{
    internal static class Program
    {
        private static readonly IntPtr DpiAwarenessContextPerMonitorAwareV2 = new IntPtr(-4);

        [DllImport("user32.dll")]
        private static extern bool SetProcessDpiAwarenessContext(IntPtr dpiContext);

        [DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();

        private static void EnableBestProcessDpiAwareness()
        {
            // Match the native hwscapture_test behavior. This must run before any WinForms
            // window/control is created, otherwise Windows may bitmap-scale the whole UI
            // after maximize or DPI changes, making the D3D preview look soft.
            try
            {
                if (SetProcessDpiAwarenessContext(DpiAwarenessContextPerMonitorAwareV2))
                    return;
            }
            catch
            {
                // Older Windows versions may not export SetProcessDpiAwarenessContext.
            }

            try
            {
                SetProcessDPIAware();
            }
            catch
            {
            }
        }

        [STAThread]
        static void Main()
        {
            EnableBestProcessDpiAwareness();

            // Do not rely on the default generated WinForms DPI mode. Use PerMonitorV2
            // explicitly so the preview child HWND size is in real device pixels.
            Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
