I want to select the SysListView32 item of an extrenal process. Here's my code so far, and it's working (I can get the text and index of the item), but how can I select the current item?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Place your cursor over listview and hit enter...");
Console.ReadLine();
POINT pt;
GetCursorPos(out pt);
IntPtr hwnd = WindowFromPoint(pt);
AutomationElement el = AutomationElement.FromHandle(hwnd);
TreeWalker walker = TreeWalker.ContentViewWalker;
int i = 0;
for (AutomationElement child = walker.GetFirstChild(el);
child != null;
child = walker.GetNextSibling(child))
{
MessageBox.Show("Item:" + child.Current.Name);
//! Select The Item Here ...
}
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
};
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(POINT pt);
[DllImport("user32.dll")]
private static extern int GetCursorPos(out POINT pt);
}
}
You'll have to call
SendMessage
Win API function and specifyLVM_SETITEMSTATE
as your message. Pass in the window handle of your listview and aLVITEM
struct forlParam
.You only need to set the following fields in the
LVITEM
structCheck MSDN to get their values of the states.