Re: Screen to clipboard?
From: Niklas Borson (niklasb_at_microsoft.com)
Date: 12/31/03
- Next message: Programmer Dude: "Re: BigNum -- BigFrac"
- Previous message: Joe \: "Re: Floating point programming algorithms"
- In reply to: Bob Margulies: "Re: Screen to clipboard?"
- Next in thread: Programmer Dude: "Re: Screen to clipboard?"
- Reply: Programmer Dude: "Re: Screen to clipboard?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 31 Dec 2003 11:28:56 -0800
Bob Margulies <rsm@webuniverse.net> wrote in message news:<R96dnZD3z-IVcm-i4p2dnA@vel.net>...
> Yes. The code snippet is
>
> case WM_PAINT :
> ....
> TextOut (hdc,
>
> following methods given in Petzold's "Programming Windows 95." It's
> obvious that I need to add something that makes the text highlightable,
> but I can't find the info anywhere.
It looks like you're using the Windows API so there are more appropriate
newsgroups for this question, e.g., microsoft.public.win32.programmer.ui.
Nevertheless, I will try to give you some guidance. Apologies to the
newsgroup for the Win32 specific stuff, but most of what I have to say
is more conceptual in nature, and not specific to windows.
It's clear that you're drawing the text yourself rather than, say,
creating a control that contains text. Since you're drawing the text
yourself, you have to implement all of the selection logic yourself
as well. This can be fairly involved, but I'll just try to give a very
general idea of how it might work.
First of all, you need some way to represent a location in your document.
By document, I just mean the content of your window, whatever that may
be. Perhaps it's just a string, in which case a location could be just
an integer index. Or perhaps for you a document comprises multiple lines
of text (i.e., a vector of strings), in which case a location might
consist of a line index and a character index. Or it might be something
more complex. In any case, I'll just assume you have a user-defined type
(struct or class) or typedef called Location.
Second, you need a way to represent the current selection. This could
simply be two locations, which I'll call the caret (aka insertion point)
and anchor. The current selection is the text between the caret and
anchor. The selection is empty if the caret and anchor are the same.
Third, change your painting code to draw selected text differently than
unselected text. Some relevant API functions are GetTextExtentPoint32,
GetSysColor, SetTextColor, SetBkColor, and ExtTextOut. The user can't
select anything yet, but you can test your painting code by initializing
the anchor and caret to diffent values.
Fourth, write a function to change the current selection. It might be
declared something like this:
void SetSelection(Position newAnchor, Position newCaret);
Any other code in your program that changes the selection should do so
through this function. In addition to simply assigning new values to
the anchor and caret, the function would validate the parameters,
perhaps maintain other invariants, and force the window (or part of it
as appropriate) to be repainted using the InvalidateRect API.
Fifth, handle keyboard input. This is easier than mouse input and gives
you a chance to test your SetSelection function. Process the WM_KEYDOWN
message and implement the appropriate function for various keystrokes
such as VK_LEFT, VK_RIGHT, etc. You might need to take into account the
state of certain keys (e.g., CTRL and SHIFT), which you can get using
GetKeyState. Add functionality incrementally and test as you go.
Sixth, before handle mouse input, you will need the ability to convert
logical locations in your document to and from physical points on the
screen or in your window. This could take the form of a helper functions
like PointFromLocation and LocationFromPoint.
Finally, handle mouse input. You might start small by just processing
WM_LBUTTONDOWN and setting the selection accordingly (taking into account
the state of the SHIFT key). After that, you can add the ability to press,
move, and release. When the button is pressed, you should capture the
mouse (using SetCapture) and set a variable indicating that your window
is in a selecting state. If the mouse is moved (WM_MOUSEMOVE) and you're
in a selecting state, you should set the selection as appropriate. If the
button is released (WM_LBUTTONUP) and you're in a selecting state, you
should release your capture of the mouse (ReleaseCapture). Capturing the
mouse ensures that you receive mouse messages even if the user moves the
mouse out of your client area. Note: mouse coordinates are sometimes given
in screen coordinates and sometimes in client coordinates (i.e., relative
to the client area of your window). This can depend not only on the message
but on whether the mouse is captured. I tend to use the GetCursorPos
function which *always* returns screen coordinates. There are APIs for
translating between the two coordinate systems.
For simplicity, I've left out some things that might matter to you. One
is scrolling. If your window is going to be scrollable, you need to
distinguish between client coordinates (relative to the client area of
your window) and document coordinates (relative to the origin of your
document which, due to scrolling, may be someware above and/or to the
left or the client origin).
In addition, there is an API for using the caret, which is the little
blinking thing that indicates the text insertion point. Your function
for setting the selection should also set the caret.
I hope this information is helpful to you. Again, I apologize to the
group for the Win32-specific stuff, but I suspect the general concepts
above would apply to other GUI environments as well and are therefore
on topic.
--Nick
- Next message: Programmer Dude: "Re: BigNum -- BigFrac"
- Previous message: Joe \: "Re: Floating point programming algorithms"
- In reply to: Bob Margulies: "Re: Screen to clipboard?"
- Next in thread: Programmer Dude: "Re: Screen to clipboard?"
- Reply: Programmer Dude: "Re: Screen to clipboard?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]