2005. november 7., hétfő

How to get the X- and Y-coordinates of Desktop icons


Problem/Question/Abstract:

Does anyone know where the X- and Y-coordinates of the icons on the Windows Desktop are stored/ saved and how I can read and write those values?

Answer:

Since the desktop is a simple ListView (embedded in a few other windows), you'll be able to find it with this (from a little utility of mine). It uses IPCThrd.pas from the IPCXXX demos in the demos directory, for the SharedMem class. You'll have to use that, since otherwise you won't be able to read the information from desktop's memory into your memory.


type
  PInfo = ^TInfo;
  TInfo = packed record
    infoPoint: TPoint;
    infoText: array[0..255] of Char;
    infoItem: TLVItem;
    infoFindInfo: TLVFindInfo;
  end;

  {...}

var
  Info: PInfo;
  IniFile: TRegIniFile;
  Wnd: HWnd;
  Count, I: Integer;
  SharedMem: TSharedMem;
begin
  {Find list view window}
  Wnd := FindWindowEx(GetDesktopWindow, 0, 'Progman', 'Program Manager');
  Wnd := FindWindowEx(Wnd, 0, 'SHELLDLL_DefView', nil);
  Wnd := FindWindowEx(Wnd, 0, 'SysListView32', nil);
  Count := ListView_GetItemCount(Wnd);
  SharedMem := TSharedMem.Create('', SizeOf(TInfo));
  Info := SharedMem.Buffer;
  with Info^ do
  try
    infoItem.pszText := infoText;
    infoItem.cchTextMax := 255;
    infoItem.mask := LVIF_TEXT;
    IniFile := TRegIniFile.Create('Software\YaddaYadda');
    try
      with IniFile do
      begin
        EraseSection('Desktop\' + CurRes);
        for I := 0 to Count - 1 do
        begin
          infoItem.iItem := I;
          try
            ListView_GetItem(Wnd, infoItem);
            ListView_GetItemPosition(Wnd, I, infoPoint);
            WriteString('Desktop\' + CurRes, infoText, Format('%.4d, %.4d', [Point.X,
              Point.Y]));
          except
          end;
        end;
      end;
    finally
      IniFile.Free;
    end;
  finally
    SharedMem.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése