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_GETITEMCOUN T = (LVM_FIRST + 4);
const uint LVM_GETITEMPOSI TION = (LVM_FIRST + 16);
[DllImport("user 32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(stri ng lpClassName, string
lpWindowName);
[DllImport("user 32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(In tPtr parentHandle, IntPtr
childAfter, string className, string windowTitle);
[DllImport("user 32.dll", CharSet=CharSet .Auto)]
public static extern int SendMessage(Int Ptr hWnd, uint Msg, int
wParam, IntPtr lParam);
[DllImport("user 32.dll", CharSet=CharSet .Auto)]
public static extern int SendMessage(Int Ptr hWnd, uint Msg, int
wParam, ref IntPtr lParam);
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
public struct POINT
{
public long x;
public long y;
}
[STAThread]
static void Main(string[] args)
{
IntPtr handle = FindWindow("pro gman", null);
handle = FindWindowEx(ha ndle, IntPtr.Zero, "shelldll_defvi ew",
null);
handle = FindWindowEx(ha ndle, IntPtr.Zero, "syslistview32" , null);
// Get the Number of Icons
int iconCount = SendMessage(han dle, LVM_GETITEMCOUN T, 0,
IntPtr.Zero);
Console.WriteLi ne("Number of Icons on Desktop: " +
iconCount.ToStr ing());
IntPtr ptr = Marshal.AllocHG lobal(Marshal.S izeOf(typeof(PO INT)));
POINT p;
p.x = 0;
p.y = 0;
Marshal.Structu reToPtr(p, ptr, true);
// Get First Icon Position
SendMessage(han dle, LVM_GETITEMPOSI TION, 0, ref ptr);
p = (POINT)Marshal. PtrToStructure( ptr, typeof(POINT));
Console.WriteLi ne("First Icon Position: (x = " + p.x.ToString() + ",
y = " + p.y.ToString() + ")");
Marshal.FreeHGl obal(ptr);
}
}
}