SUMTEC -- There's a thing in my bloglet.

But it's not only one. It's many. It's the same as other things but it exactly likes nothing else...

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
MS的代码是这样的:

public static Form ActiveForm
{
      
get
      
{
            IntSecurity.GetParent.Demand();
            IntPtr ptr1 
= UnsafeNativeMethods.GetForegroundWindow();
            Control control1 
= Control.FromHandleInternal(ptr1);
            
if ((control1 != null&& ((control1 as Form) != null))
            
{
                  
return ((Form) control1);
            }

            
return null;
      }

}

 

为什么需要首先进行control1 != null 的判断呢?我怎么记得null as Form == null的,也就是说,难道不能够这么写吗?
public static Form ActiveForm
{
      
get
      
{
            IntSecurity.GetParent.Demand();
            IntPtr ptr1 
= UnsafeNativeMethods.GetForegroundWindow();
            Control control1 
= Control.FromHandleInternal(ptr1);
            
if ((control1 as Form) != null)
            
{
                  
return ((Form) control1);
            }

            
return null;
      }

}

还在好多地方看到一些问题,不一一列出来了。
posted on 2004-07-17 15:43  Sumtec  阅读(1522)  评论(2)    收藏  举报