kuaw26
kuaw26

Reputation: 140

How to get/set desktop icons position and size from Delphi?

I like to use large icons on my desktop, but very often they back to normal size, still can't trace why :). As a programmer I decide to write my own utility for saving and restoring icons positions. Googling around doesn't give much info. Can anyone give me a hint or point to link where I could start?

Upvotes: 1

Views: 2718

Answers (4)

Ken White
Ken White

Reputation: 125748

You can't reliably. Raymond Chen explains why in this post.

Basically, it's because there's no way to force an icon to be in a specific location on the desktop, meaning there's no way to specify where an individual icon will be positioned.

Upvotes: 2

Vivian Mills
Vivian Mills

Reputation: 2562

At one point in time, ie Win2k/WinXP for sure, the desktop was actually a type of ListView. I'm not sure it's still that in the new OS's. Knowing that it was easy to get the desktop handle and using the LV api functions manipulate it into doing things like displaying with the Report style.

Here are two functions that show you how to start manipulating the desktop.

Note: I know this works upto WinXP, and I assume that it will work for Vista and Win7 but I haven't tested it. Using these examples it shouldn't take you long to write a set of functions to get/set the icon positions of everything on the desktop.

procedure ReportStyleDesktop;
var
  wHandle : THandle;
  wStyle : Longint;
begin
  wHandle := GetDesktopWindow;

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'Progman', 'Program Manager');

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'SHELLDLL_DefView', 0);

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'SysListView32', 0);

  if wHandle <> 0 then
  begin
    wStyle := GetWindowLong(wHandle, GWL_STYLE);
    wStyle := wStyle and (not LVS_TYPEMASK);
    wStyle := wStyle or LVS_REPORT or LVS_ICON;
    SetWindowLong(wHandle, GWL_STYLE, wStyle);
  end;
end;

procedure NormalStyleDesktop;
var
  wHandle : THandle;
  wStyle : Longint;
begin
  wHandle := GetDesktopWindow;

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'Progman', 'Program Manager');

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'SHELLDLL_DefView', 0);

  if wHandle <> 0 then
    wHandle := FindWindowEx(wHandle, 0, 'SysListView32', 0);

  if wHandle <> 0 then
  begin
    wStyle := GetWindowLong(wHandle, GWL_STYLE);
    wStyle := wStyle and (not LVS_TYPEMASK);
    wStyle := wStyle or LVS_ICON;
    SetWindowLong(wHandle, GWL_STYLE, wStyle);
  end;
end;

Upvotes: 2

Remus Rigo
Remus Rigo

Reputation: 1464

read this post, maybe it helps :) save-and-restore-desktop-icon-positions

Upvotes: 0

Joe Meyer
Joe Meyer

Reputation: 448

free utility: http://winfuture.de/news,21608.html

Upvotes: 1

Related Questions