Solved

Visually selecting text in MS Visual C++

Posted on 1998-05-30
6
280 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
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
Comment Utility
answer coming.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now