帮忙改一下代码,谢谢

kayoo 2008-06-29 08:45:28
http://topic.csdn.net/u/20080619/03/87b00864-1579-49be-8217-9c92d7d6d144.html
这个贴子里26楼的代码,现在的情况是保存的时候不按图标序号保存,能不能帮忙改成按图标名称保存?谢谢!!500分.不是本人懒,是技术实在有限.先谢过...
...全文
165 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kayoo 2008-06-30
  • 打赏
  • 举报
回复
现在的BUG是 如果用ID来记录和恢复,会导致图标识别的混乱了,我测试了,位置记对了,但是图标记不对,经常弄错。
kayoo 2008-06-30
  • 打赏
  • 举报
回复
JPEXE,谢谢回贴,但是你误解了我的意思,我的意思是直接按图标名称来保存坐标,恢复坐标的时候也以图标名称来恢复。。可不可以?拜托了。
JPEXE 2008-06-30
  • 打赏
  • 举报
回复
试试.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Types, CommCtrl, IniFiles;

type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

// 获取桌面句柄
function GetDesktopHandle: THandle;
begin
Result := FindWindow('progman', nil);
Result := GetWindow(Result, GW_CHILD);
Result := GetWindow(Result, GW_CHILD);
end;

// 获取桌面图标的名称(从桌面进程中读取)
function GetDesktopIconText(nIndex: Integer): string;
var
hDesktop: THandle;
dwPID: DWORD;
hProcess: THandle;
dwNumberOfBytesWritten: DWORD;
dwNumberOfBytesRead: DWORD;
pBuffer: Pointer;
item: TLVItem;
pszText: array[0..MAX_PATH] of Char;
begin
hDesktop := GetDesktopHandle;
GetWindowThreadProcessId(hDesktop, @dwPID);
hProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, dwPID);
if hProcess = 0 then Exit;
try
pBuffer := VirtualAllocEx(hProcess, nil, SizeOf(TLVItem) + MAX_PATH, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
if pBuffer = nil then Exit;
try
item.iItem := nIndex;
item.iSubItem := 0;
item.cchTextMax := MAX_PATH;
item.pszText := PChar(Cardinal(pBuffer) + SizeOf(TLVItem));
WriteProcessMemory(hProcess, pBuffer, @item, SizeOf(TLVItem) + MAX_PATH, dwNumberOfBytesWritten);
SendMessage(hDesktop, LVM_GETITEMTEXT, nIndex, LPARAM(pBuffer));
ReadProcessMemory(hProcess, Pointer(Cardinal(pBuffer) + SizeOf(TLVItem)), @pszText[0], MAX_PATH, dwNumberOfBytesRead);
Result := string(pszText);
finally
VirtualFreeEx(hProcess, pBuffer, 0, MEM_RELEASE);
end;
finally
CloseHandle(hProcess);
end;
end;

// 获取桌面图标的坐标(从桌面进程中读取)
function GetDesktopIconPostion(nIndex: Integer): TPoint;
var
hDesktop: THandle;
dwPID: DWORD;
hProcess: THandle;
dwNumberOfBytesRead: DWORD;
ppt: Pointer;
begin
hDesktop := GetDesktopHandle;
GetWindowThreadProcessId(hDesktop, @dwPID);
hProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, dwPID);
if hProcess = 0 then Exit;
try
ppt := VirtualAllocEx(hProcess, nil, SizeOf(TPoint), MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
if ppt = nil then Exit;
try
ListView_GetItemPosition(hDesktop, nIndex, PPoint(ppt)^);
ReadProcessMemory(hProcess, ppt, @Result, SizeOf(TPoint), dwNumberOfBytesRead);
finally
VirtualFreeEx(hProcess, ppt, 0, MEM_RELEASE);
end;
finally
CloseHandle(hProcess);
end;
end;

// 记录桌面图标位置
procedure SaveDesktopIcons;
var
hDesktop: THandle;
i, nCount: Integer;
pt: TPoint;
name: string;
ini: TIniFile;
begin
hDesktop := GetDesktopHandle;
nCount := ListView_GetItemCount(hDesktop);
if nCount <= 0 then Exit;
ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'SavePos.ini');
ini.WriteString('DESKTOP_ICON_POS', 'COUNT', IntToStr(nCount));
try
for i := 0 to nCount - 1 do begin
pt := GetDesktopIconPostion(i);
name := GetDesktopIconText(i);
ini.WriteString('DESKTOP_ICON_POS', Format('ICON_%d_NAME', [i]), name);
ini.WriteString('DESKTOP_ICON_POS', Format('ICON_%d_X', [i]), IntToStr(pt.X));
ini.WriteString('DESKTOP_ICON_POS', Format('ICON_%d_Y', [i]), IntToStr(pt.Y));
end;
finally
ini.Free;
end;
end;

