获取WIN7桌面图标名称,我已要抓狂,求助各位大神!
以下方法在WIN7下获取桌面句柄无效:
Handle := FindWindow('progman', nil);
改为以下函数后可以:
procedure findDesktopWnd;
Function MyEnumWindowProc(wnd: HWND; LPARAM: LPARAM): Boolean; stdcall;
var
sndWnd, targetWnd: Cardinal;
begin
sndWnd := FindWindowEx(wnd, 0, 'SHELLDLL_DefView', 0);
if sndWnd = 0 then
begin
Result := true;
Exit;
end;
targetWnd := FindWindowEx(sndWnd, 0, 'SysListView32', 'FolderView');
if targetWnd = 0 then
begin
Result := true;
Exit;
end;
deskTopHandel := wnd;
Result := false;
end;
begin
EnumWindows(@MyEnumWindowProc, 0);
end;
在正确获得桌面句柄后,获取图标位置XP,WIN7的代码都好用
// 取得桌面存放图标的listview句柄
function GetDesktopLvHand: THandle;
begin
//Result := FindWindow('progman', nil);
findDesktopWnd;
Result :=deskTopHandel;
Result := GetWindow(Result, GW_Child);
Result := GetWindow(Result, GW_Child);
end;
// 取得图标的坐标位置
function GetIcoPosition(h: THandle; idx: Integer): TPoint;
var
hpro, pid, dm, i: Cardinal;
p: Pointer;
begin
GetWindowThreadProcessId(h, pid);
hpro := OpenProcess(PROCESS_ALL_ACCESS, False, pid);
if hpro <> 0 then
try
p := VirtualAllocEx(hpro, nil, SizeOf(TPoint), MEM_COMMIT,
PAGE_READWRITE);
ListView_GetItemPosition(h, idx, TPoint(p^));
ReadProcessMemory(hpro, p, @Result, SizeOf(Result), dm);
finally
VirtualFreeEx(hpro, p, 0, MEM_RELEASE);
CloseHandle(hpro);
end;
end;
现在问题来了,XP上好用的一段代码在WIN7下程序会崩溃,查了很资料,也没有办法解释是为什么:
function ListView_GetItemText_Ex(hwndLV: HWND; i, iSubItem: Integer; pszText: PChar; cchTextMax: Integer): Integer;
var
LVItem: TLVItem;
ProcessID, ProcessHD, Temp: DWORD;
MemPoint: Pointer;
begin
GetWindowThreadProcessId(hwndLV, ProcessID);
ProcessHD := OpenProcess( PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, FALSE, ProcessID);
MemPoint := VirtualAllocEx(ProcessHD, nil, SizeOf(TLVItem) + cchTextMax, MEM_COMMIT, PAGE_READWRITE);
LVItem.iSubItem := iSubItem;
LVItem.cchTextMax := cchTextMax;
LVItem.pszText := PChar(Integer(MemPoint) + SizeOf(TLVItem));
WriteProcessMemory(ProcessHD, MemPoint, @LVItem, SizeOf(TLVItem), Temp);
Result := SendMessage(hwndLV, LVM_GETITEMTEXT, i, Integer(MemPoint));
ReadProcessMemory(ProcessHD, Pointer(Integer(MemPoint) + SizeOf(TLVItem)), pszText, cchTextMax, Temp);
VirtualFreeEx(ProcessHD, MemPoint, SizeOf(TLVItem) + cchTextMax, MEM_DECOMMIT);
VirtualFreeEx(ProcessHD, MemPoint, 0, MEM_RELEASE);
end;
function GetIcoItemText(i: Integer): string;
var
TextBuffer: array [0 .. 100] of char;
FDeskTopLvHandle: THandle;
begin
FDeskTopLvHandle := GetDesktopLvHand;
ListView_GetItemText_Ex(FDeskTopLvHandle, i, 0, TextBuffer, 100);
Result := TextBuffer;
end;
经过排查
Result := SendMessage(hwndLV, LVM_GETITEMTEXT, i, Integer(MemPoint));
这句会造成WIN7下程序崩溃。
求各位大神帮看下,谢谢!WIN764位+delphi7.