TrevorH
TrevorH

Reputation: 49

How do I find the screen location of all desktop shortcuts?

We have a number of shortcuts that are placed on our customer desktops and like to keep them together for easy use, and away from other shortcuts that they may use for other programs. If they change the screen resolution, monitor or make other changes to the display, the shortcuts become "scrambled".

I found some code examples to find other properties of shortcuts, eg. target, but I didn't see any properties that indicate where the shortcut is physically located on the screen.

I was thinking of creating a simple program that reads the location of our shortcuts and saves this to a file when they are in the correct locations. A simple button press would allow them to be restored to their correct places.

I am programming in Delphi, we are using W7-10 on various customer machines.

Upvotes: 1

Views: 1618

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595305

The Desktop is implemented as a standard Win32 ListView control in icon mode. The items on the Desktop are ListView items. You can use the GetDesktopWindow() function to get the HWND of the Desktop's ListView and then use standard ListView messages to manipulate it as needed.

There is no API to retreive the Desktop ListView items that represent specific files/shortcuts, so you will have to look for them manually. Either by:

  1. looping through the items using LVM_GETITEMCOUNT and LVM_GETITEMTEXT, comparing each item's text to your shortcut names.

  2. using LVM_FINDITEM to search for the item with the same text as a given shortcut name.

Once you have found the intended items, you can then use LVM_GETITEMPOSITION and LVM_SETITEMPOSITION/LVM_SETITEMPOSITION32 to get/set the item positions as needed.


UPDATE:

Now, with that said... the fact that the Desktop uses a ListView for its icons is an implementation detail that you should not rely on, as it would eventually break:

A reminder about the correct way of accessing and manipulating the position of icons on the desktop

Starting in Windows 10 version 1809, there were changes to the way that the positions of the desktop icons are managed, and one of the consequences is that if you try to manipulate the icon positions by talking directly to the ListView control, those changes aren’t taken into account by the icon management code

The recommended approach is to use IFolderView::GetItemPosition() instead:

Manipulating the positions of desktop icons

Okay, we already have enough to be able to enumerate all the desktop icons and print their names and locations.

...

After getting the IFolderView, ... We ask the view for its Items enumeration, then proceed to enumerate each of the items. For each item, ... , and we ask the IFolder­View for its position. Then we print the results.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612784

The shell API provides functionality for this. The key interface is IFolderView, and in particular the methods IFolderView::GetItemPosition and IFolderView::SelectAndPositionItems.

Using the shell API is somewhat involved. As always, it takes to fair amount of scaffolding before you get to call these methods. I refer you to Raymond Chen's article, Manipulating the positions of desktop icons, which gives example code to do this.

Upvotes: 3

Related Questions