Solved

How to grab selected text in a window

Posted on 2004-04-21
6
220 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
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

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

 

Author Comment

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

Expert Comment

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

6 Experts available now in Live!

Get 1:1 Help Now