Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Screen Cursor Position on RichEdit

611 views
Skip to first unread message

espvida

unread,
Jul 21, 2001, 7:18:43 AM7/21/01
to
The TRichEdit propriety CaretPos gives the position of the cursor in the
RichEdit component, so that if there is a large RichEdit and it is being
shown on the screen at its end, the CaretPos is very large, even if the
cursor is on the top of the screen. We know with CaretPos if cursor is
on the last line of RichEdit, but not where it is on the screen, if on
top or lower. How is possible to know the position of a RichEdit cursor
on the screen, independently if it is on the beginning or the end of
RichEdit?

Thank you for an answer.

Antonio Donato

Doctor Fission

unread,
Jul 21, 2001, 9:35:40 AM7/21/01
to
I may not have understood your question (mostly cuz i don't use CaretPos)
but I think the answer is because we can tell the total length of the
document and CaretPos tells what position the cursor is in in relation to
the whole document. I don't think that answers your question tho......
sorry, just trying to seem less newbie.

Hope it helped,
~Fission


Robert Dunn

unread,
Jul 21, 2001, 12:08:30 PM7/21/01
to
Hi, Antonio.

You can use the WinAPI EM_CHARFROMPOS and EM_POSFROMCHAR messages to
determine where, within the RE, a particular character position is
displayed. You can use this in combination with the SelStart position
to get the caret location and with the client window coordinates to get
the character offset of the topmost visible character. Is this what you
are looking for? If not, you might want to check out
http://home.att.net/~robertdunn/Yacs.html and see if the answer is
there.

robert

espvida

unread,
Jul 22, 2001, 1:18:07 PM7/22/01
to
Thank you for your answer. My question is because I need to know in what
screen coordinates the cursor is at some moment for, when opening a new and
more little form over that in which is the Rich Edit Component the new form
does not open over the selected text in the Rich Edit if there is any. The
CaretPos, unhappily, says where the cursor is in the Rich Edit Component, not
in the Screen Coordinates.
How do we do to know the Screen Coordinates of the cursor?

Thank you.

Antonio Donato

Doctor Fission ha scritto:

espvida

unread,
Jul 22, 2001, 1:27:00 PM7/22/01
to
Mr. Robert Dunn,

thank you for your answer. I want to know the Screen Coordinates of the
SelStart in order to allow another form to open over the Rich Edit without
cover the SelStart position. The CaretPos gives the position of Caret on the
document, and not on the screen. I did not understand how it is possible to be
done from your explanation. Thank you for your attention.

Antonio Donato

Robert Dunn ha scritto:

Robert Dunn

unread,
Jul 23, 2001, 7:08:24 PM7/23/01
to
Hi, Antonio.

Ok, I think that I understand. You want to get the screen coordinates
(in device units = pixels) of the starting position of the currently
selected text. I do not use TRichEdit::CaretPos, so I will offer the
following that *should* work (untested, of course).

POINT pt;
// get coordinates in client units (relative to client window)
::SendMessage(re->Handle, EM_POSFROMCHAR, (WPARAM) &pt, (LPARAM)
re->SelStart);
// convert to screen coordinates
::ClientToScreen(re->Handle, &pt);
// pt now contains screen coordinates (use pt.x & pt.y to get each)

Of course, keep in mind that the selected text may not be visible on the
screen. That is, it may be scrolled out of view in the Rich Edit
window. I *think* that, in this case, you still get valid values
relative to the client window (0, 0), but they may be completely outside
of the visible screen range.

HTH.

robert

espvida

unread,
Jul 24, 2001, 9:38:29 PM7/24/01
to
Mr. Dunn,

I tried to do what you taught me, but I verified that the method ClientToScreen has
still the same problem that in previous messages. It really does not gives the
absolute position of the cursor on the screen, but the position of the cursor in the
RichEdit document added to the position of the beggining of the RichEditDocument.
Thus, if you have an A in a RichEdit scrolling near the MainMenu and its y position
converted to Screen is, for example, 304, and you scroll the document till this same
A reaches the bottom of the screen, its y position continues to be 304, the height
of the Menu plus the length of the RichEdit from its beggining to the A. What I
would like to know is if this A is the selected character, where it lies know, if it
is on the top of the screen, on the middle or on the bottom, in order that when a
new form opens over the Rich Edit, it may be opened in a y position that does not
cover the selected text. It can be easily done with horizontal coordinates, but I
would like to know how to manage it on their vertical coordinates.

If there is an answer to this question, I thank very much the attention given to it.

Robert Dunn

unread,
Jul 25, 2001, 6:58:38 PM7/25/01
to
Hi, Antonio.

espvida wrote:
>
> Mr. Dunn,
>
> I tried to do what you taught me, but I verified that the method ClientToScreen has
> still the same problem that in previous messages. It really does not gives the
> absolute position of the cursor on the screen, but the position of the cursor in the
> RichEdit document added to the position of the beggining of the RichEditDocument.
> Thus, if you have an A in a RichEdit scrolling near the MainMenu and its y position
> converted to Screen is, for example, 304, and you scroll the document till this same
> A reaches the bottom of the screen, its y position continues to be 304, the height
> of the Menu plus the length of the RichEdit from its beggining to the A. What I
> would like to know is if this A is the selected character, where it lies know, if it
> is on the top of the screen, on the middle or on the bottom, in order that when a
> new form opens over the Rich Edit, it may be opened in a y position that does not
> cover the selected text. It can be easily done with horizontal coordinates, but I
> would like to know how to manage it on their vertical coordinates.
>
> If there is an answer to this question, I thank very much the attention given to it.

Oh, now I understand. The coordinates are always relative to the
"virtual RE text space." (For clarity, the "caret" is the insertion
point in the RE; "cursor" usually means the mouse cursor.)

Not a problem. Simply use EM_GETFIRSTVISIBLELINE to get the character
offset of the line at the top of the visible window. Use EM_POSFROMCHAR
to get the virtual offset of this line. Subtract this value from the
value you already have for the caret position. If negative, the is
scrolled above the window. If positive, the caret is on/below the top
line (but possibly not visible, i.e., scrolled below the bottom of the
visible window).

Whatever the value, add it to the RE Top property (this gives client
coordinates). Use ClientToScreen() to convert this value to screen
coordinates. This should give what you want (keeping in mind that the
result may be offscreen).

Does that solve it?

robert

espvida

unread,
Jul 26, 2001, 10:29:19 PM7/26/01
to
Mrs. Dunn,

thank you very much for the time you have spend with my questions. I think that your
answer now is what I needed, but only in two days I will bi able to try it to verify. I
would like meanwhile to ask you were I may see a list of Windows Messages? I have some
books about C, C++ and C++Builder, and in them there are comments about how to use these
messages, but there is not a list of the available ones.

Robert Dunn ha scritto:

Robert Dunn

unread,
Jul 27, 2001, 11:13:31 AM7/27/01
to
Hi, Antonio.

espvida wrote:
>
> thank you very much for the time you have spend with my questions. I think that your
> answer now is what I needed, but only in two days I will bi able to try it to verify. I
> would like meanwhile to ask you were I may see a list of Windows Messages? I have some
> books about C, C++ and C++Builder, and in them there are comments about how to use these
> messages, but there is not a list of the available ones.

There are hundreds, if not thousands, of messages. It is usually easier
to look up a control (such as Rich Edit) in the WinAPI help that ships
with BCB. Usually the help about the control will list messages
specific to the control. If you really want to look at a list of
messages, you can open the WinAPI help and type "WM_" into the index.
This will show you general windows messages. Other messages for
specific types of controls are prefixed with different characters, e.g.,
EM_ for edit messages (for Edit and Rich Edit controls).

HTH.

robert

espvida

unread,
Aug 1, 2001, 9:33:32 PM8/1/01
to
Mr. Robert Dunn,

I was out this lastweek, for that the delay on answer. Unhappily I did not find any section on
the C++Builder Help with the Messages of Windows API. Perhaps they are not on my copy, or I
must know something more to find them. If there is another place to find them and you know
where, I'd like to see them. About the message EM_GETFIRSTVISIBLELINE that you mentioned in a
past letter, are there not some parameters to send with it? I think that it was on that help I
do not find where they would be found. Thank you very much again.

Antonio Donato

Robert Dunn ha scritto:

> Hi, Antonio.

Robert Dunn

unread,
Aug 2, 2001, 6:59:04 PM8/2/01
to
Hi, Antonio.

espvida wrote:
>
> Mr. Robert Dunn,
>
> I was out this lastweek, for that the delay on answer. Unhappily I did not find any section on
> the C++Builder Help with the Messages of Windows API. Perhaps they are not on my copy, or I
> must know something more to find them. If there is another place to find them and you know
> where, I'd like to see them. About the message EM_GETFIRSTVISIBLELINE that you mentioned in a
> past letter, are there not some parameters to send with it? I think that it was on that help I
> do not find where they would be found. Thank you very much again.

Sorry, I should have mentioned that the WinAPI help is in a separate
help file. Mine is in "C:\Program Files\Common Files\Borland
Shared\MSHelp\win32.hlp". If you do not find it there, search your
drive for win32.hlp. Also, the MS developer support site has the
information online. Go to www.microsoft.com. In the toolbar on the
left, under Information For, select developer. Then select MSDN
library. Then use the search feature on the left.

And, no, this message has no parameters (or, more accurately, the
parameters are 0's). The return value is the zero-based line number.

HTH.

robert

espvida

unread,
Aug 14, 2001, 9:05:42 PM8/14/01
to
Mr. Robert Dunn,

thank you again for your answer. I found the Windows help where you mentioned it and am going to
examine it very carefully.

Antonio Donato

Robert Dunn ha scritto:

> Hi, Antonio.

0 new messages