Solved

Visually selecting text in MS Visual C++

Posted on 1998-05-30
6
291 Views
Last Modified: 2008-03-06
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.)
0
Comment
Question by:cwilliso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1164965
answer coming.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1164966
Opp's that was supposed to be an answer.

I don't use edit boxes (or any built in control) either.  I've written my own (they are substancially more powerful that window's.)

details to follow.


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164967
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.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1164968
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1164969
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.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164970
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).

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question