为什么TForm.Handle是获取器而不是字段? [英] Why TForm.Handle is a getter instead of a field?

查看:99
本文介绍了为什么TForm.Handle是获取器而不是字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在调试一个复杂的错误.这是由于访问不存在的Form.Handle(垃圾状指针)引起的.该错误以一种非常出乎意料的方式向我展示了自己的信息-访问Forms Handle会导致调整大小和重新绘制.

I was debugging a complicated bug recently. It was caused by accessing a non-existing Form.Handle (garbaged pointer). The bug revealed itself in rather unexpected way for me - accessing Forms Handle caused resizes and repaints.

我希望通过垃圾指针访问Form.Handle只会返回一些垃圾THandle.期望Handle在创建表单时创建一次,并保持不变直到表单被销毁.

I would expect accessing Form.Handle by a garbage pointer would just return some garbage THandle. Expecting that the Handle is created once on form creation and stays the same till the Form is destroyed.

为什么会这样,TForm.Handle不是在表单创建时初始化的字段,而是通过

Why is it so, that TForm.Handle is not a field that gets initialized on form creation and is accessed via

property Handle: Integer read FHandle;

,但是吸气剂

property Handle: Integer read GetHandle;

在第一次访问时创建句柄甚至窗口(CreateWnd)?

that creates the Handle and even the Window (CreateWnd) on first access?

推荐答案

即使基础操作系统 window 不存在, object 形式也可能存在.在这段时间内,Handle字段将为0,这对于需要有效窗口句柄的代码没有帮助.为了确保每次需要一个有效的句柄,都需要在引用Handle字段之前调用HandleNeeded.作为具有吸气剂的属性,该属性可以自动为您调用HandleNeeded,从而使使用Handle属性更加容易.

The form object can exist even when the underlying OS window doesn't. During those times, the Handle field would be 0, which isn't helpful to code that needs a valid window handle. To ensure you get a valid handle each time you need one, you'd need to call HandleNeeded prior to referring to the Handle field. As a property with a getter, the property can call HandleNeeded for you automatically, making it easier to use the Handle property.

这篇关于为什么TForm.Handle是获取器而不是字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