Esempio n. 1
0
        // From Microsoft's Knowledge Base article #140723:

        // http://support.microsoft.com/kb/140723

        // "How to force a screen saver to close once started

        // in Windows NT, Windows 2000, and Windows Server 2003"


        public static void KillScreenSaver(bool canCloseForegroundWindow)
        {
            IntPtr hDesktop = WinAPI.OpenDesktop("Screen-saver", 0,
                                                 false, DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);

            if (hDesktop != IntPtr.Zero)
            {
                WinAPI.EnumDesktopWindows(hDesktop, new
                                          WinAPI.EnumDesktopWindowsProc(KillScreenSaverFunc),
                                          IntPtr.Zero);
                WinAPI.CloseDesktop(hDesktop);
            }
            else
            {
                if (canCloseForegroundWindow)
                {
                    WinAPI.PostMessage(WinAPI.GetForegroundWindow(), WM_CLOSE,
                                       0, 0);
                }
            }
        }
Esempio n. 2
0
        public static void RenderInit()
        {
#if SWITCHDESKTOP
            IntPtr hDesktop;

            if (Base.hOldDesktop == IntPtr.Zero)
            {
                Base.hOldDesktop = WinAPI.OpenInputDesktop(0, false, WinAPI.DESKTOP_ACCESS.GENERIC_ALL);
                if (Base.hOldDesktop == IntPtr.Zero)
                {
                    Debug.Assert(false, "SOCON##");
                }
                //MessageBox.Show(Marshal.GetLastWin32Error().ToString(), "SOCON##");
            }
            else
            {
                Debug.WriteLine("?!!!!!!!!!!!!!!!!!!!!!!!! OpenInputDesktop -> " + Base.hOldDesktop);
            }
            if ((hDesktop = soconwnt.ScwntCreateDesktop()) == IntPtr.Zero)
            {
                Debug.Assert(false, "SOCON# " + Marshal.GetLastWin32Error());
                //MessageBox.Show(Marshal.GetLastWin32Error().ToString(), "SOCON#");
            }

            //CreateProcessAsSystem("cmd", 1, "WinSta0\\socon_", false, WinAPI.PROCESS_PRIORITY.NORMAL, false, out WinAPI.ProcessInformation info);


            WinAPI.SwitchDesktop(hDesktop);
            if (!WinAPI.SetThreadDesktop(hDesktop))
            {
                Render.DefaultSource.Instance.PushTextNormal("SetThreadDesktop failed! " + Marshal.GetLastWin32Error());
                Console.Beep();
            }
#endif

            var wcx = WinAPI.WNDCLASSEX.Build();
            wcx.style         = WinAPI.WindowClassStyle.CS_NOCLOSE /* | WinAPI.WindowClassStyle.CS_HREDRAW | WinAPI.WindowClassStyle.CS_VREDRAW*/;
            wcx.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(new WinAPI.WndProc(myWndProc));
            wcx.cbClsExtra    = 0;
            wcx.cbWndExtra    = 0;
            wcx.hInstance     = Process.GetCurrentProcess().Handle;
            wcx.hIcon         = IntPtr.Zero;
            wcx.hCursor       = IntPtr.Zero;
            wcx.hbrBackground = (IntPtr)2;             // Black
            wcx.lpszMenuName  = null;
            wcx.lpszClassName = "socon";
            wcx.hIconSm       = IntPtr.Zero;

            ushort regClass;
            regClass = WinAPI.RegisterClassExW(ref wcx);
            var err = Marshal.GetLastWin32Error();
            Debug.Assert(regClass != 0, "RegisterClassEx, Last err: " + err);

            Base.hWnd = WinAPI.CreateWindowExW(
                WinAPI.WindowStylesEx.WS_EX_CONTEXTHELP /* |
                                                         * WinAPI.WindowStylesEx.WS_EX_TOPMOST*/,
                regClass,
                "socon",
                WinAPI.WindowStyles.WS_POPUP | WinAPI.WindowStyles.WS_MINIMIZE,
                0,
                0,
                Screen.ScreenSize.Width,
                Screen.ScreenSize.Height,
                IntPtr.Zero,
                IntPtr.Zero,
                wcx.hInstance,
                IntPtr.Zero);

            Debug.Assert(Base.hWnd != IntPtr.Zero, "CreateWindowEx, Last err: " + Marshal.GetLastWin32Error());

            /*Base.Wnd = new MainFormWndHook();
             * Base.Wnd.FormBorderStyle = FormBorderStyle.None;
             * Base.Wnd.Location = new Point(0, 0);
             * Base.Wnd.Size = Screen.ScreenSize;
             * Base.Wnd.ShowInTaskbar = false;
             * Base.hWnd = Base.Wnd.Handle;*/

            Base.D2DFactory = new SharpDX.Direct2D1.Factory();
            Base.DWFactory  = new SharpDX.DirectWrite.Factory();

            Base.DefaultTextFormat = new TextFormat(
                Base.DWFactory,
                Settings.Text.FontFamily,
                FontWeight.Normal,
                SharpDX.DirectWrite.FontStyle.Normal,
                FontStretch.Normal,
                Settings.Text.FontSize)
            {
                TextAlignment      = TextAlignment.Leading,
                ParagraphAlignment = ParagraphAlignment.Near
            };

            var desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(Screen.ScreenSize.Width, Screen.ScreenSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = new RawBool(true),
                OutputHandle      = Base.hWnd,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput,
                Flags             = SwapChainFlags.AllowModeSwitch
            };

            SharpDX.Direct3D11.Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug,
                new SharpDX.Direct3D.FeatureLevel[] {
                SharpDX.Direct3D.FeatureLevel.Level_10_0
            },
                desc,
                out Base.DXDevice,
                out Base.DXSwapChain
                );

            Base.DXBackBuffer = Texture2D.FromSwapChain <Texture2D>(Base.DXSwapChain, 0);

            Surface surface = Base.DXBackBuffer.QueryInterface <Surface>();

            Base.D2DRenderTarget = new RenderTarget(
                Base.D2DFactory,
                surface,
                new RenderTargetProperties(
                    new PixelFormat(
                        Format.Unknown,
                        SharpDX.Direct2D1.AlphaMode.Premultiplied
                        )
                    )
                );

            surface.Dispose();

            WinAPI.ShowWindow(Base.hWnd, 1);
            while (true)
            {
                try {
                    Base.DXSwapChain.SetFullscreenState(new RawBool(true), null);
                    break;
                } catch { Thread.Sleep(50); }
            }

            Base.Brushes.InitBrushes();

            if (!Base.FirstInit)
            {
                Render.Elements.AllElements.ForEach(x => x.RebaseDX());
            }
            else
            {
                // First init
                var fpsRenderer = new Render.FPS();
                var topLeft     = new Render.TopLeftElements();
                topLeft.AddElement(fpsRenderer);
                Base.CurrentKeyboardInput.SwitchReceiver(Render.DefaultSource.Instance);
                Base.BoxListeners.Add(Render.DefaultSource.Instance);
                Render.Elements.Add(Render.DefaultSource.Instance);
                Render.Elements.Add(topLeft);
            }

            Base.FirstInit = false;
            Base.EvTheBox.Set();
            Base.TheBox = true;

            for (;;)
            {
                if (!Base.TheBox)
                {
                    // DIE
                    Base.DXSwapChain.SetFullscreenState(new RawBool(false), null);
                    Debug.Assert(WinAPI.DestroyWindow(Base.hWnd));
                    Debug.Assert(WinAPI.UnregisterClass("socon", Process.GetCurrentProcess().Handle));
                    Base.hWnd = IntPtr.Zero;
                    Base.DefaultTextFormat.Dispose();
                    Base.DefaultTextFormat = null;
                    Base.D2DRenderTarget.Dispose();
                    Base.D2DRenderTarget = null;
                    Base.D2DFactory.Dispose();
                    Base.D2DFactory = null;
                    Base.DWFactory.Dispose();
                    Base.DWFactory = null;
                    Base.DXBackBuffer.Dispose();
                    Base.DXBackBuffer = null;
                    Base.DXDevice.ImmediateContext.ClearState();
                    Base.DXDevice.ImmediateContext.Flush();
                    Base.DXDevice.Dispose();
                    Base.DXDevice = null;
                    Base.DXSwapChain.Dispose();
                    Base.DXSwapChain = null;
                    GC.Collect();
                    foreach (var list in Base.BoxListeners)
                    {
                        list.BoxSwitched(false);
                    }

#if SWITCHDESKTOP
                    List <IntPtr> hwnds = new List <IntPtr>();
                    int           self  = Process.GetCurrentProcess().Id;
                    WinAPI.EnumWindows((IntPtr hWnd, IntPtr lParam) => {
                        WinAPI.GetWindowThreadProcessId(hWnd, out int procId);
                        if (procId == self)
                        {
                            hwnds.Add(hWnd);
                        }
                        return(true);
                    }, IntPtr.Zero);
                    foreach (var hWnd in hwnds)
                    {
                        WinAPI.DestroyWindow(hWnd);
                        var className = new StringBuilder(255);
                        WinAPI.GetClassName(hWnd, className, className.Capacity);
                        WinAPI.UnregisterClass(className.ToString(), Process.GetCurrentProcess().Handle);
                    }

                    WinAPI.SwitchDesktop(Base.hOldDesktop);
                    new Thread(() => {
                        Base.RenderThread.Join();
                        Debug.WriteLine("Main CloseDesktop");
                        if (!WinAPI.CloseDesktop(hDesktop))
                        {
                            var a = Marshal.GetLastWin32Error();
                            var b = 2;
                        }
                        else
                        {
                            Debug.WriteLine("!!!!!@@!!!!!!!!!!!!! CloseDesktop " + hDesktop);
                        }
                    }).Start();
#endif
                    Base.EvTheBox.Set();
                    return;
                }

                const int WM_DISPLAYCHANGE = 0x007E;
                const int OCM_BASE         = 0x2000;

                /*while (PeekMessage(out msg, IntPtr.Zero, 0, 0, 0) != 0) {
                 *      if (GetMessage(out msg, IntPtr.Zero, 0, 0) == -1)
                 *              continue;
                 *
                 *      if (msg.msg == OCM_BASE + WM_DISPLAYCHANGE)
                 *              Debug.WriteLine("DISPLAY CHANGE: " + msg.lParam + " " + msg.wParam);
                 *
                 *      //var message = new System.Windows.Forms.Message() { HWnd = msg.handle, LParam = msg.lParam, Msg = (int)msg.msg, WParam = msg.wParam };
                 *      TranslateMessage(ref msg);
                 *      DispatchMessage(ref msg);
                 * }*/
                WinAPI.MSG msg;
                if (WinAPI.PeekMessage(out msg, IntPtr.Zero, 0, 0, 1) != 0)
                {
                    WinAPI.TranslateMessage(ref msg);
                    WinAPI.DispatchMessage(ref msg);
                }
                Draw();

                /*WinAPI.MSG msg;
                 * while (WinAPI.GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) {
                 *      WinAPI.TranslateMessage(ref msg);
                 *      WinAPI.DispatchMessage(ref msg);
                 *      Draw();
                 * }*/
                //Application.DoEvents();
            }
            //RenderLoop.Run(Base.Wnd, Draw, false);
        }