Click here to Skip to main content
14,932,691 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to select columns of text when alt and mouse left key is down and dragged.
I need to implement the operation which exists in visual studio as we select columns of text with alt and mouse left key is clicked and dragged.

Thanks in advance

What I have tried:

I dont have any idea of implementing this.
Updated 10-Sep-18 0:11am
Jochen Arndt 10-Sep-18 5:36am
It depends on the type of the control from where you want to select the text.

Most controls support such already when the Shift key is down while dragging.
Eshwar Reddy 10-Sep-18 5:51am
I want to implement the same thing in the editor in my project.
The control is derived from CWnd class.

1 solution

So you have created your own CWnd based control.

Then you have to implement it yourself using the common handlers for mouse button down and up, and mouse move. There check the ALT key state with GetKeyState(VK_MENU) and set class member variables accordingly to indicate that selection is active and where it starts:
  • Button down: if ALT down save start position and set state to "selecting"
  • Move: if state is "selecting" update screen to show the selection
  • Button up: if state is "selecting" save end position and set state to "has selection"
  • Copy: if state is "has selection" copy to clipboard
How to show the selection depends on your drawing routine. It usually requires getting the start and end character position from the mouse positions.
Eshwar Reddy 25-Sep-18 6:41am
Hi, thanks for the response.

I am able to hit the events and do as you said.But, how can i select the text as a rectangle.
Jochen Arndt 25-Sep-18 7:11am
You have to get the character position (buffer offset) from the mouse position. This requires knowing the page and line offsets when scrolled, the line height, and the character width (with fixed width fonts). With proportional fonts it is more complicated but there are Windows API functions for such calculations. The keyword is "text metrics".
Eshwar Reddy 1-Oct-18 5:42am
We have CharfromPos() to get the character index in CEdit or CRicheditctrl class.
My control is derived from CWnd. Is there any similar function like CharfromPos() in CWnd or i need to implement with my own code.

Thanks in advance.
Jochen Arndt 1-Oct-18 6:13am
I don't know that control. You have to check the documentation if there is a similar function. If not, you have to implement it yourself using the metrics of the used font.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900