QT Combat Development Software Data Acquisition Assistant (EventFilter processing Mouse Press, event handling mouse release)

Source: Internet
Author: User

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
  1. BOOL Mainwindow::eventfilter (Qobject *obj, qevent *event)
  2. {
  3. if (event->type () = = qevent::mousebuttonpress) {
  4. if (obj = = Ui->lab_mousestyle) {
  5. Qmouseevent *mouseevent = static_cast<qmouseevent*> (event);
  6. if (mouseevent->button () = = Qt::leftbutton) {
  7. Ui->lab_mousestyle->setpixmap (Qpixmap ("://image/wait.png"));
  8. //Set mouse style
  9. Qcursor cursor;
  10. Qpixmap Pixmap ("://image/searchmouse.png");
  11. cursor = Qcursor (pixmap,-1,-1);
  12. SetCursor (cursor);
  13. //Modify Flag
  14. m_ismousestylechanged = true;
  15. //Turn on Timer 300 ms Refresh once
  16. M_ptimer->start (300);
  17. return true;
  18. }
  19. }
  20. }
  21. return Qwidget::eventfilter (obj, event);
  22. }


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
  1. BOOL Mainwindow::event (qevent *event)
  2. {
  3. if (event->type () = = qevent::mousebuttonrelease) {
  4. if (m_ismousestylechanged = = true) {
  5. //When the user releases the mouse, restores the mouse style and re-sets the icon to the Mouse style label
  6. SetCursor (Qt::arrowcursor);
  7. Ui->lab_mousestyle->setpixmap (Qpixmap ("://image/searchmouse.png"). Scaled (Qsize (48,48), Qt::  Keepaspectratio));
  8. M_ismousestylechanged = false;
  9. //Stop timer
  10. M_ptimer->stop ();
  11. return true;
  12. }
  13. }
  14. return Qwidget::event (event);
  15. }


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
  1. Mode one: by sending Wm_gettext directly
  2. TCHAR lpres[20000] = L"";
  3. int len =:: SendMessage (M_deshwnd, Wm_gettext, (WPARAM) 10000, (LPARAM) lpres);
  4. char str[40240] = "";
  5. int ilength = <span style="color: #ff0000;"  >WideCharToMultiByte</span> (CP_ACP, 0, Lpres,-1, NULL, 0, NULL, NULL);
  6. <span style="color: #ff0000;"  >WideCharToMultiByte</span> (CP_ACP, 0, Lpres, -1,str, ilength, NULL, NULL);
  7. 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
  1. This function implements fetching rich text data through a handle
  2. QString Mainwindow::getdatafromhwnd (hwnd hwnd)
  3. {
  4. //Get total rows First and then read the data
  5. int linecount=:: SendMessage (hWnd, em_getlinecount, 0, 0);
  6. if (LineCount >0) {
  7. TCHAR strres[102400] = L""; //Use wide characters
  8. int count = 0; //Used to record the total number of characters acquired
  9. For (int i = 0; i< linecount;++i) {
  10. TCHAR szbuffer[200] = L"";
  11. ((word*) szbuffer) [0] = 200;
  12. int len =:: SendMessage (HWnd, Em_getline, (WPARAM) I, (LPARAM) szbuffer);
  13. ((word*) szbuffer) [Len] = ' + ';
  14. For (int j= 0; j< len;++j)
  15. strres[count++] = Szbuffer[j];
  16. strres[count++] = ' \ n ';
  17. }
  18. //Convert wide characters to ASCII characters
  19. char str[30240] = "";
  20. int ilength = WideCharToMultiByte (CP_ACP, 0, Strres,-1, NULL, 0, NULL, NULL);
  21. WideCharToMultiByte (CP_ACP, 0, Strres, -1,str, ilength, NULL, NULL);
  22. return Qstring::fromascii (str);
  23. }
  24. return QString ("");
  25. }


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
  1. Set to boot
  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. }



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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.