예제 #1
0
        static void Main()
        {
            logger = LogManager.GetCurrentClassLogger();

            AppDomain.CurrentDomain.UnhandledException += (o, a) =>
            {
                Exception ex = null;

                var obj = a.ExceptionObject;
                if (obj != null)
                {
                    ex = obj as Exception;
                    logger.Fatal(ex);
                }
                if (ex != null)
                {
                    logger.Fatal(ex);
                }
                else
                {
                    logger.Fatal("FATAL ERROR!!!");
                }


                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            };

            logger.Info("========== START ============");

            var  winVersion            = Environment.OSVersion.Version;
            bool isCompatibleOSVersion = (winVersion.Major >= 6 && winVersion.Minor >= 2);

            if (!isCompatibleOSVersion)
            {
                logger.Fatal("Windows versions earlier than 8 are not supported.");

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
            InitMediaLib();

            Shcore.SetProcessPerMonitorDpiAwareness();

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

            MainForm form = new MainForm();

            Application.Run(form);

            ShutdownMediaLib();

            logger.Info("========== THE END ============");


            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm());
        }
예제 #2
0
        private Screen(IntPtr monitor)
        {
            var info = new User32.MONITORINFO()
            {
                cbSize = Marshal.SizeOf <User32.MONITORINFO>()
            };

            if (User32.GetMonitorInfoW(monitor, ref info))
            {
                PhysicalDisplayArea = info.rcMonitor.ToRect();
                PhysicalWorkingArea = info.rcWork.ToRect();
                IsPrimary           = (info.dwFlags & User32.MONITORINFOF_PRIMARY) != 0;
            }
            uint dpix = 96, dpiy = 96;

            try {
                Shcore.GetDpiForMonitor(monitor, Shcore.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, ref dpix, ref dpiy);
            }
            catch (DllNotFoundException) {
            }
            catch (EntryPointNotFoundException) {
            }
            DpiX = (int)dpix;
            DpiY = (int)dpiy;
        }
예제 #3
0
 /// <summary>
 /// Tries to enable per-monitor high dpi support if possible (win8.1+), or general high dpi support (vista+).
 /// This can only be set once at process startup. To use this method <see cref="System.Windows.Media.DisableDpiAwarenessAttribute"/>
 /// must be set on the assembly.
 /// </summary>
 /// <returns></returns>
 public static bool EnableHighDpiSupport()
 {
     if (Shcore.IsPlatformSupported)
     {
         return(Shcore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE));
     }
     return(User32.SetProcessDPIAware());
 }
예제 #4
0
        protected override void OnStartup(StartupEventArgs e)
        {
            var test = Shcore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE);

            if (!test)
            {
                var err = new Win32Exception();
            }

            base.OnStartup(e);
        }
예제 #5
0
    internal static int GetScreenDpi(Screen currentScreen)
    {
        if (IsPerMonitorV2Awareness)
        {
            var hMonitor = User32.MonitorFromPoint(new POINT(currentScreen.Bounds.X + currentScreen.Bounds.Width / 2, currentScreen.Bounds.Y + currentScreen.Bounds.Height / 2), MonitorOptions.MONITOR_DEFAULTTONEAREST);

            Shcore.GetDpiForMonitor(hMonitor, DpiType.Effective, out var dpiX, out var dpiY);
            return((int)dpiY);
        }

        return(DeviceDpi);
    }
예제 #6
0
    internal static int GetScreenDpiFromPoint(Point point)
    {
        if (IsPerMonitorV2Awareness)
        {
            var hMonitor = User32.MonitorFromPoint(new POINT(point), MonitorOptions.MONITOR_DEFAULTTONEAREST);

            Shcore.GetDpiForMonitor(hMonitor, DpiType.Effective, out var dpiX, out var dpiY);
            return((int)dpiY);
        }

        return(DeviceDpi);
    }
예제 #7
0
    internal static float GetScaleFactorForCurrentWindow(IntPtr handle)
    {
        if (IsPerMonitorV2Awareness)
        {
            var hMonitor = User32.MonitorFromWindow(handle, User32.MONITOR_DEFAULTTONEAREST);

            Shcore.GetDpiForMonitor(hMonitor, DpiType.Effective, out var dpiX, out var dpiY);

            return(dpiX / LOGICAL_DPI);
        }

        return(DeviceDpi / LOGICAL_DPI);
    }
예제 #8
0
    internal static int GetDpiForCurrentWindow(IntPtr handle)
    {
        if (IsPerMonitorV2Awareness)
        {
            var hMonitor = User32.MonitorFromWindow(handle, User32.MONITOR_DEFAULTTONEAREST);

            Shcore.GetDpiForMonitor(hMonitor, DpiType.Effective, out var dpiX, out var dpiY);

            return((int)dpiX);
        }

        return(DeviceDpi);
    }
예제 #9
0
        static void Main()
        {
            MediaToolkitManager.Startup();

            //DwmApi.DisableAero(true);
            Shcore.SetProcessPerMonitorDpiAwareness();

            WinMM.timeBeginPeriod(1);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());

            WinMM.timeEndPeriod(1);
        }
예제 #10
0
        void InitBorders()
        {
            _left = new BorderWindow(this)
            {
                Side = BorderSide.Left
            };
            _top = new BorderWindow(this)
            {
                Side = BorderSide.Top
            };
            _right = new BorderWindow(this)
            {
                Side = BorderSide.Right
            };
            _bottom = new BorderWindow(this)
            {
                Side = BorderSide.Bottom
            };

            SetRegion(hWndContent, 0, 0, true);

            var hSrc = HwndSource.FromHwnd(hWndContent);

            hSrc.AddHook(WndProc);


            //Calculate the effective DPI used by WPF;
            _wpfDPI = 96.0 * hSrc.CompositionTarget.TransformToDevice.M11;
            //Get the Current DPI of the monitor of the window.
            _monitorDPI = Shcore.GetDpiForWindow(hSrc.Handle);
            //Calculate the scale factor used to modify window size, graphics and text
            _dpiScaleFactor       = _monitorDPI / _wpfDPI;
            ContentWindow.Width  *= _dpiScaleFactor;
            ContentWindow.Height *= _dpiScaleFactor;
            RescaleForDpi();

            // SWP_DRAWFRAME makes window bg really transparent (visible during resize) and not black
            User32.SetWindowPos(hWndContent, IntPtr.Zero, 0, 0, 0, 0,
                                SetWindowPosOptions.SWP_NOOWNERZORDER |
                                SetWindowPosOptions.SWP_DRAWFRAME |
                                SetWindowPosOptions.SWP_NOACTIVATE |
                                SetWindowPosOptions.SWP_NOZORDER |
                                SetWindowPosOptions.SWP_NOMOVE |
                                SetWindowPosOptions.SWP_NOSIZE);
        }
예제 #11
0
        private static void HookupDpiMessage(Window win, IntPtr hwnd)
        {
            var hSrc = HwndSource.FromHwnd(hwnd);

            hSrc.AddHook(WndProc);

            //Calculate the effective DPI used by WPF;
            var wpfDPI = 96.0 * hSrc.CompositionTarget.TransformToDevice.M11;
            //Get the Current DPI of the monitor of the window.
            var monitorDPI = Shcore.GetDpiForWindow(hSrc.Handle);

            SetWindowDpi(win, monitorDPI);
            //Calculate the scale factor used to modify window size, graphics and text
            var dpiScaleFactor = monitorDPI / wpfDPI;

            SetWindowDpiScale(win, dpiScaleFactor);
            win.Width  *= dpiScaleFactor;
            win.Height *= dpiScaleFactor;
            RescaleForDpi(win);
        }
예제 #12
0
    internal static void InitializeDpiHelper()
    {
        if (isInitializeDpiHelper)
        {
            return;
        }

        // initialize shared fields
        Initialize();

        // We are in Windows 10/1603 or greater when this API is present.
        if (ApiHelper.IsApiAvailable("Shcore.dll", nameof(Shcore.GetProcessDpiAwareness)))
        {
            // We are on Windows 10/1603 or greater all right, but we could still be DpiUnaware or SystemAware, so let's find that out...
            PROCESS_DPI_AWARENESS processDpiAwareness;
            var currentProcessId = Kernel32.GetCurrentProcessId();

            IntPtr hProcess = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION, false, currentProcessId);

            var result = Shcore.GetProcessDpiAwareness(hProcess, out processDpiAwareness);

            // Only if we're not, it makes sense to query for PerMonitorV2 awareness from now on, if needed.
            if (!(processDpiAwareness == PROCESS_DPI_AWARENESS.PROCESS_DPI_UNAWARE ||
                  processDpiAwareness == PROCESS_DPI_AWARENESS.PROCESS_SYSTEM_DPI_AWARE))
            {
                doesNeedQueryForPerMonitorV2Awareness = true;
            }
        }

        if (IsScalingRequired || doesNeedQueryForPerMonitorV2Awareness)
        {
            isScalingRequirementMet = true;
        }

        isInitializeDpiHelper = true;
    }
예제 #13
0
        public static int Main(string[] args)
        {
            int exitCode = 0;

            InitLogger();

            logger.Info("============================ START ============================");

            bool  createdNew = false;
            Mutex mutex      = null;

            try
            {
                mutex = new Mutex(true, AppConsts.ApplicationId, out createdNew);
                if (!createdNew)
                {
                    logger.Info("Another instance is already running...");
                    if (!Models.AppModel.AllowMutipleInstance)
                    {
                        var res = Services.WndProcService.ShowAnotherInstance();
                        return(0);

                        //return -1;
                        //...
                    }
                }

                StartupParams = StartupParameters.Create(args);

                logger.Info(StartupParams.GetSysInfo());

                try
                {
                    MediaToolkitManager.Startup();

                    Shcore.SetProcessPerMonitorDpiAwareness();

                    var application = new App();
                    application.DispatcherUnhandledException += Application_DispatcherUnhandledException;
                    application.InitializeComponent();

                    logger.Info("============================ RUN ============================");
                    application.Run();
                }
                finally
                {
                    MediaToolkitManager.Shutdown();
                }
            }
            catch (Exception ex)
            {
                ProcessError(ex);
            }
            finally
            {
                if (mutex != null)
                {
                    if (createdNew)
                    {
                        mutex.ReleaseMutex();
                    }
                    mutex.Dispose();
                }

                logger.Info("============================ THE END ============================");
            }


            return(exitCode);
        }
