【问题标题】:Select CheckBoxes in Listbox via user32.dll? (通过 user32.dll 在 Listbox 中选择 CheckBoxes? (
【发布时间】:2019-03-12 08:27:06
【问题描述】:

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

现在我有一个包含 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);        
}   

【问题讨论】:

    标签: c# windows user32


    【解决方案1】:

    相信我的使用方式能满足你在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;
                }
            }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-27
      • 2012-03-23
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2015-03-30
      相关资源
      最近更新 更多