2009-11-15 76 views

回答

2

您可以使用下面的代码:

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

的参数是要显示屏幕预览的形式或控制。请注意,屏幕保护程序会在调整大小之前短暂地全屏显示。

+0

屏幕保护程序已成功加载,但coontrol未调整大小,屏幕保护程序将继续以全屏模式运行。 – Ahmed 2009-11-15 14:28:23

+0

我使用面板作为控制预览屏幕保护程序。 – Ahmed 2009-11-15 14:29:55

+0

这不是需要调整大小的控件,而只是屏幕保护程序运行的窗口。如果您使用面板,只需将面板作为方法参数传递即可。我正在使用这段代码,它工作正常(在Windows 7上) – 2009-11-15 14:33:32

相关问题