简体   繁体   中英

Unable to get checkbox/radio button state using SendMessage

Situation:

I am trying to use a third-party program using User32.dll's SendMessage. I need to be able to get the state of a checkbox and a radio button.

Using Spy++ to watch the messages, when it is an unchecked checkbox I see "S BM_GETCHECK" and "R BM_GETCHECK fCheck:BST_UNCHECKED" and if I send the command again on a checked checkbox it still says BST_UNCHECKED

retVal is 0 and Marshal.GetLastWin32Error() returns 0 as well

Ideally anything I use would be compatible with WinXP and .NET 2.0

Any help would be appreciated!

Relevant Code:

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 is the window handle for the checkbox/radiobox (confirmed using Spy++), I have also tried 0x00F2 and 0xF0F0 as the second arguments.

Is it a requirement to use SendMessage? This works for me (I used https://bytes.com/topic/net/answers/637107-how-find-out-if-check-box-checked as inspiration):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM