1
如何在Windows窗体中运行屏幕保护程序作为背景? 用户还可以在屏幕保护程序运行时与窗体控件进行交互。如何在Windows窗体中运行屏幕保护程序作为背景?
[为什么呢?] 我们有我们需要运行Windows泡泡屏保,同时用户 可以继续使用表单控件交互的情况下?
如何在Windows窗体中运行屏幕保护程序作为背景? 用户还可以在屏幕保护程序运行时与窗体控件进行交互。如何在Windows窗体中运行屏幕保护程序作为背景?
[为什么呢?] 我们有我们需要运行Windows泡泡屏保,同时用户 可以继续使用表单控件交互的情况下?
您可以使用下面的代码:
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);
的参数是要显示屏幕预览的形式或控制。请注意,屏幕保护程序会在调整大小之前短暂地全屏显示。
屏幕保护程序已成功加载,但coontrol未调整大小,屏幕保护程序将继续以全屏模式运行。 – Ahmed 2009-11-15 14:28:23
我使用面板作为控制预览屏幕保护程序。 – Ahmed 2009-11-15 14:29:55
这不是需要调整大小的控件,而只是屏幕保护程序运行的窗口。如果您使用面板,只需将面板作为方法参数传递即可。我正在使用这段代码,它工作正常(在Windows 7上) – 2009-11-15 14:33:32