首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 帮忙改一下代码,谢谢 [已结贴,结贴人:kayoo]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-29 08:45:28 楼主
    http://topic.csdn.net/u/20080619/03/87b00864-1579-49be-8217-9c92d7d6d144.html
    这个贴子里26楼的代码,现在的情况是保存的时候不按图标序号保存,能不能帮忙改成按图标名称保存?谢谢!!500分.不是本人懒,是技术实在有限.先谢过...
    200  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-29 08:46:461楼 得分:0
    上面打错了个字,将"不按图标序号保存"改成"是按图标序号保存",失误..
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-30 09:05:502楼 得分:0
    试试.

    Delphi(Pascal) code
    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.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-30 10:51:233楼 得分:0
    JPEXE,谢谢回贴,但是你误解了我的意思,我的意思是直接按图标名称来保存坐标,恢复坐标的时候也以图标名称来恢复。。可不可以?拜托了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-30 10:53:504楼 得分:0
    现在的BUG是 如果用ID来记录和恢复,会导致图标识别的混乱了,我测试了,位置记对了,但是图标记不对,经常弄错。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-30 15:33:245楼 得分:200
    现在由于需要比对文字,
    且按顺序重新摆放,
    则循环*2,效率低了,
    恢复耗时会稍微长一点.

    Delphi(Pascal) code
    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.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-06-30 16:40:556楼 得分:0
    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;
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved