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?
LVL 2
MindoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bhatCommented:
Use Ownerdraw property for radio button.
Set BS_OWNERDRAWN property
0
vachoohoCommented:
Insert your ActiveX control in your dialog.
0
s_stirleyCommented:
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 ..

}
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

MindoAuthor Commented:
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.
0
s_stirleyCommented:
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.
0
MindoAuthor Commented:
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.
0
s_stirleyCommented:
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.
0
MindoAuthor Commented:
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.
0
s_stirleyCommented:
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.
0
MindoAuthor Commented:
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?
0
s_stirleyCommented:
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.
0
MindoAuthor Commented:
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?
0
mikeblasCommented:
> The compiler says that the GetState method is undeclared.

Since you're subclassing a button control, you don't get the GetState() member. In MFC, GetState() is a member of CButton and not COleControl.

You can cast the this pointer safely to use the other window types:

  CButton* pButton = (CButton*) this;
  pButton->GetState();

..B ekiM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.