// 恢复桌面图标位置
procedure RestoreDesktopIcons;
var
hDesktop: THandle;
i, nCount: Integer;
nPosX, nPosY: Integer;
ini: TIniFile;
begin
hDesktop := GetDesktopHandle;
ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'SavePos.ini');
try
nCount := ini.ReadInteger('DESKTOP_ICON_POS', 'COUNT', 0);
if nCount <= 0 then Exit;
for i := 0 to nCount - 1 do begin
nPosX := ini.ReadInteger('DESKTOP_ICON_POS', Format('ICON_%d_X', [i]), 0);
nPosY := ini.ReadInteger('DESKTOP_ICON_POS', Format('ICON_%d_Y', [i]), 0);
ListView_SetItemPosition(hDesktop, i, nPosX, nPosY);
end;
finally
ini.Free;
end;
end;

// 记录
procedure TForm1.btn1Click(Sender: TObject);
begin
SaveDesktopIcons;
end;

// 恢复
procedure TForm1.btn2Click(Sender: TObject);
begin
RestoreDesktopIcons;
end;


end.
  • 打赏
  • 举报
回复
procedure RestoreDesktopIcons;
var
hDesktop: THandle;
i, nCount: Integer;
nPosX, nPosY: Integer;
ini: TIniFile;
begin
hDesktop := GetDesktopHandle;
ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'SavePos.ini');
try
nCount := ini.ReadInteger('DESKTOP_ICON_POS', 'COUNT', 0);
if nCount <= 0 then Exit;
for i := 0 to nCount - 1 do begin
nPosX := ini.ReadInteger('DESKTOP_ICON_POS', Format('ICON_%d_X', [i]), 0);
nPosY := ini.ReadInteger('DESKTOP_ICON_POS', Format('ICON_%d_Y', [i]), 0);
ListView_SetItemPosition(hDesktop, i, nPosX, nPosY);
end;
finally
ini.Free;
end;
end;
JPEXE 2008-06-30
  • 打赏
  • 举报
回复
现在由于需要比对文字,
且按顺序重新摆放,
则循环*2,效率低了,
恢复耗时会稍微长一点.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Types, CommCtrl, IniFiles;

type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

// 获取桌面句柄
function GetDesktopHandle: THandle;
begin
Result := FindWindow('progman', nil);
Result := GetWindow(Result, GW_CHILD);
Result := GetWindow(Result, GW_CHILD);
end;

