Click to See Complete Forum and Search --> : Cursor Position
geoffba
September 12th, 2003, 11:45 AM
I need to get the current position of the cursor from a Rich Edit Control, however I do not know how. Basically I need to retrieve the current line the cursor is on, and the current x axis position. Does anyone know how to perform this??
Thanks in advance.
hankdane
September 12th, 2003, 01:32 PM
Use EM_GETSEL or EM_EXGETSEL to get the current character position or selection.
Use EM_LINEFROMCHAR to figure out what line it's on.
geoffba
September 12th, 2003, 05:25 PM
Ok so I think I have that stuff working but what I need is to recieve the x and y coordinates. I can get y from the get line number, but how do I get only x and then set only x??
hankdane
September 12th, 2003, 06:29 PM
Geoff,
I think you got the metaphor wrong. It's not a coordinate system. The contents of a Rich Edit control is essentially a document, and position is denoted by which characters are selected.
To advance the selection one character, use EM_SETSEL([Result from EM_GETSEL] + 1). You should handle multi-character selections as well, it's not clear from your posts what the best way to do that would be in your situation.
geoffba
September 12th, 2003, 06:33 PM
I am attempting to look for a backspace character code (from the incomming data on the serial port) and then move the cursor back a space print the incomming space code and move the cursor back another space.
My problem now is that I do not understand how to use the EM_SETSEL and EMGETSEL in combination. Since it would be foolish for me to describe everything to you here is my code but it does not work like this.
if(data[0]==8)
{
WPARAM start;
LPARAM end;
SendMessage(
hTerm,
EM_GETSEL,
start,
end);
start--;
end--;
SendMessage(
hTerm,
EM_SETSEL,
start,
end);
}
geoffba
September 15th, 2003, 11:09 AM
I did some more testing over the weekend and I think I understand how the two messages work. The EM_SETSEL is working for me but I cannot get the EM_GETSEL to return anything but a zero. I assume this is because I have nothing highlighted on in the control. That makes sens to me but what I need to get is the position of the cursur when there is nothing highlighted. I am going to look up the EM_EXGETSEL that you mention to see if it will do the trick. thanks again
geoffba
September 15th, 2003, 11:15 AM
So the EM_EXGETSEL does not return position either just 0,0 if nothing is selected. Do you know of a method to get the cursor location, or of how to move the curcor one space backwords??
hankdane
September 15th, 2003, 05:23 PM
First, just to make sure we are talking about the right thing:
You keep refferring 'cursor' position, but I think you mean the caret position. It's confusing because there are three different positions in Windows relating to input: The mouse cursor, a potential DOS cursor if you are in a console window, and a potential caret position if you are in a window that accepts character input. I think you are referring to the latter.
On EM_GETSEL, I'm not sure why you expect something other than zero when nothing is selected. By default, the caret is at the beginning of the text. So both the start and end is at the first character, which is zero. I would expect EM_GETSEL to return zero when 'nothing' is selected.
(There is always a selection, but not always a visible one...)
In regards to your code, you are not initializing your variables, and you are not using the return value from EM_GETSEL. Try this instead:
{
WPARAM start = 0;
LPARAM end = 0;
if (-1 != SendMessage(
hTerm,
EM_GETSEL,
(WPARAM)&start,
(LPARAM)&end);
{
//start++;
end++;
SendMessage(
hTerm,
EM_SETSEL,
start,
end);
}
}
I'm advancing a character here instead of retreating one. If you want to retreat, you should also check for position zero; if you are at position zero, it does not make much sense to go back. The exact results depends on the semantics you decide for that situation.
I commented out the line that advances 'start,' this way, it's easier to see if the code is changing the selection. Also note that both the caret and a selected range is only visible when the window (edit control) has the focus. You should uncomment the line again for your purpose.
geoffba
September 15th, 2003, 08:30 PM
that worked for me and now the code is running the way I wanted it to. Thank you very much, your help was greatly appreciated.
geoffba
September 15th, 2003, 10:15 PM
So this will work fine for deleting a single character, but how would you go about deleting a complete line, without running onto the previous line?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.