当前位置: 代码迷 >> PB >> pb怎么实现其他软件的数据抓取
  详细解决方案

pb怎么实现其他软件的数据抓取

热度:52   发布时间:2016-04-29 08:17:25.0
pb如何实现其他软件的数据抓取
如题,不是通过数据库抓取,而是通过其他软件的界面抓取,最好有具体的例子。

------解决方案--------------------
要抓取其他软件的什么信息呢?
------解决方案--------------------
你是想截个图还是在光标下的文本信息?
------解决方案--------------------
提供一个获取指定窗口上所有的文字的方法,供参考:
1.调用API获得指定窗口的句柄
2.发送WM_PRINT消息给这个窗口句柄,并打印在内存DC中.(相当于窗口截图)
3.将内存DC保存为一个bmp临时文件
4.调用OCR对这个bmp上的文本进行识别并返回一个字符串,这个字符串就是这个窗口中你看到的所有的文字
5.将临时bmp删除
------解决方案--------------------
之前上面提到的方面在我自己尝试后发现有很多受限的地方,现提供API方法获取指定窗口的文字信息:
C/C++ code
添加API函数声明:Function ulong FindWindow(ref string lpClassName,ref string lpWindowName) LIBRARY "user32.dll" ALIAS FOR "FindWindowA;ansi"Function ulong GetWindow(ulong hwnd,ulong wCmd) LIBRARY "user32.dll"Function ulong FindWindowEx(ulong hWnd1,ulong hWnd2,ref string lpsz1,ref string lpsz2) LIBRARY "user32.dll" ALIAS FOR "FindWindowExW"Function ulong SendMessage(ulong hwnd,ulong wMsg,ulong wParam,ref ulong lParam) LIBRARY "user32.dll" ALIAS FOR "SendMessageA;ansi"FUNCTION long SendMessageString( long hWindow, uint Msg, ulong wParam, Ref string szText ) Library 'user32' alias for 'SendMessageW'Function ulong GetClassName(ulong hwnd,ref string lpClassName,ulong nMaxCount) LIBRARY "user32.dll" ALIAS FOR "GetClassNameW"Function ulong GetNextWindow(ulong hwnd,ulong wFlag) LIBRARY "user32.dll"测试按钮Clicked事件:Constant Int WM_GETTEXT=13Constant Int WM_GETTEXTLENGTH=14Constant Int GW_HWNDNEXT=2Constant Int GW_CHILD=5Long   ll_HParent,ll_retUlong ll_HSourceText,ll_findFirstuLong ll_TextLengthString   ls_Class,ls_winTitleString ls_getText,ls_RetSetNull(ll_findFirst)SetNull(ls_winTitle)SetNull(ls_Class)ls_winTitle   = "窗口标题"ll_HParent   =   FindWindow(ls_Class,ls_winTitle)//获取指定窗口中的所有文本和输入信息ll_HSourceText = GetWindow(ll_HParent,GW_CHILD)if ll_HSourceText <> 0 then    //ls_Class = Space(10)    //GetClassName(ll_HSourceText,ls_Class,10)    //if ls_Class = "Button" or ls_Class="Edit" or ls_Class = "ComboBox"  then    ////过滤类型        ll_TextLength = 0        ll_TextLength = SendMessage(ll_HSourceText,WM_GETTEXTLENGTH,0,ll_TextLength)    //取字符长度        ls_Ret=Space(ll_TextLength)        SendMessageString(ll_HSourceText, WM_GETTEXT, ll_TextLength, ref ls_Ret)                //获取字符        if Len(ls_Ret) > 0 then            ls_getText += ls_Ret + "~n"        end if    //end if    ll_HSourceText = GetWindow(ll_HSourceText,GW_HWNDNEXT)    do while ll_HSourceText > 0        //ls_Class = Space(10)        //GetClassName(ll_HSourceText,ls_Class,10)        //if ls_Class = "Button" or ls_Class="Edit"  then                 ll_TextLength = 0            ll_TextLength = SendMessage(ll_HSourceText,WM_GETTEXTLENGTH,0,ll_TextLength)            ls_Ret=Space(ll_TextLength)            SendMessageString(ll_HSourceText, WM_GETTEXT, ll_TextLength, ref ls_Ret)            if Len(ls_Ret) > 0 then                ls_getText += ls_Ret + "~n"            end if        //end if        ll_HSourceText = GetWindow(ll_HSourceText,GW_HWNDNEXT)    loop    messagebox("",ls_getText)end if
------解决方案--------------------
修改了一下,多加了一个字符的长度
C/C++ code
添加API函数声明:Function ulong FindWindow(ref string lpClassName,ref string lpWindowName) LIBRARY "user32.dll" ALIAS FOR "FindWindowA;ansi"Function ulong GetWindow(ulong hwnd,ulong wCmd) LIBRARY "user32.dll"Function ulong FindWindowEx(ulong hWnd1,ulong hWnd2,ref string lpsz1,ref string lpsz2) LIBRARY "user32.dll" ALIAS FOR "FindWindowExW"Function ulong SendMessage(ulong hwnd,ulong wMsg,ulong wParam,ref ulong lParam) LIBRARY "user32.dll" ALIAS FOR "SendMessageA;ansi"FUNCTION long SendMessageString( long hWindow, uint Msg, ulong wParam, Ref string szText ) Library 'user32' alias for 'SendMessageW'Function ulong GetClassName(ulong hwnd,ref string lpClassName,ulong nMaxCount) LIBRARY "user32.dll" ALIAS FOR "GetClassNameW"Function ulong GetNextWindow(ulong hwnd,ulong wFlag) LIBRARY "user32.dll"测试按钮Clicked事件:Constant Int WM_GETTEXT=13Constant Int WM_GETTEXTLENGTH=14Constant Int GW_HWNDNEXT=2Constant Int GW_CHILD=5Long   ll_HParent,ll_retUlong ll_HSourceText,ll_findFirstuLong ll_TextLengthString   ls_Class,ls_winTitleString ls_getText,ls_RetSetNull(ll_findFirst)SetNull(ls_winTitle)SetNull(ls_Class)ls_winTitle   = "窗口标题"ll_HParent   =   FindWindow(ls_Class,ls_winTitle)//获取指定窗口中的所有文本和输入信息ll_HSourceText = GetWindow(ll_HParent,GW_CHILD)if ll_HSourceText <> 0 then    //ls_Class = Space(10)    //GetClassName(ll_HSourceText,ls_Class,10)    //if ls_Class = "Button" or ls_Class="Edit" or ls_Class = "ComboBox"  then    ////过滤类型        ll_TextLength = 0        ll_TextLength = SendMessage(ll_HSourceText,WM_GETTEXTLENGTH,0,ll_TextLength) + 1    //取字符长度        ls_Ret=Space(ll_TextLength)        SendMessageString(ll_HSourceText, WM_GETTEXT, ll_TextLength, ref ls_Ret)                //获取字符        if Len(ls_Ret) > 0 then            ls_getText += ls_Ret + "~n"        end if    //end if    ll_HSourceText = GetWindow(ll_HSourceText,GW_HWNDNEXT)    do while ll_HSourceText > 0        //ls_Class = Space(10)        //GetClassName(ll_HSourceText,ls_Class,10)        //if ls_Class = "Button" or ls_Class="Edit"  then                 ll_TextLength = 0            ll_TextLength = SendMessage(ll_HSourceText,WM_GETTEXTLENGTH,0,ll_TextLength) + 1            ls_Ret=Space(ll_TextLength)            SendMessageString(ll_HSourceText, WM_GETTEXT, ll_TextLength, ref ls_Ret)            if Len(ls_Ret) > 0 then                ls_getText += ls_Ret + "~n"            end if        //end if        ll_HSourceText = GetWindow(ll_HSourceText,GW_HWNDNEXT)    loop    messagebox("",ls_getText)end if
  相关解决方案