// 获取桌面图标的名称(从桌面进程中读取)
function GetDesktopIconText(nIndex: Integer): string;
var
hDesktop: THandle;
dwPID: DWORD;
hProcess: THandle;
dwNumberOfBytesWritten: DWORD;
dwNumberOfBytesRead: DWORD;
pBuffer: Pointer;
item: TLVItem;
pszText: array[0..MAX_PATH] of Char;
begin
hDesktop := GetDesktopHandle;
GetWindowThreadProcessId(hDesktop, @dwPID);
hProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, dwPID);
if hProcess = 0 then Exit;
try
pBuffer := VirtualAllocEx(hProcess, nil, SizeOf(TLVItem) + MAX_PATH, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
if pBuffer = nil then Exit;
try
item.iItem := nIndex;
item.iSubItem := 0;
item.cchTextMax := MAX_PATH;
item.pszText := PChar(Cardinal(pBuffer) + SizeOf(TLVItem));
WriteProcessMemory(hProcess, pBuffer, @item, SizeOf(TLVItem) + MAX_PATH, dwNumberOfBytesWritten);
SendMessage(hDesktop, LVM_GETITEMTEXT, nIndex, LPARAM(pBuffer));
ReadProcessMemory(hProcess, Pointer(Cardinal(pBuffer) + SizeOf(TLVItem)), @pszText[0], MAX_PATH, dwNumberOfBytesRead);
Result := string(pszText);
finally
VirtualFreeEx(hProcess, pBuffer, 0, MEM_RELEASE);
end;
finally
CloseHandle(hProcess);
end;
end;

// 获取桌面图标的坐标(从桌面进程中读取)
function GetDesktopIconPostion(nIndex: Integer): TPoint;
var
hDesktop: THandle;
dwPID: DWORD;
hProcess: THandle;
dwNumberOfBytesRead: DWORD;
ppt: Pointer;
begin
hDesktop := GetDesktopHandle;
GetWindowThreadProcessId(hDesktop, @dwPID);
hProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, dwPID);
if hProcess = 0 then Exit;
try
ppt := VirtualAllocEx(hProcess, nil, SizeOf(TPoint), MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
if ppt = nil then Exit;
try
ListView_GetItemPosition(hDesktop, nIndex, PPoint(ppt)^);
ReadProcessMemory(hProcess, ppt, @Result, SizeOf(TPoint), dwNumberOfBytesRead);
finally
VirtualFreeEx(hProcess, ppt, 0, MEM_RELEASE);
end;
finally
CloseHandle(hProcess);
end;
end;

// 记录桌面图标位置
procedure SaveDesktopIcons;
var
hDesktop: THandle;
i, nCount: Integer;
ptPos: TPoint;
strName: string;
ini: TIniFile;
begin
hDesktop := GetDesktopHandle;
nCount := ListView_GetItemCount(hDesktop);
if nCount <= 0 then Exit;
ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'SavePos.ini');
ini.EraseSection('DESKTOP_ICON_POS');
ini.WriteString('DESKTOP_ICON_POS', 'COUNT', IntToStr(nCount));
try
for i := 0 to nCount - 1 do begin
ptPos := GetDesktopIconPostion(i);
strName := GetDesktopIconText(i);
ini.WriteInteger('DESKTOP_ICON_POS', Format('%s_Index', [strName]), i);
ini.WriteInteger('DESKTOP_ICON_POS', Format('%s_PosX', [strName]), ptPos.X);
ini.WriteInteger('DESKTOP_ICON_POS', Format('%s_PosY', [strName]), ptPos.Y);
end;
finally
ini.Free;
end;
end;

// 恢复桌面图标位置
procedure RestoreDesktopIcons;
var
hDesktop: THandle;
i, nCount: Integer;
strName: string;
nIndex: Integer;
nPosX, nPosY: Integer;
ini: TIniFile;
begin
hDesktop := GetDesktopHandle;
nCount := ListView_GetItemCount(hDesktop);
if nCount <= 0 then Exit;
ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'SavePos.ini');
try
for nIndex := 0 to nCount - 1 do begin
for i := 0 to nCount - 1 do begin
strName := GetDesktopIconText(i);
if nIndex = ini.ReadInteger('DESKTOP_ICON_POS', Format('%s_Index', [strName]), 0) then begin
nPosX := ini.ReadInteger('DESKTOP_ICON_POS', Format('%s_PosX', [strName]), 0);
nPosY := ini.ReadInteger('DESKTOP_ICON_POS', Format('%s_PosY', [strName]), 0);
ListView_SetItemPosition(hDesktop, i, nPosX - 1, nPosY - 1);
Break;
end;
end;
end;
finally
ini.Free;
end;
end;

// 记录
procedure TForm1.btn1Click(Sender: TObject);
begin
SaveDesktopIcons;
end;

// 恢复
procedure TForm1.btn2Click(Sender: TObject);
begin
RestoreDesktopIcons;
end;


end.
kayoo 2008-06-29
  • 打赏
  • 举报
回复
上面打错了个字,将"不按图标序号保存"改成"是按图标序号保存",失误..

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