unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, CommCtrl, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> MemoText: TMemo;<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure FormCreate(Sender: TObject);<br> function ListViewColumnCount(mHandle: THandle): Integer;<br> function GetListViewText(mHandle: THandle; mStrings: TStrings): Boolean;<br> procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY ;<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> HotKeyId: Integer;<br><br>{用到3个API函数 <br><br>BOOL RegisterHotKey( <br>HWND hWnd, //响应该热键的窗口句柄 <br>Int id, //该热键的唯一标识 <br>UINT fsModifiers, //该热键的辅助按键 <br>UINT vk //该热键的键值<br>); <br><br>为了得到唯一标识,我们还将用到另一个API函数 <br><br>ATOM GlobalAddAtom( <br><br>LPCTSTR lpString //自己设定的一个字符串 <br><br>); <br><br>因为我们还要在程序退出的时候,消除这个热键, 所以需要声明一个全局变量: <br><br>HotKeyId: Integer;<br>}<br><br><br>implementation<br><br>{$R *.dfm}<br><br>function TForm1.ListViewColumnCount(mHandle: THandle): Integer;<br>begin<br> Result := Header_GetItemCount(ListView_GetHeader(mHandle));<br>end;<br><br><br>{注册了热键,就该写下响应代码了。<br><br>首先,在程序头部分的private段中加入声明 (作用是声明这个过程,和声明变量类似。<br>关于如何声明函数、过程,请请参考各自的帮助文件或其它资料):<br><br>procedure HotKeyDown(var Msg: Tmessage); message WM_HOTKEY;<br><br>然后在程序中加入如下代码:<br>}<br><br>procedure TForm1.HotKeyDown(var Msg: Tmessage);<br>begin<br>if (Msg.LparamLo = MOD_ALT) AND (Msg.LParamHi = VK_F8) then // 假设热键为ALT+F8<br>begin<br>GetListViewText(WindowFromPoint(Point(Mouse.CursorPos.X, Mouse.CursorPos.Y)),MemoText.Lines);<br><br>end;<br>end;<br><br><br>function TForm1.GetListViewText(mHandle: THandle; mStrings: TStrings): Boolean;<br>var<br> vColumnCount: Integer;<br> vItemCount: Integer;<br> I, J: Integer;<br> vBuffer: array[0..255] of Char;<br> vProcessId: DWORD;<br> vProcess: THandle;<br> vPointer: Pointer;<br> vNumberOfBytesRead: Cardinal;<br> S: string;<br> vItem: TLVItem;<br><br><br>begin<br> Result := False;<br> if not Assigned(mStrings) then Exit;<br><br><br> vColumnCount := ListViewColumnCount(mHandle);<br> showmessage('有'+inttostr(vColumnCount)+'列');<br> if vColumnCount <= 0 then Exit;<br> vItemCount := ListView_GetItemCount(mHandle);<br> GetWindowThreadProcessId(mHandle, @vProcessId);<br> vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or<br> PROCESS_VM_WRITE, False, vProcessId);<br> vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,<br> PAGE_READWRITE);<br> mStrings.BeginUpdate;<br> try<br> mStrings.Clear;<br> for I := 0 to vItemCount - 1 do begin<br> S := '';<br> for J := 0 to vColumnCount - 1 do begin<br> with vItem do begin<br> mask := LVIF_TEXT;<br> iItem := I;<br> iSubItem := J;<br> cchTextMax := SizeOf(vBuffer);<br> pszText := Pointer(Cardinal(vPointer) + SizeOf(TLVItem));<br> end;<br> WriteProcessMemory(vProcess, vPointer, @vItem,<br> SizeOf(TLVItem), vNumberOfBytesRead);<br> SendMessage(mHandle, LVM_GETITEM, I, lparam(vPointer));<br> ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)),<br> @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);<br> S := S + #9 + vBuffer;<br> end;<br> Delete(S, 1, 1);<br> mStrings.Add(S);<br> end;<br> finally<br> VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);<br> CloseHandle(vProcess);<br> mStrings.EndUpdate;<br> end;<br> Result := True;<br><br>end; { GetListViewText }<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>HotKeyId := GlobalAddAtom('MyHotKey') - $C000;<br>{注: HotKeyId的合法取之范围是0x0000到0xBFFF之间, GlobalAddAtom函数得到的值 <br>在0xC000到0xFFFF之间,所以减掉0xC000来满足调用要求。}<br> RegisterHotKey(Handle,hotkeyid,MOD_ALT,VK_F8);<br><br><br>{热键的辅助按键包括Mod_Ctrl 、Mod_Alt、Mod_Shift,对于Windows兼容键盘还支持Windows<br>键,即其键面上有Windows标志的那个键,其值为Mod_win。<br><br>上面 的代码注册了一个热键:ALT+F8。当然如果你希望象TAKEIT那样,只用F8, 就这么写: <br><br>RegisterHotKey(Handle, hotkeyid, 0, VK_F8); <br><br>注:handle是一个特殊的变量,它表示当前窗口的句柄。 <br><br>这个函数你应该能句举一反三了吧。 <br><br>原理: <br><br>一旦热键设置成功,在程序应用过程中如果有相应的键被按下,Windows系统都会给你的应 <br>用程序发送一个消息WM_HOTKEY,不管你的应用程序是否为当前活动的。其中WM_HOTKEY消 <br>息的格式为: <br><br>idHotKey = (int) wParam; // 该参数在设置系统级的热键有用,一般不予使用 <br>fuModifiers = (UINT) LOWORD(lParam); //热键的辅助按键 <br>uVirtKey = (UINT) HIWORD(lParam); //热键的键值<br>}<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>UnRegisterHotKey(handle, HotKeyId);<br>{在窗口的close事件中加入 <br><br>UnRegisterHotKey(handle, HotKeyId); //注销HotKey, 释放资源。}<br>end;<br><br>end.