Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do u know better way of tracking CaretPos?

Status
Not open for further replies.

StoneColdCrazy

Programmer
Jun 30, 2000
11
0
0
US
In TMemo or TRichEdit, when u write a text editor, for example u want to display the caret position in your status bar, the way I currently doing it is like this:<br><br>// in my RichEdit-&gt;OnKeyDown event handler<br>//-----------------------------------------------<br>/*some params r missing*/<br>void __fastcall TMainForm::OnRichEditKeyDown(WORD& Key)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;SendMessage(RichEdit-&gt;Handle, WM_KEYUP, Key, 0);<br>&nbsp;&nbsp;}<br><br>// in my RichEdit-&gt;OnKeyUp event handler<br>//------------------------------------------------<br>/*some params r missing*/<br>void __fastcall TMainForm::OnRichEditKeyUp(/**/)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;StatusBar-&gt;Panels-&gt;Items[0]-&gt;Text =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String(++RichEdit-&gt;CaretPos.y) +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;:&quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String(++RichEdit-&gt;CaretPos.x);<br>&nbsp;&nbsp;}<br><br>WM_KEYDOWN occurs when the key is held down, even when for a while.&nbsp;&nbsp;WM_KEYUP occurs only when the key is released, so if u held down a key for a while many WM_KEYDOWN events will occur, but only one WM_KEYUP (at the end) when u released the key.&nbsp;&nbsp;TRichEdit and TMemo update CaretPos after WM_KEYDOWN, but before WM_KEYUP.&nbsp;&nbsp;So WM_KEYUP is the place to query CaretPos, and in onKeyDown event handler we send a message WM_KEYUP, so that TRichEdit update the carret and call OnkeyUp event handler.&nbsp;&nbsp;This way we can track CaretPos event when the user holds down the key for a while...<br><br>Is there any better way to do this?&nbsp;&nbsp;~~StonE~~
 
&nbsp;&nbsp;&nbsp;&nbsp;Try this, it came from one of Borland's FAQs...<br><br>For TMemo this code will work:<br>&nbsp;&nbsp;&nbsp;&nbsp;TPoint Point;<br>&nbsp;&nbsp;&nbsp;&nbsp;GetCaretPos(&Point);<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD both;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;both = Memo1-&gt;Perform(EM_CHARFROMPOS, 0, MAKELPARAM(Point.x, Point.y));<br>&nbsp;&nbsp;&nbsp;&nbsp;int indexLine&nbsp;&nbsp;&nbsp;= HIWORD(both);<br>&nbsp;&nbsp;&nbsp;&nbsp;int indexLength = LOWORD(both) - Memo1-&gt;Perform(EM_LINEINDEX, -1, 0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;indexLine++;<br>&nbsp;&nbsp;&nbsp;&nbsp;indexLength++;<br>&nbsp;&nbsp;&nbsp;&nbsp;char RowCol[10] = &quot; &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;sprintf(RowCol, &quot;%d:%d&quot;, indexLine, indexLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;StatusBar1-&gt;Panels-&gt;Items[4]-&gt;Text = RowCol;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>In TRichEdit, the previous code would produce an access violation when using EM_CHARFROMPOS.<br><br>This code will work for TRichEdit:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;TPoint Point;<br>&nbsp;&nbsp;&nbsp;&nbsp;GetCaretPos(&Point);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD both = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;both = MAKELPARAM(Point.x, Point.y);<br>&nbsp;&nbsp;&nbsp;&nbsp;int indexLine = RichEdit1-&gt;Perform(EM_LINEFROMCHAR, -1, 0);<br>&nbsp;&nbsp;&nbsp;&nbsp;int indexLength = (LOWORD(both)/8) + RichEdit1-&gt;SelLength;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;indexLength++;<br>&nbsp;&nbsp;&nbsp;&nbsp;indexLine++;<br>&nbsp;&nbsp;&nbsp;&nbsp;char RowCol[10] = &quot; &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;sprintf(RowCol, &quot;%d:%d&quot;, indexLine, indexLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;StatusBar1-&gt;Panels-&gt;Items[4]-&gt;Text = RowCol;<br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
2ffat,<br><br>Where do I stick that code?&nbsp;&nbsp;I mean which event handler.<br>And where did u found that userful FAQ?
 
&nbsp;&nbsp;&nbsp;&nbsp;You should be able to put that code just about anywhere. I would put it in whatever event updates TMemo.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If you join the &quot;Community&quot; (it's free) at <A HREF=" TARGET="_new"> or <A HREF=" TARGET="_new"> , you can do a search. For example, I can do a search for TMemo and Caret to see the FAQ I got this from.<br><br> <p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top