c#利用winapi遍历桌面控件算法

public enum GUIInfoType

{

     guiText.

     guiTextClass,

     guiTextParent,

     guiTextClassParent,

}

 

 

 

 

 

private static int level=0

public static int FindGUILike(ref int hWndArray,int hWndStart,ref string windowText,ref string className,ref string parentText)

{

int hwnd=0;

int r=0;

StringBuilder sWindowText=new StringBuilder();

StringBuilder sClassname=new StringBuilder();

StringBuilder sParentText=new StringBuilder();

 

if(level==0)

{

  hWndTarget=0;

  if(hWndTarget==0) hWndStart = GetDesktopWindow();
}

 

level=level+1;

hwnd=GetWindow(hWndStart,GW_CHILD);

while(hwnd!=0)

{

   r=FindGUILike(ref hWndArray,hwnd windowText,ref classname,ref parentText);

   sWindowText.Capacity=255;

   r=GetClassName(hwnd,sWindowsText,255);

  sClassname.Capacity=255;

  r=GetWindowText(GetParent(hwnd),sParentText,225);

  GUIInfoType guiInfoType=GetGUIInfoType(windowText,className,parentText);

  ResetGUIInfo(guiInfoType,hwnd,ref hWndArray,ref windowText,ref className,ref parentText,sWindowText,sClassname,sParentText);

  hwnd=GetWindow(hwnd,GW_HWNDNEXT);

}

level=level-1;

return 0;

}

 

private static GUIInfoType GetGUIInfoType(string winText,string winClass,string winTextParent)

{

  if(winText!=''&&winClass!=''&winTextParent=='')

    return GUIInfoType.guiTextClass;

  else if(winText!=''&&winClass==''&winTextParent!='')

   return GUIInfoType.guiTextParent;

  else if(winText!=''&&winClass!=''&winTextParent=='')

    return GUIInfoType.guiClassParent;

  return GUIInfoType.guiText;

}

 

private static void ResetGUIInfo(GUIInfoType guiInfoType,int hwnd,ref int hWndTarget,ref string windowText,ref string className,ref string parentText,StringBuilder sParentText)

{

  string clsStartedWith='';

  if(className.IndexOf('.')>=0)

   {

     clsStartedWith=className.Replace(className.Split('.')[className.Split('.').Length-1],'');

   }

else

{

  clsStartedWith=className;

}

  if(guiInfoType==GUIInfoType.guiText)

{

    if(sWindowText.ToString()==windowText)

{

     hWndTarget=hwnd;

     className=sClassname.ToString();

     parentText=sParentText.ToString();

}

}

else if(guiInfoType==GUIInfoType.guiTextClass)

{

    if(sWindowText.Tostring()==windowText&&sClassname.Tostring().StartsWith(clsStartedWith))

   {

        hWndTarget=hwnd;

        parentTexts=sParentText.ToString();

    }

 

}

else if(guiInfoType==GUIInfoType.guiTextParent)

{

 if(sWindowText.ToString()==windowText&&sParentText.ToString()==parentText)

{

   hWndTarget=hwnd;

   className=sClassname.ToString();

}

}

else if(guiInfoType==GUIInfoType.guiTextClassParent)

{

    if(sWindowText.ToString()==windowText&&sClassname.ToString().StartsWith(clsStartedWith)&&sParentText.toString()==parentText)

 {

   hWndTarget=hwnd;

}

}

else if(guiInfoType==GUIInfoType.guiClassParent)

{

   if(sClassname.ToString().StartsWith(clsStartedWith)&&sParentText.ToString()==parentText)

{

 hWndTarget=hwnd;

windowText=sWindowText.ToString();

}

}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值