• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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?
1
Mindo
Asked:
Mindo
1 Solution
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now