Link to home
Start Free TrialLog in
Avatar of Mindo
Mindo

asked on

Creating your own RadioButton ActiveX control

I must create my own RadioButton control. What i need is to change the way it's drawned. We're using stylish controls.

These are the requirements:

1. The control must function like a standard RadioButton control

2. The drawing of the control must provided by me.

3. The control must be an ActiveX control, i.e. it's appearance must be changed at design time.

I see the following solutions:

1. Create one from scratch using MFC. The following difficulty arises to me: how to manage the consistency among several controls(when one of the radiobuttons is selected, others must be deselected).

2. Inherit from the standard RadioButton control and change it's drawing. (How to do this? Which control should i inherit from?)

Can anybody help me? Has anybody done something like this before?
Avatar of bhat
bhat

Use Ownerdraw property for radio button.
Set BS_OWNERDRAWN property
Insert your ActiveX control in your dialog.
Set the BS_OWNERDRAW style when creating your overridden radio button control. In your derived class overried the DrawItem method, similar to below :

void MyRadioButton::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
  ASSERT(lpDrawItemStruct);
  ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
  if (lpDrawItemStruct->itemAction  == ODA_DRAWENTIRE)
    {
      // create an MFC device context for drawing into
      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
      ASSERT(pDC);
      // fill in the requested rectangle, add *your* drawing methods here ...
      pDC->FillRect(lpDrawItemStruct->rctItem);
    }
  // else act on other item actions too ..

}
Avatar of Mindo

ASKER

How to know when my RadioButton is selected and deselected? I need this info to redraw my radio button accordingly.

ODA_* constants do not give this information.
Override CButton::OnSetFocus in your derived radio button class. Then set a member variable for the selected state of your button and call Invalidate to make it redraw the button. When this calls your DrawItem method (as mentioned before) use the member variable to decide what to draw.
Avatar of Mindo

ASKER

I didn't meant to know when i get the Input/Output focus. I did it already by handling the WM_SETFOCUS, WM_KILLFOCUS messages.

I need to know my RadioButton's state. Is there a check on it or not. Not the focus state.
Sorry, I misunderstood the last request.

You can add a boolean member variable which is tied to the radio buttons' state. Do it from the ClassWizard on the member variables tab. If you also need to know when this changes (or is just changing) then override the OnClick method also.
Avatar of Mindo

ASKER

This is the same as creating my RadioButton from scratch. If i derive from the standard RadioButton control, then i assume i can use the member variables of the parent class. It must have a variable which shows if the RadioButton is selected or not. If there is a group of radio buttons, then only one at a time can be selected.
I think you can do everything you need with CButton::GetState() then. It will return focus state, button check/uncheck state, and highlight state.

Something like

if (button->GetState() & 0x0003)
  ; // button is checked
else
 ; // button is not checked

Check the online help for more info on this method.
Avatar of Mindo

ASKER

Since i derive from the parent BUTTON control, i don't have such a member "button" variable in it. Also, i cannot call the GetState() directly from my control. Is there any other way to call this method?
GetState is a member of CButton, so in your derived class you can just call

if (GetState() & 0x0003)

etc etc

I just used button->GetState() so it was obvious it was a member of the button class. Presumably you want to know this information in your draw function so you can control the appearance depending on its selected state.

I hope this now does _everything_ you want, I have a sample project I've created which implements this as I understand your requirements to be.
Avatar of Mindo

ASKER

I've just created an ActiveX control using the ActiveX control Wizard. I've chosen the BUTTON class as my parent class.

Then i add the following function in the Button Click event:
========================================
void CRadioTestCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{      
      if (GetState() & 0x0003)
      {
      
      }

      COleControl::OnLButtonDown(nFlags, point);
}
========================================
The compiler says that the GetState method is undeclared.

Does it work for you?
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial