Qt实战之开发软件数据获取助手(eventFilter处理鼠标按下,event处理鼠标松开)

前段时间,受朋友委托,需要做一个能够获取别人软件文本框中内容的助手。当然这需要调用win api来解决问题。一开始,我想都没想,就用getWindowText()。。。。居然没用,好郁闷。于是查msdn。。发现关于返回值,是这样写的

Return Values

The length, in characters, of the copied string, not including the terminating null character, indicates success. Zero indicates that the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid. To get extended error information, call GetLastError.

This function cannot retrieve the text of an edit control in another application.

原来是不能跨线程获取数据。。。。。。好吧。。那没办法。接着用SendMessage发送WM_GETTEXT。。。测试记事本是完全没有问题的。。。但是对那个软件却不起作用。。。。。于是各种百度。。无果。。好吧。。暂时搁一搁。。后来,同学在用vb弄文本框时发现了EM_GETLINECOUNT和  EM_GETLINE  。至此,这个问题算是解决了。

啰嗦了那么多,进入主题吧。接下来,我们先实现这个助手。spy++这个大同小异。无非就是多了几个api的调用和线程处理问题。好了。先看下这个助手的最终效果图。。

按住那个放大镜后  鼠标的样式会自动改变  然后获取鼠标所在位置的窗口句柄。。选择模式 尝试获取数据。  如果有填写发送数据的目的地信息。。还可以直接发送数据。

好了。我们来重点说说以下几点 :

1) 获取窗口句柄      所用API函数为:  HWNDWindowFromPoint( POINTPoint);  只有获取句柄后我们才能调用SendMessage来获取信息

2)Qt修改鼠标样式  首先,要修改鼠标样式,我们就应该对存放图片的便签进行事件处理 鼠标按下,切换切换label背景图片。同时修改鼠标样式。。代码如下

[cpp] view plaincopy
 
  1. bool MainWindow::eventFilter(QObject *obj, QEvent *event)  
  2. {  
  3.     if (event->type() == QEvent::MouseButtonPress) {  
  4.   
  5.         if (obj == ui->lab_MouseStyle) {  
  6.   
  7.             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);  
  8.             if (mouseEvent->button() == Qt::LeftButton) {  
  9.                 ui->lab_MouseStyle->setPixmap(QPixmap("://image/wait.png"));  
  10.                 // 设置鼠标样式  
  11.   
  12.                 QCursor cursor ;  
  13.                 QPixmap pixmap("://image/searchMouse.png") ;  
  14.                 cursor = QCursor(pixmap,-1,-1);  
  15.                 setCursor(cursor) ;  
  16.   
  17.                 // 修改标志  
  18.                 m_IsMouseStyleChanged = true;  
  19.   
  20.                 // 开启计时器 300 毫秒刷新一次  
  21.                 m_pTimer->start(300);  
  22.   
  23.                 return true;  
  24.             }  
  25.         }  
  26.     }  
  27.     return QWidget::eventFilter(obj, event);  
  28. }  


其中lab_MouseStyle就是软件截图中,存放放大镜的label。。。

同时,当用户松开鼠标时。我们需要还原鼠标样式,并且切换label背景.   这个时候,就要对整个程序进行事件拦截了。而不是对label处理事件了。具体代码如下

[cpp] view plaincopy
 
  1. bool MainWindow::event(QEvent *event)  
  2. {  
  3.     if (event->type() == QEvent::MouseButtonRelease) {  
  4.   
  5.         if (m_IsMouseStyleChanged == true) {  
  6.   
  7.             // 当用户松开鼠标时, 恢复鼠标样式并且重新给鼠标样式标签设置图标  
  8.             setCursor(Qt::ArrowCursor);  
  9.             ui->lab_MouseStyle->setPixmap(QPixmap("://image/searchMouse.png").scaled(QSize(48,48), Qt::KeepAspectRatio));  
  10.             m_IsMouseStyleChanged = false;  
  11.   
  12.             // 停止计时器  
  13.             m_pTimer->stop();  
  14.             return true;  
  15.         }  
  16.     }  
  17.     return QWidget::event(event);  
  18. }  


计时器的做用时通知绘图  绘制窗口的黑色边框。。具体的可以看源码部分。。(绘图部分参考于网络,并非原创。。太久不用gdi了。。有点生疏哈)

   3) 通过句柄获取数据   这里只是谈谈消息的使用   同时有个新问题。怎么处理字符编码问题。。 代码如下(  部分 )

