Thread: Grabbing Text from a Window

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Grabbing Text from a Window

    Hi, I am trying to modify some code that grabs the contents of a chat window. Unfortunately the code is for MSN Messenger, and I'd like to try it with AIM. Basically, when executed, it takes the text from the window, copies it to the clipboard and then prints it out.

    It basically gets the HWND by checking whether there is a MSN Conversation window open. This is done by using the EnumWindows() API. If it's open, it gets its HWND and checks its child windows to find a window having the window class 'RichEdit20W' or 'RichEdit20A'; this is done by using the EnumChildWindows() API. After getting the HWND of the conversation text window, it sends WM_COPY a message.

    The problem is that AIM doesn't use RichEdit20W, it uses AteWindow. In Spy++ the hierarchy looks like:

    [top_level] "ScreenName - Instant Message" AIM_IMessage
    [child] "AteWindow" WndAte32Class
    [childchild] '"' Ate32Class

    The code responsible for finding the code is:

    Code:
    BOOL CALLBACK CMSNChatTextDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    	TCHAR buff[1000];
    	int buffsize=100;
    	HWND hMSNWnd;
    	hMSNWnd=NULL;
    		
    	::GetWindowText(hwnd,buff,buffsize);
    	if (strlen(buff)<1)
    		return TRUE;
    
    	string strTemp(buff);
    
    	//CHECK for MSN MESSENGER CHAT WINDOW HERE
    	string::size_type pos=0;
    	pos=strTemp.rfind(" - Instant Message",strTemp.length());
    	if (pos!=-1)
    		EnumChildWindows(hwnd,ChildWndProc,0);
    
    	return TRUE;
    }
    
    BOOL CALLBACK CMSNChatTextDlg::ChildWndProc(HWND hwnd, LPARAM lParam)
    {
    	static int i=0;
    	LPTSTR    lptstr; 
    	HGLOBAL   hglb; 
    	char wndowclass[CLASS_SIZE];
    
    	if (GetClassName(hwnd,wndowclass,CLASS_SIZE)==0)
    		return TRUE;
    
    	string strTemp(wndowclass);
    // THE OLD MSN MESSENGER USED THIS WINDOW
    // I suppose I would change this string to find Ate32Class?
    	if ((strTemp==string("RichEdit20W"))) || (strTemp==string("RichEdit20A")))
    	{
    		::SendMessage(hwnd,EM_SETSEL,0,-1); //start selecting
    		::SendMessage(hwnd,WM_COPY,0,0);
    		::SendMessage(hwnd,EM_SETSEL,-1,0); //end selecting
    
    		if (!IsClipboardFormatAvailable(CF_TEXT)) 
    			return TRUE; 
    
    		if (! ::OpenClipboard(NULL)) 
    			return TRUE; 
    
    		hglb = GetClipboardData(CF_TEXT); 
    		if (hglb != NULL) 
    		{
    			lptstr = (LPTSTR)GlobalLock(hglb); 
    			GlobalUnlock(hglb); 
    			EmptyClipboard();
    			CloseClipboard();
    			
    			pChatText->SetWindowText(lptstr);
    			
    			return FALSE;
    		}
    	}
    	return TRUE;
    }
    Thanks for any input if you can help

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    Original

    Just to give credit where credit is due, the original is here

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb A Few Notes ...

    Well, I have tried this before, so I know what you're doing. When you do this you will need to find the [childchild] window. You will have to send the WM_GETTEXT message to this window. But, it will return in HTML format (<font=**>), etc. I have not found a way to get JUST plain text. I hope this helps you out even a little bit.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    18
    Ok, so can I go straight to this:



    if (GetClassName(hwnd,wndowclass,CLASS_SIZE)==0)
    return TRUE;

    string strTemp(wndowclass);
    if ((strTemp==string("Ate32Class")));

    or do i have to find the [child] window first in order to find the [childchild] which is the next one in the tree?

  5. #5
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Well, not exactly ...

    When I did mine, I didn't even use "EnumWindows". I just jumped straight into it. Here's my code:

    Code:
    m_pChild = m_pAIM->GetNextDlgGroupItem(m_pAIM);
    m_pChild = m_pChild->GetNextDlgGroupItem(m_pAIM);
    
    // Check to see if the window was found
    if (! (::IsWindow(m_pChild->m_hWnd))) 
    {
        // TODO: Show error message
        MessageBox("Error finding chat window child!\n");
    }
    
    // 'm_pChild' : 'CWnd*' for the child control(s)
    // 'm_pAIM' : 'CWnd*' for the Instant Message window itself
    I think that's all you will need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  3. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  4. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM