简体   繁体   中英

RadioButton click on another form

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.

But

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

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

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

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

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