[cpp] view plaincopy
 
  1. // 模式一 : 通过直接发送WM_GETTEXT  
  2.             TCHAR lpRes[20000] = L"";  
  3.   
  4.             int len = ::SendMessage(m_DesHwnd,  WM_GETTEXT  , (WPARAM)10000, (LPARAM)lpRes);  
  5.   
  6.             char str[40240] = "";  
  7.   
  8.             int iLength = <span style="color:#ff0000;">WideCharToMultiByte</span>(CP_ACP, 0, lpRes, -1, NULL, 0, NULL, NULL);  
  9.             <span style="color:#ff0000;">WideCharToMultiByte</span>(CP_ACP, 0, lpRes, -1,str , iLength, NULL, NULL);  
  10.   
  11.             ui->textEdit_Handle->setText(QString::fromAscii(str));  


利用WideCharToMultiByte函数再结合QString::fromAscii我们可以很轻松的转换编码。。。 那么问题来了? QString又该怎么便捷的转换为TCHAR呢?? 

[cpp] view plaincopy
 
  1. // 此函数实现通过句柄获取富文本数据  
  2. QString MainWindow::getDataFromHwnd(HWND hWnd)  
  3. {  
  4.     // 先获取总行数  然后读取数据  
  5.     int lineCount= ::SendMessage(hWnd, EM_GETLINECOUNT, 0, 0);  
  6.   
  7.     if (lineCount >0) {  
  8.   
  9.         TCHAR strRes[102400] = L""; // 使用宽字符  
  10.   
  11.         int  count = 0; // 用来记录总共获取了多少字符  
  12.   
  13.         for (int i = 0; i< lineCount;++i) {  
  14.   
  15.             TCHAR szBuffer[200] = L"";  
  16.             ((WORD*) szBuffer)[0] = 200;  
  17.   
  18.             int len = ::SendMessage(hWnd,  EM_GETLINE  ,(WPARAM)i, (LPARAM)szBuffer);  
  19.   
  20.             ((WORD*) szBuffer)[len] = '';  
  21.   
  22.             for  (int j= 0; j< len;++j)  
  23.                 strRes[count++] = szBuffer[j];  
  24.             strRes[count++] = ' ';  
  25.         }  
  26.   
  27.         // 将宽字符转换为ASCII码字符  
  28.         char str[30240] = "";  
  29.         int iLength = WideCharToMultiByte(CP_ACP, 0, strRes, -1, NULL, 0, NULL, NULL);  
  30.         WideCharToMultiByte(CP_ACP, 0, strRes, -1,str , iLength, NULL, NULL);  
  31.         return QString::fromAscii(str);  
  32.     }  
  33.     return QString("");  
  34. }  


这个主要是针对文本框的处理!思路很简单: 先获取文本框中的行数,然后循环获取每一行数据。。不过此代码中没有采用动态开辟数组。。这个如有需要,自行修改。

  4)到此基本的就实现了。我们讲讲题外话。。怎么实现开机启动的功能。   代码如下

[cpp] view plaincopy
 
  1. //设置为开机启动  
  2.    static void AutoRunWithSystem(bool IsAutoRun) {  
  3.        QSettings *reg = new QSettings(  
  4.                    "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",  
  5.                    QSettings::NativeFormat);  
  6.        QString AppPath = QApplication::applicationFilePath();  
  7.        QString AppName = QApplication::applicationName();  
  8.        if (IsAutoRun) {  
  9.            reg->setValue(AppName, AppPath.replace("/","\"));  
  10.        } else {  
  11.            reg->setValue(AppName, "");  
  12.        }  
  13.    }  



此代码并非原创。参考与游龙大神的代码。有修改。源代码测试有问题。问题出在windows系统注册表的‘’符号问题。。必须先修改相应的符号。。

好了。最后附上已打包的软件和源码。。。仅供学习与交流。,不用于商业用途。 有不足之处,欢迎指出。谢谢。

可执行文件下载地址: http://download.csdn.net/detail/wu5151/8939661

源码文件下载地址:   http://download.csdn.net/detail/wu5151/8939673

本博客地址: http://blog.csdn.net/wu5151

http://blog.csdn.net/wu5151/article/details/47101201

原文地址:https://www.cnblogs.com/findumars/p/5079452.html