通过user32.dll在列表框中选择复选框? (

问题描述 投票:2回答:1

我有一个来自外部应用程序的ListBox的句柄。

现在我有一个列表框,其中包含我要通过WinApi选择的x项。

我用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);        
}   
c# windows user32
1个回答
0
投票

我相信我的使用方式将满足您用鼠标单击ListBox来选择一个项目的需求,请尝试一下。


 [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;
            }
        }

© www.soinside.com 2019 - 2024. All rights reserved.