Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体屏幕保护程序预览窗口句柄_C#_Windows_Winforms - Fatal编程技术网

C# Windows窗体屏幕保护程序预览窗口句柄

C# Windows窗体屏幕保护程序预览窗口句柄,c#,windows,winforms,C#,Windows,Winforms,我已经构建了一个Windows窗体屏幕保护程序,但我似乎无法理解预览功能不起作用的原因 预览的构造函数重载 public ScreenSaverForm(IntPtr PreviewWndHandle) { InitializeComponent(); //set the preview window as the parent of this window SetParent(this.Handle, PreviewWndHandle); //make this a ch

我已经构建了一个Windows窗体屏幕保护程序,但我似乎无法理解预览功能不起作用的原因

预览的构造函数重载

public ScreenSaverForm(IntPtr PreviewWndHandle)
{
  InitializeComponent();

  //set the preview window as the parent of this window
  SetParent(this.Handle, PreviewWndHandle);

  //make this a child window, so when the select screensaver 
  //dialog closes, this will also close
  SetWindowLong(this.Handle, -16,
        new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));

  //set our window's size to the size of our window's new parent
  Rectangle ParentRect;
  GetClientRect(PreviewWndHandle, out ParentRect);
  this.Size = ParentRect.Size;

  //set our location at (0, 0)
  this.Location = new Point(0, 0);

  previewMode = true;
}
Program.cs或接受命令参数的主入口点

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
 if (args.Length > 0)
        {
            if (args[0].ToLower().Trim().Substring(0,2) == "/s") //show
            {
                //show the screen saver
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                ShowScreenSaver(); //this is the good stuff
                Application.Run();
            }
            else if (args[0].ToLower().Trim().Substring(0,2) == "/p") //preview
            {
              //show the screen saver preview
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              //args[1] is the handle to the preview window
              Application.Run(new ScreenSaverForm(new IntPtr(long.Parse(args[1]))));
            }
            else if (args[0].ToLower().Trim().Substring(0,2) == "/c") //configure
            {
              //nothing to configure
              MessageBox.Show(
              "This screensaver has no options that you can set",
              "Dancing Polygons",
              MessageBoxButtons.OK,
              MessageBoxIcon.Information);
            }
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ShowScreenSaver(); //this is the good stuff
            Application.Run();
        }
}
所以我无法理解为什么我会得到一个空引用。我相信这是命令的论点。我正在Windows8.1中运行这个屏幕保护程序

   SetParent(this.Handle, PreviewWndHandle);
不要在构造函数中使用Handle属性。这将强制过早创建窗口。您的加载事件在构造函数完成运行之前激发。这是非常不健康的,当然,当加载事件处理程序使用尚未设置的类的字段时,很容易导致NullReferenceException

您没有发布轰炸(屏幕保护程序加载)的代码,因此我们无法猜测哪个特定语句失败。使用Debug>Exceptions>CLR Exceptions>勾选抛出复选框,以确保在引发异常时调试器停止,以便可以看到失败的语句

此外,当Winforms被迫重新创建窗口时,句柄属性值可能会更改。此代码的正确位置是OnHandleCreated()方法的重写。在这一点上,句柄属性保证是有效的,并且不会引起副作用

   this.Size = ParentRect.Size;

在构造函数中更改Size属性也是错误的,表单的AutoScaleMode属性将在以后更改它以适应视频适配器的DPI设置。您必须延迟分配大小,直到加载事件以正常方式触发时,所有自动缩放都已完成。

好的,谢谢,我会处理完并返回给您。好的,是的,我找到了。我的计时器对象未实例化。好吧,那是个愚蠢的错误。好了,现在一切都解决了。谢谢你的帮助,汉斯·帕桑。
   this.Size = ParentRect.Size;