3

我在Control.

if (ParentForm != null)
{
  traceBlock.Log("ParentForm is null");

  if (!ParentForm.IsHandleCreated)
  {
    ParentForm.HandleCreated += (sender, e) =>
        {
          var text = PhysDocContext.Document.GetHeader(PhysDocContext);
          ParentForm.Text = text;
        };
  }
}

此代码使用ParentForm == null. 很明显,记录器正在记录不准确的信息。真正让我感到奇怪的是检查IsHandleCreated. 在我看来,这段代码永远不会触发。所以事件不会挂钩,并且标题永远不会被设置。

更奇怪的是,ParentForm 被设置Panel为父窗体拥有的控件。它也由作为父级的 Form 设置。让我相信这是一个更不可能的情况。

Form在没有句柄的 .NET 中可以访问a 是否正常(甚至可能) ?

4

2 回答 2

2

Yes, it's possible. The handle is the window handle of the form and it may not be created until the form actually has a window (visible or invisible).

于 2012-10-10T12:41:52.407 回答
2

Is it normal (possible even) for a Form to be accessible in .NET that has no handle?

Yes, because a Form (or a Control for that matter) is merely a wrapper class around a few Win32 API calls. The code you use to create a form is used to create the .NET object.

Most likely only when the form is shown, the calls to Win32 are made, finally giving the control a handle.

于 2012-10-10T12:43:32.017 回答