RadioButton点击另一个表单

发布于 2024-08-24 01:20:05 字数 1825 浏览 1 评论 0原文

我们有一个带有 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);

We have a legacy program with a GUI that we want to use under control of a C# program to compute some values. We can successfully enter values in the numerical input controls, press the compute button, and read the produced answers from text display boxes.

Butwe can't seem to control a pair of radio buttons .

Calling CheckRadioButton() returns a code of success, but the control does not change state.
Sending a message of BM_CLICK does not change the state.
Attempts at sending WM_LBUTTONDOWN and WM_LBUTTONUP events haven't changed the state.

Has anyone been successful at "remote control" of radio buttons?

Portions of code to illustrate what we are doing:

[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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-08-31 01:20:05

发送BM_SETCHECK 消息。请务必使用 Spy++ 等工具来查看消息。

Send the BM_SETCHECK message. Be sure to use a tool like Spy++ to see the messages.

找回味觉 2024-08-31 01:20:05

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

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

in this case i used another message BM_SETSTATE

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文