-
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 :) :) :)
-
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
-
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.
-
Cool! thanks for your share!
Similar Threads
-
By Aram Bohdjelian in forum Mobile
Replies: 0
Last Post: 07-31-2002, 05:17 PM
-
Replies: 4
Last Post: 10-02-2001, 04:38 PM
-
Replies: 2
Last Post: 04-18-2001, 02:41 AM
-
By Lars Brandt in forum Architecture and Design
Replies: 1
Last Post: 04-17-2001, 07:06 PM
-
Replies: 1
Last Post: 04-05-2001, 10: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|