我剛剛開始學習C#並試圖編寫屏幕保護程序。我App.xaml.cs包含在使用/ P參數爲下面的代碼:在C#中使用WPF在預覽模式下崩潰的Windows屏幕保護程序
else if (arg1 == "/p")
{
if (e.Args[1] == null)
{
System.Windows.Forms.MessageBox.Show("Invalid or missing window handle.");
System.Windows.Application.Current.Shutdown();
}
IntPtr previewHandle = new IntPtr(long.Parse(e.Args[1]));
System.Windows.Application.Current.Run(new MainWindow(previewHandle));
}
我MainWindow.xaml.cs
然後,這個構造處理預覽呼叫:
public MainWindow(IntPtr previewHandle)
{
App.Current.Properties["isPreviewMode"] = true;
App.Current.Properties["previewHandle"] = previewHandle;
InitializeComponent();
}
後這,它崩潰。在我的MainWindow.xaml中,我有Loaded =「MainWindow_Loaded」。
在MainWindows.xaml.cs這MainWindow_Loaded:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if ((bool)App.Current.Properties["isPreviewMode"])
{
IntPtr myHandle = new WindowInteropHelper(this).Handle;
SetParent(myHandle, (IntPtr)App.Current.Properties["previewHandle"]);
SetWindowLong(myHandle, -16, new IntPtr(GetWindowLong(myHandle, -16) | 0x40000000));
Rectangle ParentRect;
GetClientRect((IntPtr)App.Current.Properties["previewHandle"], out ParentRect);
this.Top = ParentRect.X;
this.Left = ParentRect.Y;
this.Height = ParentRect.Height;
this.Width = ParentRect.Width;
}
ssimage.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/EmbeddedImage.PNG"));
}
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
我在App.xaml.cs其它代碼來處理改變在計時器和其他代碼圖像中MainWindow.xaml.cs到處理鼠標移動,點擊和按鍵。正常運行屏幕保護程序時一切正常。這只是預覽失敗。我究竟做錯了什麼?
你能用像GIF動畫一樣預覽嗎? –
我可能可以,但它顯示的圖像是動態的。它從計算機上的文件夾中讀取它們,因此如果我有GIF,那麼屏幕保護程序運行時將不會顯示實際使用的圖像。 –