日期:2014-05-18 浏览次数:20951 次
System.Windows.Forms.Control.get_Handle()
System.Windows.Forms.Control.set_WindowText(String value)
System.Windows.Forms.Control.set_Text(String value)
System.Windows.Forms.Label.set_Text(String value)
在 set_WindowText 方法中通过
UnsafeNativeMethods.SetWindowText(new HandleRef(this.window, this.Handle), value);
去设置Text,这里要访问Handle,确保Handle正确,就会使用get_Handle,代码如下
public IntPtr get_Handle()
{
//这里会检查
if ((checkForIllegalCrossThreadCalls && !inCrossThreadSafeCall) && this.InvokeRequired)
{
throw new InvalidOperationException(SR.GetString("IllegalCrossThreadCall", new object[] { this.Name }));
}
if (!this.IsHandleCreated)
{
this.CreateHandle();
}
return this.HandleInternal;
}
设置BackGroundImage时,代码中不需要检查Handle
public virtual Image BackgroundImage
{
get
{
return (Image) this.Properties.GetObject(PropBackgroundImage);
}
set
{
if (this.BackgroundImage != value)
{
this.Properties.SetObject(PropBackgroundImage, value);
this.OnBackgroundImageChanged(EventArgs.Empty);
}
}
}