예제 #14
0
        static void Main(string[] args)
        {
            InitLogger();

            logger.Info("========== START ============");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            StartupParams = StartupParameters.Create(args);

            //if (StartupParams.RunAsSystem)
            //{
            //    if (StartupParams.IsElevated)
            //    {
            //        if (RestartAsSystem() > 0)
            //        {
            //            return;
            //        }
            //        logger.Warn("Restart failed...");
            //    }
            //    else
            //    {
            //        RunAsSystem();
            //    }
            //}

            if (StartupParams.IsElevated)
            {
                if (!StartupParams.NoRestart)
                {//Restart application with system permissions...
                    if (RestartAsSystem() > 0)
                    {
                        return;
                    }
                    logger.Warn("Restart failed...");
                    // открываем процесс как обычно...
                    //TODO:...
                }
            }



            bool  createdNew = false;
            Mutex mutex      = null;

            try
            {
                mutex = new Mutex(true, AppConsts.ApplicationId, out createdNew);
                if (!createdNew)
                {
                    logger.Info("Another instance is already running...");
                    //...
                }



                bool tempMode = !createdNew;
                Config.Initialize(tempMode);

                MediaToolkitManager.Startup();


                //DwmApi.DisableAero(true);
                Shcore.SetProcessPerMonitorDpiAwareness();

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

                MainForm form = new MainForm();

                logger.Info("========== RUN ============");
                Application.Run(form);
            }
            finally
            {
                Config.Shutdown();

                MediaToolkitManager.Shutdown();

                if (mutex != null)
                {
                    if (createdNew)
                    {
                        mutex.ReleaseMutex();
                    }
                    mutex.Dispose();
                }

                logger.Info("========== THE END ============");
            }
        }
예제 #15
0
        static void Main(string[] args)
        {
            //Console.Title = "App started...";

            logger = LogManager.GetCurrentClassLogger();

            logger.Info("========== START ============");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            bool  createdNew = false;
            Mutex mutex      = null;

            try
            {
                //mutex = new Mutex(true, AppConsts.ApplicationId, out createdNew);
                //if (!createdNew)
                //{
                //    logger.Info("Another instance is already running...");
                //    //...
                //}

                if (args != null && args.Length > 0)
                {//...
                }

                bool tempMode = !createdNew;
                //ScreenStreamer.WinForms.App.Config.Initialize(tempMode);

                MediaToolkitManager.Startup();

                //DwmApi.DisableAero(true);
                Shcore.SetProcessPerMonitorDpiAwareness();

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



                //var screen0 =  Screen.AllScreens[0];
                ////var bound0 = SystemInformation.VirtualScreen;//screen0.Bounds;//new System.Drawing.Rectangle(10, 10, 100, 100); //
                ////var bound0 = screen0.Bounds;//new System.Drawing.Rectangle(10, 10, 100, 100); //

                //var bound0 = new System.Drawing.Rectangle(10, 10, 100, 100); //
                //var src0 = new ScreenSource();
                //var capture0 = new ScreenCaptureDevice
                //{
                //    CaptureRegion = bound0,
                //    Resolution = bound0.Size,
                //    Name = screen0.DeviceName,
                //    DisplayRegion = bound0,
                //    Properties = new ScreenCaptureProperties
                //    {
                //        UseHardware = true,
                //        CaptureType = VideoCaptureType.DXGIDeskDupl,
                //        AspectRatio = true,
                //        Fps = 30,

                //    }
                //};

                //src0.Setup(capture0);
                //src0.Start();


                //var screen1 = Screen.AllScreens[1];

                ////var bound1 = screen1.Bounds;//new System.Drawing.Rectangle(-1920, 10, 10, 10); //

                //var bound1 = new System.Drawing.Rectangle(-1920, 10, 10, 10); //
                //var capture1 = new ScreenCaptureDevice
                //{
                //    CaptureRegion = bound1,
                //    Resolution = bound1.Size,
                //    Name = screen1.DeviceName,
                //    DisplayRegion = bound1,
                //    Properties = new ScreenCaptureProperties
                //    {
                //        UseHardware = true,
                //        CaptureType = VideoCaptureType.DXGIDeskDupl,
                //        AspectRatio = true,
                //        Fps = 30,

                //    }
                //};

                //var src1 = new ScreenSource();
                //src1.Setup(capture1);
                //src1.Start();



                //ScreenStreamer.WinForms.App.MainForm form = new ScreenStreamer.WinForms.App.MainForm();

                MainForm form = new MainForm();



                Application.Run(form);
            }
            finally
            {
                // ScreenStreamer.WinForms.App.Config.Shutdown();

                MediaToolkitManager.Shutdown();

                if (mutex != null)
                {
                    if (createdNew)
                    {
                        mutex.ReleaseMutex();
                    }
                    mutex.Dispose();
                }



                logger.Info("========== THE END ============");
            }
        }