Hi,
I'd like to be able to read the positions of all the Icons on my WinXP
desktop. I've had a look around to see how to do it and I've come up
with the code below...but it doesn't work. :o(
The x and y properties of my POINT structure are always zero. It seems
the structure isn't getting updated at all from the SendMessage
function and so the code just displays the default values.
Any suggestions?
Thanks in advance, AF.
-----------------
using System;
using System.Runtime.InteropServices;
namespace DesktopIcons
{
class GetInfo
{
const uint LVM_FIRST = 0x1000;
const uint LVM_GETITEMCOUNT = (LVM_FIRST + 4);
const uint LVM_GETITEMPOSITION = (LVM_FIRST + 16);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, string windowTitle);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int
wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int
wParam, ref IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct POINT
{
public long x;
public long y;
}
[STAThread]
static void Main(string[] args)
{
IntPtr handle = FindWindow("progman", null);
handle = FindWindowEx(handle, IntPtr.Zero, "shelldll_defview",
null);
handle = FindWindowEx(handle, IntPtr.Zero, "syslistview32", null);
// Get the Number of Icons
int iconCount = SendMessage(handle, LVM_GETITEMCOUNT, 0,
IntPtr.Zero);
Console.WriteLine("Number of Icons on Desktop: " +
iconCount.ToString());
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(POINT)) );
POINT p;
p.x = 0;
p.y = 0;
Marshal.StructureToPtr(p, ptr, true);
// Get First Icon Position
SendMessage(handle, LVM_GETITEMPOSITION, 0, ref ptr);
p = (POINT)Marshal.PtrToStructure(ptr, typeof(POINT));
Console.WriteLine("First Icon Position: (x = " + p.x.ToString() + ",
y = " + p.y.ToString() + ")");
Marshal.FreeHGlobal(ptr);
}
}
}