本文只供學習研究,不能用於非法用途,轉載請保留本文連結。
本文地址:http://blog.csdn.net/sushengmiyan/article/details/8774780
使用函數:EnumWindows,EnumWindowsProc
原始碼奉上:
unit QMsgMainFrm;interfaceuses 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.
我們在網路中經常會有這樣的需求,有一篇推銷內容要發送到QQ群或者QQ好友,手動一個一個發實在是麻煩的很,那麼就需要有一套好的工具來輔助我們,所以就寫了這麼一個東東。大家多多支援。