C# 是否通过user32.dll在列表框中选择复选框?(

C# 是否通过user32.dll在列表框中选择复选框?(,c#,windows,user32,C#,Windows,User32,我有一个来自外部应用程序的列表框句柄 现在我有了一个包含x个项目的列表框,我想用WinApi来选择这些项目 我用SETCURSEL试过,但不幸的是,它不起作用: private void button2_Click(object sender, EventArgs e) { IntPtr chldWnd = NativeMethods.FindWindow("#32770", "Ansichten einfügen"); IntPtr ListBoxHandle = Native

我有一个来自外部应用程序的列表框句柄

现在我有了一个包含x个项目的列表框,我想用WinApi来选择这些项目

我用SETCURSEL试过,但不幸的是,它不起作用:

private void button2_Click(object sender, EventArgs e)
{
    IntPtr chldWnd = NativeMethods.FindWindow("#32770", "Ansichten einfügen");
    IntPtr ListBoxHandle = NativeMethods.FindWindowEx(chldWnd, IntPtr.Zero, "ListBox", null);
    //MessageBox.Show(ButtonHandle.ToString());
    NativeMethods.SendMessageInt(ListBoxHandle, NativeMethods.CB_SETCURSEL, 1, 2);
}

static class NativeMethods
{
    public const int BM_CLICK = 0x00F5;
    public const int WM_SETTEXT = 0x000C;
    public const int VK_DOWN = 0x28;
    public const int WM_KEYDOWN = 0x100;
    public const int LB_SETSEL = 0x0185;
    public const int CB_SETCURSEL = 0x014E;

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
    [DllImport("user32.dll", EntryPoint="PostMessage" ,CharSet = CharSet.Unicode)]
    public static extern IntPtr SendMessageInt(IntPtr hWnd, uint Msg, int wParam, int lParam);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr PostMessage(IntPtr hwnd, int wsg, IntPtr wParam, String lParam);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        
}   

我相信我使用它的方式将满足您通过鼠标点击列表框来选择项目的需要,请尝试一下


 [DllImport("user32.dll")]
 private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

 public void SetListItem(string windowTitle, int index, int item)
        {
            try
            {
                var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);
                var childWindows = GetChildWindows(windowHWnd);
                const int LB_SETCURSEL = 0x0186;
                const int downCode = 0x201;
                const int upCode = 0x202;
                IntPtr lParam = (IntPtr)9999; // The coordinates
                IntPtr wParam = IntPtr.Zero;

                SendMessage(childWindows.ToArray()[index], LB_SETCURSEL, item, "0");

                SendMessage(childWindows.ToArray()[index], downCode, wParam, lParam); // Mouse button down
                SendMessage(childWindows.ToArray()[index], upCode, wParam, lParam);
            }
            catch (Exception)
            {
                throw;
            }
        }