【问题标题】:RadioButton click on another form单选按钮单击另一个窗体
【发布时间】:2010-03-05 03:21:44
【问题描述】:

我们有一个带有 GUI 的遗留程序,我们希望在 C# 程序的控制下使用它来计算一些值。我们可以成功地在数值输入控件中输入值,按下计算按钮,并从文本显示框中读取生成的答案。

但是我们似乎无法控制一对单选按钮。

调用 CheckRadioButton() 返回成功代码,但控件不改变状态。 发送 BM_CLICK 消息不会改变状态。 尝试发送 WM_LBUTTONDOWN 和 WM_LBUTTONUP 事件并未改变状态。

有没有人成功地“远程控制”单选按钮?

部分代码来说明我们在做什么:

[DllImport("user32.dll", EntryPoint="SendMessage")]
public static extern int SendMessageStr(int hWnd, uint Msg, int wParam, string lParam);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, long wParam, long lParam);

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError=true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", EntryPoint="CheckRadioButton")]
public static extern bool CheckRadioButton(IntPtr hwnd, int firstID, int lastID, int checkedID);

static IntPtr GetControlById(IntPtr parentHwnd, int controlId) {
  IntPtr child = new IntPtr(0);
  child = GetWindow(parentHwnd, GetWindow_Cmd.GW_CHILD);
  while (GetWindowLong(child.ToInt32(), GWL_ID) != controlId) {
    child = GetWindow(child, GetWindow_Cmd.GW_HWNDNEXT);
    if (child == IntPtr.Zero) return IntPtr.Zero;
  }
  return child;
}

// find the handle of the parent window
IntPtr ParenthWnd = new IntPtr(0);    
ParenthWnd = FindWindowByCaption(IntPtr.Zero, "Legacy Window Title");

// set "N" to 10
IntPtr hwndN = GetControlById(ParenthWnd, 17);
SendMessageStr(hwndN.ToInt32(), WM_SETTEXT, 0, "10");

// press "compute" button (seems to need to be pressed twice(?))
int hwndButton = GetControlById(ParenthWnd, 6).ToInt32();
SendMessage(hwndButton, BM_CLICK, 0, 0);
SendMessage(hwndButton, BM_CLICK, 0, 0);

// following code runs succesfully, but doesn't toggle the radio buttons
bool result = CheckRadioButton(ParenthWnd, 12, 13, 12);

【问题讨论】:

    标签: c# click radio-button


    【解决方案1】:

    发送BM_SETCHECK message。请务必使用 Spy++ 之类的工具来查看消息。

    【讨论】:

    • 谢谢,这确实有效。它最初不起作用,但我发现我对 RadioButton 的处理不好。我没有意识到 RadioButtons 周围的 Frame 实际上是它们的父级。
    【解决方案2】:

    在这种情况下,我使用了另一条消息 BM_SETSTATE

    SendMessage((IntPtr)hWnd, Win32Api.BM_SETSTATE, (IntPtr)newState, IntPtr.Zero);

    【讨论】:

    • 我试过这个命令:SendMessage(hwndRBtn, BM_SETSTATE, 1, 0);没有成功。
    猜你喜欢
    • 2017-07-13
    • 2015-11-24
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多