Some time ago, commissioned by a friend, you need to be able to get someone else's software text box content in the assistant. Of course this needs to call the win API to fix the problem. At first, I didn't think about it, just use GetWindowText () .... Incredibly useless, good depressed. So check out MSDN. Found about the return value, that's what it says.
Return Values
The length, in characters, of the copied string, not including the terminating null character, indicates success. Zero indicates that the window have 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 a edit control in another application.
It turns out that you can't get data across threads ... All right.. There's no way. Then send Wm_gettext with SendMessage ... Testing Notepad is absolutely no problem ... But it doesn't work for that software ..... So all kinds of Baidu. No fruit. All right.. Put aside for a moment. Later, the classmate in using VB to get text box found Em_getlinecount and Em_getline. At this point, the problem is solved.
So many wordy, go to the subject. Next, we'll implement this assistant first. Spy + + has a similar. It's just a few more API calls and threading issues. All right. First look at this assistant's final.
When you hold the magnifying glass, the mouse style automatically changes and then gets the window handle where the mouse is located ... Select mode to try to get the data. If you have filled in the destination information to send the data. You can also send data directly.
All right. Let's focus on the following points:
1) The API function used to get the window handle is: hwndwindowfrompoint (Point Point); only after we get the handle can we call SendMessage to get the information
2) QT modified mouse style first, to modify the mouse style, we should store the image of the note to handle the event mouse press, toggle the label background image. Modify the mouse style at the same time ... The code is as follows
[CPP]View Plaincopy
- BOOL Mainwindow::eventfilter (Qobject *obj, qevent *event)
- {
- if (event->type () = = qevent::mousebuttonpress) {
- if (obj = = Ui->lab_mousestyle) {
- Qmouseevent *mouseevent = static_cast<qmouseevent*> (event);
- if (mouseevent->button () = = Qt::leftbutton) {
- Ui->lab_mousestyle->setpixmap (Qpixmap ("://image/wait.png"));
- //Set mouse style
- Qcursor cursor;
- Qpixmap Pixmap ("://image/searchmouse.png");
- cursor = Qcursor (pixmap,-1,-1);
- SetCursor (cursor);
- //Modify Flag
- m_ismousestylechanged = true;
- //Turn on Timer 300 ms Refresh once
- M_ptimer->start (300);
- return true;
- }
- }
- }
- return Qwidget::eventfilter (obj, event);
- }
Where Lab_mousestyle is the software, the label that holds the magnifying glass ...
At the same time, when the user releases the mouse. We need to restore the mouse style and toggle the label background. At this time, the entire program will be intercepted by the event. Instead of handling the event for the label. The specific code is as follows
[CPP]View Plaincopy
- BOOL Mainwindow::event (qevent *event)
- {
- if (event->type () = = qevent::mousebuttonrelease) {
- if (m_ismousestylechanged = = true) {
- //When the user releases the mouse, restores the mouse style and re-sets the icon to the Mouse style label
- SetCursor (Qt::arrowcursor);
- Ui->lab_mousestyle->setpixmap (Qpixmap ("://image/searchmouse.png"). Scaled (Qsize (48,48), Qt:: Keepaspectratio));
- M_ismousestylechanged = false;
- //Stop timer
- M_ptimer->stop ();
- return true;
- }
- }
- return Qwidget::event (event);
- }
A black border of the drawing window is notified when the timer is made ... Specific can see the source section: (The drawing section refers to the network, not original.) Not GDI for too long. A little rusty, huh)
3) Get the data through the handle here's just talking about the use of the message and there's a new problem. How to handle character encoding issues: The code is as follows (partial)
[CPP]View Plaincopy
- Mode one: by sending Wm_gettext directly
- TCHAR lpres[20000] = L"";
- int len =:: SendMessage (M_deshwnd, Wm_gettext, (WPARAM) 10000, (LPARAM) lpres);
- char str[40240] = "";
- int ilength = <span style="color: #ff0000;" >WideCharToMultiByte</span> (CP_ACP, 0, Lpres,-1, NULL, 0, NULL, NULL);
- <span style="color: #ff0000;" >WideCharToMultiByte</span> (CP_ACP, 0, Lpres, -1,str, ilength, NULL, NULL);
- Ui->textedit_handle->settext (Qstring::fromascii (str));
With the WideCharToMultiByte function and qstring::fromascii we can easily convert the encoding ... So that's the problem? Qstring again how convenient to convert to TCHAR??
[CPP]View Plaincopy
- This function implements fetching rich text data through a handle
- QString Mainwindow::getdatafromhwnd (hwnd hwnd)
- {
- //Get total rows First and then read the data
- int linecount=:: SendMessage (hWnd, em_getlinecount, 0, 0);
- if (LineCount >0) {
- TCHAR strres[102400] = L""; //Use wide characters
- int count = 0; //Used to record the total number of characters acquired
- For (int i = 0; i< linecount;++i) {
- TCHAR szbuffer[200] = L"";
- ((word*) szbuffer) [0] = 200;
- int len =:: SendMessage (HWnd, Em_getline, (WPARAM) I, (LPARAM) szbuffer);
- ((word*) szbuffer) [Len] = ' + ';
- For (int j= 0; j< len;++j)
- strres[count++] = Szbuffer[j];
- strres[count++] = ' \ n ';
- }
- //Convert wide characters to ASCII characters
- char str[30240] = "";
- int ilength = WideCharToMultiByte (CP_ACP, 0, Strres,-1, NULL, 0, NULL, NULL);
- WideCharToMultiByte (CP_ACP, 0, Strres, -1,str, ilength, NULL, NULL);
- return Qstring::fromascii (str);
- }
- return QString ("");
- }
This is mainly for the processing of text boxes! The idea is simple: get the number of rows in the text box first, and then loop through the data for each row ... However, this code does not take the dynamic Open array: This, if necessary, modifies itself.
4) To this basic is achieved. Let's talk about the topic. How to implement the power-on start function. The code is as follows
[CPP]View Plaincopy
- Set to boot
- static void Autorunwithsystem (bool isautorun) {
- Qsettings *reg = new Qsettings (
- "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
- Qsettings::nativeformat);
- QString AppPath = Qapplication::applicationfilepath ();
- QString AppName = Qapplication::applicationname ();
- if (isautorun) {
- Reg->setvalue (AppName, Apppath.replace ("/","\ \"));
- } Else {
- Reg->setvalue (AppName, "");
- }
- }
This code is not original. Refer to the code of the Great God of Dragon Tour. have been modified. There is a problem with the source code test. The problem is with the ' \ ' sign problem in the Windows system registry. The corresponding symbol must be modified first:
All right. Finally attach the packaged software and source code ... For learning and communication only. and is not intended for commercial use. There are shortcomings, welcome to point out. Thank you.
Executable file: http://download.csdn.net/detail/wu5151/8939661
Source file: http://download.csdn.net/detail/wu5151/8939673
This blog address: http://blog.csdn.net/wu5151
http://blog.csdn.net/wu5151/article/details/47101201
QT Combat Development Software Data Acquisition Assistant (EventFilter processing Mouse Press, event handling mouse release)