使用SendMessage

时间:2018-10-04 02:36:55

标签: c# winapi pinvoke user32

情况:

我正在尝试通过User32.dll的SendMessage使用第三方程序。我需要能够获得复选框和单选按钮的状态。

使用Spy ++观看消息时,如果未选中该复选框,则会看到“ S BM_GETCHECK”和“ R BM_GETCHECK fCheck:BST_UNCHECKED”,并且如果我再次在选中的复选框上发送命令,它仍然显示BST_UNCHECKED

retVal为0,Marshal.GetLastWin32Error()也返回0

理想情况下,我使用的任何东西都将与WinXP和.NET 2.0兼容

任何帮助将不胜感激!

相关代码:

using System;
using System.Runtime.InteropServices;

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

int retValB = (int)SendMessage(hWnd, 0x00F0, IntPtr.Zero, IntPtr.Zero);

hWnd是复选框/单选框的窗口句柄(已使用Spy ++确认),我也尝试将0x00F2和0xF0F0作为第二个参数。

1 个答案:

答案 0 :(得分:0)

是否需要使用 SendMessage?这对我有用(我使用 https://bytes.com/topic/net/answers/637107-how-find-out-if-check-box-checked 作为灵感):

using Accessibility;

[DllImport("oleacc.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);

public static Nullable<bool> isCheckBoxChecked(IntPtr checkBoxHandle)
{
    const UInt32 OBJID_CLIENT = 0xFFFFFFFC;
    const int UNCHECKED       = 1048576;
    const int CHECKED         = 1048592;
    Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");

    IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(checkBoxHandle, OBJID_CLIENT, ref uid);
    object o2 = accObj.get_accState(0);
    if ((int)o2 == UNCHECKED)
    {
        return false;
    }
    else if ((int)o2 == CHECKED)
    {
        return true;
    }
    else
    {
        return null;
    }
}