Get word from mouse position in Edit, RichEdit, IE home page, etc.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Get word from mouse position in Edit, RichEdit, IE home page, etc.

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    1

    Get word from mouse position in Edit, RichEdit, IE home page, etc.

    Hello everyone,
    First of all, I must thank you for taking your time to review this article.
    I want to make the feature like below.

    1. move the mouse cursor over edit, richedit or IE page. Then, suppose the mouse cursor is over a word.
    2. I want to get the word.


    It is just the feature that many dictionary softwares have.
    Can you help me how I can do it?
    I am a VC++ developer. So I very appreciate if you let me know what APIs I must use. It is great if you can make me get a good sample.

    Thank you very much.
    Albert :) :) :)

  2. #2
    Join Date
    Jul 2013
    Posts
    2

    Get word under mouse

    Even though this thread is 6 years old, for the rest of you searching for getting a word under the mouse pointer of a richedit control, here is the code (C++):

    Code:
    std::string GetWordUnderMouse(int x, int y)
    {
    	POINT pt = { x, y };
    	int ci = SendMessage(hEdit, EM_CHARFROMPOS, 0, (LPARAM)&pt);
    	if (ci < 0)
    	{
    		return "";
    	}		
    	int lix = SendMessage(hEdit, EM_EXLINEFROMCHAR, 0, ci);
    	int co = ci - SendMessage(hEdit, EM_LINEINDEX, lix, 0);
    	char buffer[1024] = { 0, };
    	((WORD *)buffer)[0] = 1024;
    	SendMessage(hEdit, EM_GETLINE, lix, (LPARAM)buffer);
    	//search for spaces between words
    	int j = co;
    	while (j > 0 && buffer[j] != ' ')
    	{
    		--j;
    	}
    	int k = co;
    	while (k < strlen(buffer) && buffer[k] != ' ')
    	{
    		++k;
    	}
    	//build output buffer
    	std::ostringstream str;
    	for (int i = j + 1; i <= k; ++i)
    	{
    		str << buffer[i];
    	}
    	return str.str();
    }
    This code assumes you have a reference to the richedit control hEdit. X and Y are the LWORD and HIWORD order from the lParam message of WM_MOUSEMOVE

  3. #3
    Join Date
    Jul 2013
    Posts
    2

    Get word under mouse

    Actually, after playing with it a bit more, I have a) optimized the loop, b) checked for termination characters and c) if the character under mouse is a space, return an empty string (not a word there). Here's the updated code:

    Code:
    std::string GetWordUnderMouse(int x, int y)
    {
    	POINT pt = { x, y };
    	int ci = SendMessage(hEdit, EM_CHARFROMPOS, 0, (LPARAM)&pt); //current character index
    	if (ci < 0)
    	{
    		return "";
    	}		
    	int lix = SendMessage(hEdit, EM_EXLINEFROMCHAR, 0, ci); //line index
    	int co = ci - SendMessage(hEdit, EM_LINEINDEX, lix, 0); //current character offset/position
    	char buffer[1024] = { 0, }; //buffer to hold characters (1024 should be enough for the average line)
    	((WORD *)buffer)[0] = 1024; //set first byte to size of buffer as specified in MSDN
    	SendMessage(hEdit, EM_GETLINE, lix, (LPARAM)buffer);
    	if (buffer[co] == ' ' || buffer[co] == '\n')
    	{
    		return ""; //currently at a space character or end of line
    	}	
    	std::ostringstream str; //string stream to hold the resultant word
    	int i = co;
    	bool forward = false; //direction the loop is searching in
    	//search for spaces between words
    	while (i < strlen(buffer))
    	{
    		if (!forward)
    		{
    			//decrease pointer looking for start of line or space
    			--i;			
    			if (buffer[i] == ' ')
    			{
    				++i; //skip over this character
    				forward = true;				
    			}
    			else if (i > 0)
    			{
    				continue; //not at 0 keep looping backwards
    			}
    			else
    			{
    				forward = true; //at 0 so exit this part of the loop
    			}
    		}
    		//increment pointer looking forward for a space		
    		if (buffer[i] == ' ' || buffer[i] == '\n')
    		{
    			break; //break out of the loop
    		}
    		//append current characters to output stream
    		str << buffer[i];
    		++i;
    	}
    	return str.str(); // <- and here is our word
    }
    Please note: this method only deals with single-byte characters, not multi-byte, so unicode will not work too well with this, however, this can be changed.

  4. #4
    Join Date
    May 2013
    Location
    Sydney
    Posts
    6
    Cool! thanks for your share!

Similar Threads

  1. Replies: 0
    Last Post: 07-31-2002, 06:17 PM
  2. Position page on load and keep state?
    By Saul in forum ASP.NET
    Replies: 4
    Last Post: 10-02-2001, 05:38 PM
  3. Replies: 2
    Last Post: 04-18-2001, 03:41 AM
  4. Home Page Icon
    By Lars Brandt in forum Architecture and Design
    Replies: 1
    Last Post: 04-17-2001, 08:06 PM
  5. Replies: 1
    Last Post: 04-05-2001, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center