views:

129

answers:

2

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 we 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);
A: 

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

Hans Passant
Thanks, this does work. It did not work initially, but I found I had a bad handle for the RadioButton. I did not realize that the Frame surrounding the RadioButtons was actually their parent.
Mark T
A: 
TGadfly
I have tried this command:SendMessage(hwndRBtn, BM_SETSTATE, 1, 0);with no success.
Mark T