Link to home
Start Free TrialLog in
Avatar of cwilliso
cwilliso

asked on

Visually selecting text in MS Visual C++

How can I write some display logic in my WM_PAINT: section
that can show the user what text he is selecting with
the mouse??  I am not using MFC, as I am learning that on
the side, meanwhile, I need something quick.  I can't use
a text box or control that automatically has it, because
I have much custom display logic already.  I will always
accept suggestions for change however.
((I am using Microsoft Visual C++ 2.0.))


Side Question: Are there any tools that can speed up
development with MS VC++ ??  Any libraries??
I would prefer C, and non MFC. (I'm a caveman programmer.)
(This dosen't have anything to do with the main question
 And it dosen't need to be answered to obtain points.)
Avatar of nietod
nietod

answer coming.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Simply change the font or text colour appropriately when you draw the text.  There is no need for text boxes or anything any more complicated.

Another way to show a selection is to use 'greying' function.  An easy one is to use a checkerboard patterns of bits and xor fill the bounding rect of the text, then draw the text and finally xor fill the rectangle again.  This will 'grey' the selected text only .. it is quite effective.

I use this techinqiue in my CAD/drawing package to display selected text (and other objects) in a drawing.

BTW: if this is helpful to you, don't forget that you need to reject the currently proposed answer in order to grade me .. or apply to EE customer service to split the points.

First, what you need to do is maitain some information about the selection.  I use a structure that has an enumerated type that indicates if the cursor is in "insert", "overtype", or "selection".  It also has two ints.  One is the offset of the cursor, in terms of the number of characters into the edit box.  This is used for all cursor types.  The second integer is the length of the selection cursor, in terms of the number of characters selected.  Obviously this is used only in the selection cursor type.

I assume you want the text color to be the same throughout, but want to change the background where the cursor is.
When you set the DC's text color to what you want as the foreground color.  Ussualy black.  You set the DC's background color for the non-cursor color.  Probably white or maybe grey.  Then you TextOut() the characters that appear before the cursor.  (If there are no characters before the cursor, You can textout() zero characters safely).  Then you calculate the position of the end of wat you outputed (use one of the GetTextExtent() functions).  Set the background to the cursor color (I use different colors for each cursor type).  Then ouput the number of characters that are part of the cursor.  (1 for insert or overtype.  Multiple for a selection).  Then set the DC's background color back to you edit box's background color and print the remaining characters.

I hope this helps.  Let me know if you need more.

One thing to be careful of is to make sure your painting procedure can safeuly handle the fact that the text's length changes as the characters change.  (Assuming you have a proportional font).  The problem is that if you type in a short character to replace a long character.  A i to replace a W for example.  The string gets shorter (in display length), so their is an area to the right of the end of the displayed string that used to contain displayed text, but should not be clear.  You need to clear this area by filling it with your background color.
To reiterate (as my comment was in the midst of nietod's multi-part answer..

Simply change the font or text colour appropriately when you draw the text.  There is no need for text boxes or anything any more complicated.

Another way to show a selection is to use 'greying' function.  An easy one is to use a checkerboard patterns of bits and xor fill the bounding rect of the text, then draw the text and finally xor fill the rectangle again.  This will 'grey' the selected text only .. it is quite effective.

I use this techinqiue in my CAD/drawing package to display selected text (and other objects) in a drawing.

BTW: if this is helpful to you, don't forget that you need to reject the currently proposed answer in order to grade me .. or apply to EE customer service to split the points.

PS: I can provide some code to show hwo this is done (working out a bounding box, and doing the highlighting).