How does one run screensaver within Windows Form as background for it?
How does one run screensaver within Windows Form as background for it? User also can interact with form controls while screensaver running.
[Wh开发者_StackOverflow社区y this?] We have a case which we need to run Windows Bubbles screensaver while user can continue interacting with form controls?
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.
精彩评论