Creating Windows XP Style ActiveX Button

Environment: VC6 SP4, NT4 SP3

Creating an ActiveX control is very simple if you use MFC wizard. Here I explain how to create a simple ActiveX button control in VC++ that looks like a Windows XP buttons.

Create a new project by selecting the MFC ActiveX control wizard. Give it the name "XpButtonEx." There are two steps in the ActiveX control wizard. In the first dialog box select one control, no runtime licensing, source-file comments, and no Help files. In the second dialog box select Activates When Visible, Available in "Insert Object" Dialog, and has an "About Box." Select "BUTTON" in the combo-box where the wizard asks: "Which window class, if any, should this control subclass?" Click Finish. AppWizard will create some 19 files. There are 3 classes—CxpButtonExApp, CxpButtonExCtrl, and CxpButtonExPropPage.

Now open the class wizard. Make sure that the selected class name is "CXpButtonExCtrl." Add the message map entries for WM_CREATE, WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE. Right-click "CXpButtonExCtrl" in the Class view tab and add the virtual function PreSubclassWindow. Add the line:



in the PreSubclassWindow function. Now open XpButtonEx.h and add the following member variables and functions.

CPen *pBoundryPen;
CPen *pInsideBoundryPenLeft;
CPen *pInsideBoundryPenTop;
CPen *pInsideBoundryPenRight;
CPen *pInsideBoundryPenBottom;
CPen *pOldPen;
CBrush *pFillActive;
CBrush *pFillInactive;
CBrush *pOldBrush;
BOOL m_bOverControl;
void DoGradientFill(CDC *pDC, CRect rect);
void DrawInsideBorder(CDC *pDC, CRect rect);

