to smithcouple:你的函数仍在熟悉中,我自己找到一个别人写好的过程,我试过了确实能用,给大家分享下<br>//参考如下代码~~<br>//原理:可以通过消息LVM_SETITEMSTATE设置各项的状态,lparam参数是一个局部指针,所以需要把数据填写在该进程空间里~~<br>uses CommCtrl;<br><br>procedure SetListViewIndex(mHandle: THandle; mIndex: Integer);<br>var<br>vProcessId: DWORD;<br>vProcess: THandle;<br>vPointer: Pointer;<br>vNumberOfBytesRead: Cardinal;<br>vItem: TLVItem;<br>begin<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>try<br>vItem.stateMask := LVIS_FOCUSED;<br>vItem.state := LVIS_FOCUSED;<br>WriteProcessMemory(vProcess, vPointer, @vItem,<br>SizeOf(TLVItem), vNumberOfBytesRead);<br>SendMessage(mHandle, LVM_SETITEMSTATE, mIndex, lparam(vPointer));<br><br>vItem.stateMask := LVIS_SELECTED;<br>vItem.state := LVIS_SELECTED;<br>WriteProcessMemory(vProcess, vPointer, @vItem,<br>SizeOf(TLVItem), vNumberOfBytesRead);<br>SendMessage(mHandle, LVM_SETITEMSTATE, mIndex, lparam(vPointer));<br>finally<br>VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);<br>CloseHandle(vProcess);<br>end;<br>end; { SetListViewIndex }<br><br>procedure TForm1.SpeedButton1Click(Sender: TObject);<br>begin<br>SetListViewIndex(ListView1.Handle, 0);<br>end;