i'm trying to make a program that when i press a hotkey it concatenate a certain text to a selected text from a window. for example: i have the text "capture a text selected with the mouse", i select the word "text" with the mouse and now when i press a certain hotkey it copies me to clipboard the following : xxx+text+xxx. so my question is how to return the word selected by mouse?
thanks!!
from what u told me i understud this:
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Clipbrd;
type
TForm4 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY;
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
implementation
const
MY_ID = 123;
{$R *.dfm}
procedure TForm4.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, MY_ID, MOD_CONTROL, ord('1'));
end;
procedure TForm4.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle, MY_ID);
end;
procedure TForm4.WMHotkey(var Message: TWMHotKey);
lookup_word: string;
begin
clipboard.clear;
if Message.HotKey = MY_ID then
begin
if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
RaiseLastOSError;
try
SendMessage( GetFocus, WM_GETTEXT, 0, 0 );
lookup_word:= clipboard.astext;
edit1.Text := lookup_word;
Clipboard.AsText := '<font color=blue> edit1.text </font>';
SendMessage(GetFocus, WM_PASTE, 0, 0);
finally
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
end;
end;
end;
end;
end.
is this ok?
i manage to create my app the way i intended to. but i 开发者_开发问答ran into another problem now. it wont work on a aspx application. it wont recognize the text from an aspx edit box. Is there any way around this problem?
thanks!
If I understand your question correctly, what you mean with "the text selected with the mouse" is the normal highlighted text on an edit control such as in a TEdit, TMemo or TRichEdit. If such the case, then VCL has a Seltext property which contains currently selected text. so the code will be something like: (example for TMemo control)
...
uses Clipbrd;
...
Clipboard.asText:= xxx + Memo1.SelText + xxx;
...
If the selected text is from other application, then, its very depend on the control used by the application. If the control is a standard windows control or it's descendant (mostly), then you can get the selected text by sending message to that control, but if the component is not a standard one, it won't response the message correctly. This method require you to know the window handle of the target control (using GetFocus in Windows unit): 1. Get the whole text by sending WM_GETTEXT message 2. Get the selection position by sending an EM_GETSEL message 3. Calculate the selected text (the sub string of the whole text) using the selection position from point 2. If you have a vcl source than you can use TCustomEdit class source code implementation in StdCtrls unit as a reference. my example:
...
var
Buff: array[0..65535] of char;
...
function CurrentSelectedText: string;
var
hFocus: hWnd;
aStart, aEnd: integer;
begin
//added by andrei, attach input to current thread
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true);
hFocus:= GetFocus;
SendMessage(hFocus, WM_GETTEXT, 65535, integer(@buff));
SendMessage(hFocus, EM_GETSEL, Integer(@aStart), Integer(@aEnd));
result:= Copy(StrPas(Buff), 1+aStart, aEnd-aStart);
end;
Please don't abuse the clipboard this way. The clipboard is provided for the convenience of the user, not the programmer. If the user has something important on the clipboard, you're going to wipe it out. And you're going to cause unexpected/unwanted data to appear in clipboard extender apps. You'll cause unwanted network traffic when using Remote Desktop products of any kind.
精彩评论