Solved

How to grab selected text in a window

Posted on 2004-04-21
6
231 Views
Last Modified: 2010-04-05
Is it possible to get selected text from a window without sending it to the clipboard using the keystrokes Ctrl + C?

What I do at the moment, is that I react on the mouse event WM_LBUTTONUP in windows/controls with classnames that I list in my Delphi program. At the moment of the Mouse Up Event, I also know the handle of that window/control. I send Ctrl + C to that window, but my question is: Is there another way, can the selected text be acquired with for instance the Windows messaging methods, or by other means, not including the clipboard?
0
Comment
Question by:haab
[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
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10882727
You could program a HotKey to get the current focused controls text, is this what your interested in?

Shane
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 350 total points
ID: 10882800
Hotkeys:

Uses Messages;

In the main forms OnCreate handler assign the hotkey:

  If not RegisterHotkey( Handle, 1, MOD_ALT or MOD_SHIFT, VK_F9 ) Then
    ShowMessage('Unable to assign Alt-Shift-F9 as hotkey.');

In the main forms OnClose event remove the handler:

  UnRegisterHotkey( Handle, 1 );

Add a handler for the WM_HOTKEY message to the form:

  private // form declaration
    Procedure WMHotkey( Var msg: TWMHotkey );
      message WM_HOTKEY;

Procedure TForm1.WMHotkey( Var msg: TWMHotkey );
  Begin
   // If msg.hotkey =
  End;


OR

You can also use Form's onShortCut event.

procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
case msg.CharCode of
  120 : begin { F9 pressed }; Handled := true; end;
  121 : begin { F10 pressed }; Handled := true; end;
end;
end;


Current Focused control

 Form1.ActiveControl


You could simple test the control for type

 if Form1.ActiveControl is TEdit


 MyText:= TEdit(Form1.ActiveControl).Text;


Hope this helps!

Shane
0
 

Author Comment

by:haab
ID: 10882821
Well, it is only the selected text that I want. I already know how to get the entire text of controls; the problem is to grab only the selected text, and the question whether it is possible to do so without involving the clipboard.

Thank you for your kind response, both on this issue and my other question from today regarding the maintaining of clipboard formats!
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!

 

Author Comment

by:haab
ID: 10882844
I see that you have given some code as well. Thank you! I will try it out!
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10882897
Again, test for the type of focused control, then use the

 SelText property of the control

MyText:= TEdit(Form1.ActiveControl).SelText;


Shane
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 150 total points
ID: 10886683
you could use the wm_gettext-message,
this will return you the whole text of course,
not only the selection

alternativly you could try the EM_GETSELTEXT-message
(but i guess this works only for richtext-controls)

meikl ;-)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

687 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