Send a message to the specified window with spy ++-1

Source: Internet
Author: User

Let's talk about the background of my work:

I want to find a word database with a Chinese translation for each word, but I haven't found it online for a long time. I don't know if the keyword I entered is incorrect. There is no way to create a word database by yourself. If you want to generate a word library by yourself, the first problem is translation. First, find the commonly used word table, which is very simple. The key is to translate each word in the word list into Chinese. I certainly cannot manually enter the Chinese meaning of each word, so I want to use the translation software to automatically complete the process of translating words into Chinese. In this way, I need to send a message to the translation software to automatically enter words and query words. The biggest headache here is "sending messages to the designated window of the translation software". I found a lot of information on the Internet and found it scattered. I had a hard time learning and I had to find it for a while, after solving this problem, I thought it was necessary to sort it out to make it easier for myself and for others. (Note: I am a normal user. please correct me if any error occurs in this article. Thank you !)

 

1. translation software (lingoes linger)

 

2. Find the text box (location A), which is the place where we enter the word, so the message of our input word is sent to this window (the text box is also a window)

First, start spy ++. I use the spy ++ 8.0 that comes with vs2005,

 

 

After spy is started, click the 5th button on the toolbar, the "window telescope" icon, or press Ctrl + F to open a search window interface.

 

 

You can drag the sight icon in the window, drag the icon to the external frame of our translation software interface, and release the sight. The following information is displayed:

 

 

: The translation software window has been selected by spy. The search window displays information about the translation software. What is useful here is the title ". Click "OK" to view more detailed attribute information. Aiming at the main window here is mainly to get the "title" information, so there is no need to view details. Detailed information is required when you search for the input window (a character window. The reason for obtaining the title of the translation software is that we need to call the API to find the main window. After finding the main window, we can find the subwindow under the main window, that is, the word input box (location a) we are looking for. Because the title of a software is usually not changed, I chose to find the main window through the title. There is a handle value above the title on the screen. This is only the "Number" assigned to the translation software after it is run ", this number will not change when the translation software runs, but if you close the translation software and run it again, the number assigned by the system to the translation software may be different from the number assigned previously. This is very understandable, just like our ADSL Internet access. When we use the network, the service provider will provide us with an IP address. If we do not use the network, this IP address may be assigned to other users by the service provider. When we use the network again next time, the service provider will allocate an IP address to us. However, it may be the same or different if the IP address is the same as the previous IP address.

 

The following code shows the main window of the translation software based on the title: (VC ++ 2005 MFC)

 

 

 

  1. /* The system calls this function every time a subwindow is found in the list of subwindows. hwnd is the handle of the subwindow, and lparam is the value we pass when calling enumchildwindows */
  2. Bool callback enumproc (hwnd, lparam)
  3. {
  4. Return true;
  5. }
  6. /* The dialog box contains a button. click the button to start searching for the main window and query all the subwindows in the main window. */
  7. Void cspydlg: onbnclickedbutton1 ()
  8. {
  9. // Search for the window pointer titled "lingoes lingers"
  10. Cwnd * w = findwindow (null, "lingoes Linger ");
  11. // Parent is the handle of the main window. If it is converted into hexadecimal format, it is equal to 0004092e.
  12. Hwnd parent = W-> m_hwnd;
  13. // Lists all subwindows in the main window, including subwindows.
  14. Enumchildwindows (parent, enumproc, (lparam) This );
  15. }

Here we mainly use two mfc api functions: findwindow and enumchildwindows.

Find the findwindow information on msdn as follows:

static CWnd* PASCAL FindWindow(   LPCTSTR lpszClassName,   LPCTSTR lpszWindowName );

Parameters
Lpszclassname// WndclassStruct pointer. If this parameter is null, all matches)

Points to a null-terminated string that specifies the window's class name (WndclassStructure). IfLpclassnameIsNull, All class names match.

 

Lpszwindowname// Window name. If it is null, all values match. (This is a parameter we use)

Points to a null-terminated string that specifies the window name (the window's title). IfLpwindownameIsNull, All window names match.

Return Value // if the return value is null, the specified window is not found.

Identifies the window that has the specified class name and window name. It isNullIf no such window is found.

 

 

At the end of the Code for searching for the main window, I called enumchildwindows (parent, enumproc, (lparam) This); to enumerate all subwindows in the main window.

The function is described as follows in msdn:

 

BOOL EnumChildWindows(      
    HWND hWndParent,    WNDENUMPROC lpEnumFunc,    LPARAM lParam);

Parameters

 

Hwndparent// Parent window handle, which is the parent we found

[In] handle to the parent window whose child windows are to be enumerated. If this parameter is null, this function is equivalent to enumwindows.Windows 95/98/me: HwndparentCannot be null.

 

Lpenumfunc// Callback function pointer. Every time a subwindow is found, the system will call this function for processing. You need to write this function by yourself.

// For the specific function Format, see enumchildproc. Here we write the function name enumproc, and the code is above.
[In] pointer to an application-defined callback function. For more information, see enumchildproc.

 

Lparam// We will pass an additional parameter by ourselves. Here we will pass a window pointer over
[In] specifies an application-defined value to be passed to the callback function.

 

 

 

 

 

 

 

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.