0

外部プロセスの SysListView32 項目を選択したい。これまでのコードは次のとおりで、機能しています (アイテムのテキストとインデックスを取得できます) が、現在のアイテムを選択するにはどうすればよいですか?

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);
    }
}
4

1 に答える 1

-1

SendMessageWin API 関数を呼び出しLVM_SETITEMSTATEて、メッセージとして指定する必要があります。リストビューのウィンドウ ハンドルと のLVITEM構造体を渡しますlParam

LVITEM構造体で次のフィールドを設定するだけです。

lvi.state = LVIS_FOCUSED | LVIS_SELECTED;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
lvi.iItem = 5 // Index of item to select

状態の値を取得するには、MSDN を確認してください。

于 2013-06-01T23:31:14.163 に答える