bhonneus
asked on
Owner Draw Buttons
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?
You can use SubclassDlgItem as well.
ASKER
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.
Do I need to override the OnPaint from my CButton derived class? Currently, I'm not doing this.
ASKER
Yes, I know. It still doesn't work using either or both. What about my question regarding OnPaint?
Can you provide essential part of your overridden DrawItem() function ?
ASKER
Sure. Here it is. The commented out lines were also tried at various times, without success:
void ODButton::DrawItem(LPDRAWI TEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItem Struct->hD C);
pDC->SetBkColor (RGB(0,255,0)); // Green background
// lpDrawItemStruct->hDC = pDC->Detach();
// SetRedraw(TRUE);
// RedrawWindow();
}
void ODButton::DrawItem(LPDRAWI
{
CDC* pDC = CDC::FromHandle(lpDrawItem
pDC->SetBkColor (RGB(0,255,0)); // Green background
// lpDrawItemStruct->hDC = pDC->Detach();
// SetRedraw(TRUE);
// RedrawWindow();
}
ASKER
Sure. Here it is. The commented out lines were also tried at various times, without success:
void ODButton::DrawItem(LPDRAWI TEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItem Struct->hD C);
pDC->SetBkColor (RGB(0,255,0)); // Green background
// lpDrawItemStruct->hDC = pDC->Detach();
// SetRedraw(TRUE);
// RedrawWindow();
}
void ODButton::DrawItem(LPDRAWI
{
CDC* pDC = CDC::FromHandle(lpDrawItem
pDC->SetBkColor (RGB(0,255,0)); // Green background
// lpDrawItemStruct->hDC = pDC->Detach();
// SetRedraw(TRUE);
// RedrawWindow();
}
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.
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.
ASKER
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,5 0,14
CONTROL "Button1",IDC_BUTTON1,"But ton",BS_OW NERDRAW | WS_TABSTOP,
37,30,62,18
END
I'm not customizing anything in the dialog class except for the OnInitDialog:
BOOL CTestButtonDlg::OnInitDial og()
{
// This is my code
VERIFY(m_odButton.Subclass DlgItem(ID C_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.
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,5
CONTROL "Button1",IDC_BUTTON1,"But
37,30,62,18
END
I'm not customizing anything in the dialog class except for the OnInitDialog:
BOOL CTestButtonDlg::OnInitDial
{
// This is my code
VERIFY(m_odButton.Subclass
// 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.
See SAMPLES\MFC\GENERAL\CTRLTE ST example how to use owner draw buttons. Good luck.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Yes,
I didn't realize I should mention that all the painting needs to be done by yourself.
I didn't realize I should mention that all the painting needs to be done by yourself.
Also use CDC functions to draw wahtever you want inside the control.