//參考以下函數,因沒有98,沒有試過98是否可以,2000下面測試通過<br>//取得的字符在text保存<br>//ptPosition保存位置<br>procedure GetDesktopIconItem(var Text: string; var ptPosition: TPoint;<br> Index: Integer);<br>var<br> DesktopWnd: HWND;<br> ItemText: array[0..MaxTextLen] of char;<br> Item: TLvItem;<br><br> dwProcessID: DWORD;<br> hProcess: THandle;<br> lpTextRemote: Pointer;<br> lpPositionRemote: Pointer;<br> lpListItemRemote: Pointer;<br><br> lpData: Pointer;<br><br> dwBytesRead, dwBytesWrite: DWORD;<br>begin<br> Text := '';<br> ptPosition.X := 0;<br> ptPosition.Y := 0;<br><br> DesktopWnd := GetDesktop; // 桌面的句柄,改成你要的<br><br> GetWindowThreadProcessId(DesktopWnd, dwProcessID);<br> hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID);<br> if hProcess = 0 then //得不到指定進程的句柄<br> Exit;<br><br> try<br> lpData := VirtualAllocEx(hProcess, nil,<br> MaxTextLen + 1 + SizeOf(TPoint) + SizeOf(TLvItem), // 長度<br> MEM_COMMIT, PAGE_READWRITE);<br><br> if not Assigned(lpData) then<br> Exit;<br><br> lpTextRemote := lpData;<br> lpPositionRemote := PChar(lpData) + MaxTextLen + 1;<br> lpListItemRemote := PChar(lpData) + MaxTextLen + 1 + SizeOf(TPoint);<br><br> try<br> ZeroMemory(@ItemText, MaxTextLen + 1);<br> WriteProcessMemory(hProcess, lpTextRemote, @ItemText,<br> MaxTextLen + 1, dwBytesWrite);<br> WriteProcessMemory(hProcess, lpPositionRemote, @ptPosition,<br> SizeOf(TPoint), dwBytesWrite);<br><br> ZeroMemory(@Item, SizeOf(TLvItem));<br> Item.mask := LVIF_TEXT;<br> Item.iItem := Index;<br> Item.iSubItem := 0;<br> Item.pszText := lpTextRemote;<br> Item.cchTextMax := MaxTextLen;<br> dwBytesWrite := 0;<br><br> if not WriteProcessMemory(hProcess, lpListItemRemote, @Item,<br> sizeof(TLvItem),<br> dwBytesWrite) then<br> Exit;<br><br> SendMessage(DesktopWnd, LVM_GETITEMTEXT, Index,<br> LPARAM(lpListItemRemote));<br> SendMessage(DesktopWnd, LVM_GETITEMPOSITION, Index,<br> LPARAM(lpPositionRemote));<br> ReadProcessMemory(hProcess, lpTextRemote, @ItemText, MaxTextLen + 1,<br> dwBytesRead);<br> ReadProcessMemory(hProcess, lpPositionRemote, @ptPosition,<br> SizeOf(TPoint), dwBytesRead);<br><br> Text := ItemText;<br> finally<br> VirtualFreeEx(hProcess, lpData, 0, MEM_RELEASE);<br> end;<br> finally<br> CloseHandle(hProcess);<br> end;<br>end;<br><br>//應用例子<br>function GetDesktopIconCount: Integer;<br>begin<br> Result := ListView_GetItemCount(GetDesktop);<br>end;<br><br>procedure SaveDesktopIconPos(FileName: string);<br>var<br> i: Integer;<br> IconCount: Integer;<br> Text: string;<br> ptPosition: TPoint;<br> Ini: TIniFile;<br>begin<br> IconCount := GetDesktopIconCount;<br><br> if FileExists(FileName) then<br> DeleteFile(FileName);<br><br> Ini := TIniFile.Create(FileName);<br><br> try<br> for i := 0 to IconCount - 1 do<br> begin<br> GetDesktopIconItem(Text, ptPosition, i);<br> Ini.WriteString('Item' + IntToStr(i), 'Text', Text);<br> Ini.WriteInteger('Item' + IntToStr(i), 'X', ptPosition.X);<br> Ini.WriteInteger('Item' + IntToStr(i), 'Y', ptPosition.Y);<br> end;<br> finally<br> Ini.Free;<br> end;<br>end;<br>