如何在Windows窗体中运行屏幕保护程序作为它的背景?

如何在Windows窗体中运行屏幕保护程序作为它的背景? 用户还可以在屏幕保护程序运行时与表单控件进行交互。

[为什么会这样?]我们有一个案例需要运行Windows Bubbles屏幕保护程序,而用户可以继续与表单控件进行交互?

您可以使用以下代码:

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); 

参数是要在其中显示屏幕保护程序预览的表单或控件。 请注意,屏幕保护程序在resize之前会短暂地全屏显示。