Solved

Owner Draw Buttons

Posted on 1997-06-26
14
1,862 Views
Last Modified: 2013-11-19
If I give a button an ownerdraw property, how do I get it to display itself within my dialog box?  I've got a DrawItem routine in my CButton inherited class where I do my customization, but I can't seem to get the button to draw itself.  Is there some basic code necessary for displaying a plain vanilla owner draw button apart from any customization?
0
Comment
Question by:bhonneus
14 Comments
 
LVL 2

Expert Comment

by:rajesh032097
ID: 1302316
Did you use SubClassWindow() function in your OnInitDialog() to associate your own button class with the control?
Also use CDC functions to draw wahtever you want inside the control.

0
 
LVL 2

Expert Comment

by:rajesh032097
ID: 1302317
You can use SubclassDlgItem as well.
0
 

Author Comment

by:bhonneus
ID: 1302318
Yes, I did.  I also tried (separately) using the Data Exchange macro which should have been equivalent.  In both cases my DrawItem function got called but the button was not drawn (sorry I didn't clarify this before).

Do I need to override the OnPaint from my CButton derived class?  Currently, I'm not doing this.
0
 

Author Comment

by:bhonneus
ID: 1302319
Yes, I know.  It still doesn't work using either or both.  What about my question regarding OnPaint?
0
 
LVL 2

Expert Comment

by:rajesh032097
ID: 1302320
Can you provide essential part of your overridden DrawItem() function ?
0
 

Author Comment

by:bhonneus
ID: 1302321
Sure.  Here it is.  The commented out lines were also tried at various times, without success:

void ODButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
      
      pDC->SetBkColor (RGB(0,255,0)); // Green background
//      lpDrawItemStruct->hDC = pDC->Detach();
//      SetRedraw(TRUE);
//      RedrawWindow();
}

0
 

Author Comment

by:bhonneus
ID: 1302322
Sure.  Here it is.  The commented out lines were also tried at various times, without success:

void ODButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
      
      pDC->SetBkColor (RGB(0,255,0)); // Green background
//      lpDrawItemStruct->hDC = pDC->Detach();
//      SetRedraw(TRUE);
//      RedrawWindow();
}

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:rajesh032097
ID: 1302323
The code seems right, and is working fine in my application.
You don't have to override OnPaint().
Please reject the answer.
I think you should put more light on what's happening exactly because it's very difficult to make out what might have gone wrong with these details.

 
0
 

Author Comment

by:bhonneus
ID: 1302324
Ok.  I've created a sample app with MFC 4.2 which is a simple dialog box with an owner draw button.  The resource code excerpt follows:

IDD_TESTBUTTON_DIALOG DIALOGEX 0, 0, 185, 92
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "TestButton"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,128,7,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,128,23,50,14
    CONTROL         "Button1",IDC_BUTTON1,"Button",BS_OWNERDRAW | WS_TABSTOP,
                    37,30,62,18
END

I'm not customizing anything in the dialog class except for the OnInitDialog:

BOOL CTestButtonDlg::OnInitDialog()
{
     // This is my code
      VERIFY(m_odButton.SubclassDlgItem(IDC_BUTTON1, this));
     // This is my code

//      CDialog::OnInitDialog();

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}

My ODButton class just has one overridden function which is the DrawItem.  When I run the app, the dialog box comes up without the IDC_BUTTON1 drawn.

0
 
LVL 4

Expert Comment

by:AVaulin
ID: 1302325
See SAMPLES\MFC\GENERAL\CTRLTEST example how to use owner draw buttons. Good luck.
0
 

Author Comment

by:bhonneus
ID: 1302326
I just tried modifying the CTRLTEST example.  My sample app is using a CButton derived class, not CBitmapButton which CTRLTEST uses.  So I thought that by changing one of the buttons in CTRLTEST to CButton that I should be able to get it to work and then compare the code to my app.  It didn't work.

I changed one of the CBitmapButton objects in the OwnerButton Test1 dialog to a CButton.  I kept it owner draw and removed all code which dealt with the bitmaps to that button.  Running the test caused an assertion failure and failed to draw the button.

I've upped the points to 100 since this seems like a more difficult problem than I originally anticipated.
0
 

Accepted Solution

by:
rpb earned 50 total points
ID: 1302327
Here is some code that works for me:

void CColourButton::DrawItem(LPDRAWITEMSTRUCT lpDrawStruct)
{
   CDC* pDrawDC = CDC::FromHandle(lpDrawStruct->hDC);
      
   UINT state = lpDrawStruct->itemState;
   if ((state & ODS_SELECTED))
      DrawButtonDown(pDrawDC);
   else
      DrawButtonUp(pDrawDC);
}

And then in each of these functions you can do something like:


void CColourButton::DrawButtonUp(CDC* pDC)
{
   CBrush newBrush;
   CBrush* pOldBrush = NULL;
   CPen drawPen(PS_SOLID, 1, RGB(128, 128, 128));

   // This is a member variable in this case
   GetWindowRect(&m_buttonSize);

   // Draw a nice round rectangle in light grey
   pDC->SelectObject(GetStockObject(LTGRAY_BRUSH));
   pDC->SelectObject(GetStockObject(BLACK_PEN));
   pDC->RoundRect(0, 0, m_buttonSize.Width(), m_buttonSize.Height(), 4, 4);

   // This is the colour of our brush (another member variable)
   newBrush.CreateSolidBrush(m_colour);

   pOldBrush = (CBrush *) pDC->SelectObject(&newBrush);
   pDC->Rectangle(7, 7, m_buttonSize.Width() - 6, m_buttonSize.Height() - 6);
   pDC->SelectObject(pOldBrush);

   // You could now draw the 3D lines if required...
}

Then in your resource file you will want something like this:

   CONTROL "Colour...",ID_COL_BUTTON,"Button",BS_OWNERDRAW | WS_GROUP | WS_TABSTOP,45,145,39,14

This should then draw a grey button with a coloured rectangle in the middle
0
 

Author Comment

by:bhonneus
ID: 1302328
Thanks.  I didn't realize that I had to explicitly draw the button step by step.  What everyone else told me up to now seemed to indicate that Windows had all the info it needed.  I'll play around with this and see what I can come up with!
0
 
LVL 2

Expert Comment

by:rajesh032097
ID: 1302329
Yes,
I didn't realize I should mention that all the painting needs to be done by yourself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
sameEnds challenge 3 135
isEverywhere  challenge 19 72
Trying to run powershell  in a batch file. How do I do this? 8 67
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now