Open XpButtonEx.cpp. In the constructor function CXpButtonExCtrl(), add the following:

  m_bOverControl = FALSE;
  pBoundryPen = new CPen(PS_INSIDEFRAME|PS_SOLID,1,RGB(0,0,0));
  pInsideBoundryPenLeft = new CPen(PS_INSIDEFRAME|
  pInsideBoundryPenRight = new CPen(PS_INSIDEFRAME|
  pInsideBoundryPenTop = new CPen(PS_INSIDEFRAME|
  pInsideBoundryPenBottom = new CPen(PS_INSIDEFRAME|
  pFillActive = new CBrush(RGB(222,223,236));
  pFillInactive = new CBrush(RGB(222,223,236));

And in the destructor ~CXpButtonExCtrl(), delete the objects:


Add the following functions in XpButtonCtl.cpp:

void CXpButtonExCtrl::DoGradientFill(CDC *pDC, CRect rect)
    CBrush* pBrush[64];
    for (int i=0; i<64; i++)
      pBrush[i] = new CBrush(RGB(253-(i/2),
    int nWidth = (rect.right) - (rect.left);
    int nHeight = (rect.bottom) - (;
    CRect rct;

    for (; i < nHeight+2; i++)
        rct.SetRect (rect.left, i, nWidth+2, i + 1);
        pDC->FillRect (&rct, pBrush[(i * 63) / nHeight]);

    for (i=0; i<64; i++)
        delete pBrush[i];

void CXpButtonExCtrl::DrawInsideBorder(CDC *pDC,CRect rect)
  pOldPen = pDC->SelectObject(pInsideBoundryPenLeft);

Now go to the OnOcmCommand() and add the following switch statement before return 0.

  case BN_CLICKED:  // The click event should be fired
                    // when the button is clicked.

We are using the Boolean variable m_bOverControll for tracking the mouse position. The button will receive WM_MOUSEMOVE when the mouse is over the button. Change the code of OnMouseMove to the following:

void CXpButtonExCtrl::OnMouseMove(UINT nFlags, CPoint point)
  // TODO: Add your message handler code
  // here and/or call default.

  COleControl::OnMouseMove(nFlags, point);
   m_bOverControl = TRUE;
   tm.cbSize = sizeof(tm);
   tm.dwFlags = TME_LEAVE;
   tm.hwndTrack = this->m_hWnd;

Now, for detecting when the mouse leaves the button, we should manually add the following message handlers. Add


in XpButtonExCtl.h and add


in XpButtonExCtl.cpp. Add the function

   m_bOverControl = FALSE;
  return 0;

in XpButtonExCtl.cpp. Now, to draw the button, we should manually add the handler for the OCM_DRAWITEM message. Add LRESULT OnOcmDrawItem(WPARAM wParam, LPARAM lParam); in XpButtonExCtl.h and add ON_MESSAGE(OCM_DRAWITEM, OnOcmDrawItem) in XpButtonExCtl.cpp. Add the function OnOcmDrawItem in XpButtonEx.cpp.

LRESULT CXpButtonExCtrl::OnOcmDrawItem(WPARAM wParam,
                                       LPARAM lParam)
  UINT nIDCtl = (UINT) wParam;
  LPDRAWITEMSTRUCT lpDrawItemStruct = 
                            (LPDRAWITEMSTRUCT) lParam;
  CDC* pDC   = CDC::FromHandle(lpDrawItemStruct->hDC);
  CRect rect = lpDrawItemStruct->rcItem;
  UINT state = lpDrawItemStruct->itemState;
    // draw the control edges
  CPoint pt;
  pt.x = 10;
  pt.y = 10;
    pOldPen = pDC->SelectObject(pBoundryPen);
    if (state & ODS_SELECTED)
    // Deflate the drawing rect by the size of the button's edges
    rect.DeflateRect( CSize(GetSystemMetrics(SM_CXEDGE),
    // Fill the interior color if necessary

   if (m_bOverControl)
      pOldBrush = pDC->SelectObject(pFillActive);
      pOldBrush = pDC->SelectObject(pFillInactive);
      // Draw the text
    if (!m_title.IsEmpty())
        CSize Extent = pDC->GetTextExtent(m_title/*strText*/);
        CPoint pt( rect.CenterPoint().x -, 
        rect.CenterPoint().y - );

        if (state & ODS_SELECTED) 

        int nMode = pDC->SetBkMode(TRANSPARENT);
        CFont *pOldFont = SelectStockFont( pDC );

        if (state & ODS_DISABLED)
            pDC->DrawState( pt,
            pDC->TextOut(pt.x, pt.y, m_title);
  return 0;

Now we should add two properties to our ActiveX Control. One is Tile and the other is Font. Open Class Wizard and select the Automation tab. Make sure that the selected class name is CxpButtonEx. Click on the "Add Property" button. In the Add Property dialog box, give it an external name such as "title" and select the type as Cstring. Accept the default names for the variable and function. Click OK. Close the class wizard by clicking OK. Add the following line in the DoPropExchange function in XpButtonExCtl.cpp after the TODO: comment:

PX_String( pPX, _T("title"), m_title, _T("Caption"));

Now go to the Resourses view tab and open the dialog template IDD_PROPPAGE_XPBUTTONEX. Remove the "TODO:..." statement. Add one edit box, IDC_TITLE. Now open the Class wizard and select the Member Variables tab. Select the class CxpButtonExPropPage. Select IDC_TITLE and click Add Variable. In the Add Member variable dialog box, give Member Variable a name such as m_title, Category - Value, Variable Type - CString, and give the Optional Property a name such as "title." Click OK. Close the class wizard by clicking OK.

Now we can add the stock property Font. Open Class Wizard and select the Automation tab. Make sure that the selected class name is CxpButtonEx. Click on the "Add Property" button. In the Add Property dialog box, select the external name as Font in the combo box. Click OK. Close the class wizard by clicking OK. Now we can set up a property page for the font. This is really simple because we can use a prewritten property page. Open XpButtonExCtl.cpp and find the following code.


In this, change the count to 2, and add another PROPPAGEID. The new code will be like this:


Now our control is ready. Test it in an ActiveX test container.


Download demo project - 96.9 Kb
Download source - 5.48 Kb


  • Hvad ville livet til at komme vil være mere farverigt Studio

    Posted by wanzixiao on 06/04/2013 09:21am

    [url=]beats by dre danmark[/url] Beats “Supreme Sound” Headsets forskydninger, der er valgt i form af vej erklæring – selv om det faktum, at faktisk syntes at tale om! “Jeg er en god overdreven person i hans teenageår” The hovedtelefon anker var oprindeligt strålende sammen med betydelig med hensyn til neon . Men i tidligere tider flere aldre, har kunderne allerede blevet anvendt som vil high-end konkurrerende virksomheder anvender personlig bank kan synes, sammen med Beats indgået et system var oprindeligt det meste af tydeligt. Artiklen udformet en slags in-house industri team – tidligere var næsten hele pakken jobbet oprindeligt dyrket ud – at drage fordel af luftstrøm sammen med forbedre enhver mp3. Den populære sti i disse dage har en god imprintede Great Tone. [url=]beats by dre tilbud[/url] Et emne BIII booo kunne udrette ved hjælp af din adeptness sammen med aktivitet indenfor en ny og mere omfattende midler sammen med komplet inkluderet tilsætning i fald rigdom elektrisk strøm. Dit hygge del i de sande hovedtelefoner er dybest set vidunderligt. Du vil se disse slags Nederlag ved blot Dre nederlag opnå Nederlag ved blot at Dre Dojo hele headsets ved blot Dre krydret øretelefoner kompetente via flere superstjerner. Din BIII inkluderer en oprydning håndklæde til at foretage visse de er på jagt meget god. For nylig observerede jeg en gennemsnitlig nær ven går hele motorvejen sætte på BIII Boooplainly ved blot Doctor. Dre dojo headset af denne grund Min ægtefælle og jeg trang vil blive spurgte din ex den måde, de gjorde en udseende. [url=]beats by dre danmark[/url] Hestekræfter samt reggae hele verden har kombineret såvel som resultatet er den nye Doctor Dre laptop. Med ferier her og også Dr. Dre laptop annoncer viser konstant på tv, er alle spekulerer på, om de skal købe det faktiske H. s.. Misundelse (den bestemte specialiserede navn med den personlige computer).Over-the-ear hovedtelefoner er fantastisk for ejendom bliver opmærksomme samt vejledning i at lukke ud lyde som du sover. On-ear typer bør også være behageligt, sammen med delikat ekstra polstring og robust konstruktion. Bare om de mest væsentlige ting sammen med on-ear design er at sikre, at de er installeret til brug sammen med stikkene i dine gadgets.

  • A good ActiveX lesson

    Posted by Jay Tautges on 12/07/2004 02:30am

    I've been trying to get into ActiveX programming for a while, and this article was a great help. Thanks

  • buttons in winxp

    Posted by Legacy on 01/22/2004 12:00am

    Originally posted by: rana

    how can i make the style of buttons in Win Xp in my project in vb6


  • full source please ;0

    Posted by Legacy on 10/25/2003 12:00am

    Originally posted by: george

    can you please post teh full source. i followed your instructions word by word and all i ever get is a grey label and no button where am i goign wrong.

  • Very Good Code with small bug.

    Posted by Legacy on 10/03/2003 12:00am

    Originally posted by: Stephen Murphy

    This is some great code and offers a nice result. Thank you for posting it! I intend on using it in a future project that needs some cool buttons.

    One big problem I found was placing the new button control on a form in Access 2000, and then assigning some code to it which _closes_ the form. This will result in a fatal error which kills Access on the spot. I have traced the problem to the destructor lines:


    This makes sense because in some situations, those objects may not even exist any more. They are pointers to objects in the device context which were there already. The button control held on to them only briefly while using its own brushes (which should be deleted). This control should not try to delete things it doesn't own.

    Having commented out these lines, the button runs wonderfully. They look great with some 256 colour icons drawn off to one side with the text on the other. I thank you again for posting some great material for others to benefit from.

  • This is good work you boneheads!

    Posted by Legacy on 07/12/2003 12:00am

    Originally posted by: rep

    Those that think this article is outdated, worthless, whatever, what are you talking about?

    I like this article, especially for doing XP style buttons
    on *Win9x/2k*, and for projects w/o MFC! The manifest trick only works on XP w/ MFC. This code works on whatever platform I want!

    Good job on a well written article.

  • Nice

    Posted by Legacy on 03/16/2003 12:00am

    Originally posted by: Rama Krishna

    Codeing is very nice and a very new idea.Congatulations.

  • Memory Leak ?

    Posted by Legacy on 03/11/2003 12:00am

    Originally posted by: Benjamin Wang

    When the app is first run, it uses 2500k memory, after i do some opration(just click it and mouse over), the memory useage it up to 3076k. is it a memory leak?

  • Very good!I like it.

    Posted by Legacy on 10/13/2002 12:00am

    Originally posted by: mjc0513

    it is very good,I am doing it.

  • Very Nice! It's OK although have some small bug..

    Posted by Legacy on 09/24/2002 12:00am

    Originally posted by: qfly

    I test it,and it's very nice.Thanks anyway.(need to modify your article to correct some words that will lead someone have errors in their projects like some class's name)

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date