通过 user32.dll 在 Listbox 中选择 CheckBoxes? (
Posted
技术标签:
【中文标题】通过 user32.dll 在 Listbox 中选择 CheckBoxes? (【英文标题】:Select CheckBoxes in Listbox via user32.dll? ( 【发布时间】: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);
【问题讨论】:
【参考方案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;
【讨论】:
以上是关于通过 user32.dll 在 Listbox 中选择 CheckBoxes? (的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 WinApi/user32.dll 或类似的东西远程执行 ListBox 的 ListBox1_DoubleClick 事件?
如何在 go 中调用 user32.dll EnumDisplaySettings
如何从 javascript 调用 user32.dll 方法