unit QMsgMainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Clipbrd;
type
TForm1 = class(TForm)
edtMsg: TEdit;
lblTip: TLabel;
btnTry: TButton;
lblMsgWindow: TLabel;
lblMsgContextWindow: TLabel;
procedure btnTryClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;
implementation
{$R *.dfm}
procedure TForm1.btnTryClick(Sender: TObject);
begin
if Trim(edtMsg.Text) = '' then
begin
ShowMessage('请输入要发送的消息,消息不能为空');
end else
begin
EnumWindows(@EnumWindowsProc ,0);
end;
end;
function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;
var
WindowText : string ;
WindowClass : string ;
SendHwnd : DWORD ;
tdiahandle : THandle ;
tedithandle : THandle ;
tduihuakhandle: THandle ;
c: Cardinal;
ProcessHandle:LongWord;
begin
if ( IsWindowVisible(hwnd) or IsIconic(hwnd) ) and
(
(GetWindowLong(hwnd, GWL_HWNDPARENT) = 0) or
(GetWindowLong(hwnd, GWL_HWNDPARENT) = Longint(GetDesktopWindow))
) and
( GetWindowLong(hwnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
begin
SetLength(WindowText, GetWindowTextLength(hwnd)+2);
Getwindowtext(hwnd, Pchar(WindowText), GetWindowTextLength(hwnd)+2);
WindowText := string(Pchar(WindowText));
SetLength(WindowClass, 512);
GetClassName(hwnd, Pchar(WindowClass), 512);
WindowClass := string(Pchar(WindowClass) );
if SameStr(WindowClass , 'TXGuiFoundation') then
begin
BringWindowToTop(hwnd);
Form1.lblMsgContextWindow.Caption := WindowText;
if Clipboard.HasFormat(CF_TEXT) then
Clipboard.Clear;
Form1.edtMsg.SelectAll;
Form1.edtMsg.CopyToClipboard;
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event(Ord('V'), MapVirtualKey(Ord('V'), 0), 0, 0);
keybd_event(Ord('V'), MapVirtualKey(Ord('V'), 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0),0 ,0);
end;
end;
Result := True;
end;
end.