How does one run screensaver within Windows Form as background for it?

我只是一个虾纸丫 提交于 2019-12-10 11:43:59

问题


How does one run screensaver within Windows Form as background for it? User also can interact with form controls while screensaver running.

[Why this?] We have a case which we need to run Windows Bubbles screensaver while user can continue interacting with form controls?


回答1:


You can use the following code :

    private void ShowScreenSaver(Control displayControl)
    {
        using (RegistryKey desktopKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop"))
        {
            if (desktopKey != null)
            {
                string screenSaverExe = desktopKey.GetValue("SCRNSAVE.EXE") as string;
                if (!string.IsNullOrEmpty(screenSaverExe))
                {
                    Process p = Process.Start(screenSaverExe, "/P " + displayControl.Handle);
                    p.WaitForInputIdle();
                    IntPtr hwnd = p.MainWindowHandle;
                    if (hwnd != IntPtr.Zero)
                    {
                        SetParent(hwnd, displayControl.Handle);
                        Rectangle r = displayControl.ClientRectangle;
                        MoveWindow(hwnd, r.Left, r.Top, r.Width, r.Height, true);
                    }
                }
            }
        }
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndParent);

    [DllImport("user32.dll")]
    static extern bool MoveWindow(IntPtr hwnd, int x, int y, int width, int height, bool repaint);

The parameter is the form or control in which you want to display the screensaver preview. Note that the screensaver will briefly appear in full screen before it is resized.



来源:https://stackoverflow.com/questions/1737408/how-does-one-run-screensaver-within-windows-form-as-background-for-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!