How can I place an Small Icon in a button ?
Hi,
I have tried with SetIcon to put an small icon ( 16x16) on a button, but the problem, is that it puts there as a big icon, mmm..., and the problem is that seticon with parameter for big or small icons are in the CWindow class not in the CButton, so..., what can I do ?, I do:
m_pbWrite.SetIcon(AfxGetApp()->LoadIcon(IDI_WRITEU),FALSE);
Thanks, Bye !
Re: How can I place an Small Icon in a button ?
I'm afraid the only way I know of is to subclass the button and paint the icon on it yourself. To do this, take the following steps:
1. Remove the existing call to SetIcon() for this button, but leave the Icon style. If this button is in the dialogue resource and you have it mapped to a member variable of type CButton, REMOVE this mapping. You will be able to refer to the button via its subclassed control.
2. From the ClassWizard, derive a new class based on CButton. Call it CSmallIconButton, for example.
3. In this new class, override the OnPaint() method (WM_PAINT message) and put the following:
---
CClientDC *pDC ;
CRect rctClient ;
HICON hIcon ;
int iLeft, iTop ;
CButton::OnPaint(); // Paint a normal button.
GetClientRect(&rctClient);
iLeft = (rctClient.Width() - 16) / 2 ;
iTop = (rctClient.Height() - 16) / 2 ;
hIcon = AfxGetApp()->LoadIcon(IDI_WRITEU);
pDC = new CClientDC(this);
::DrawIconEx(pDC->GetSafeHdc(), iLeft, iTop, hIcon, 16, 16, 0, NULL, 0);
delete pDC ;
---
4. In your dialogue class' header file, add a private member variable:
---
.
.
.
private:
CSmallIconButton m_btnWrite ;
.
.
.
---
5. In your dialogue class' code file, in OnInitDialog(), add this line:
---
m_btnWrite.SubclassDlgItem(IDC_WRITE, this);
---
(where IDC_WRITE is the ID of your button). If your button is created at run time rather than already present in the dialogue resource script, then this line should come after the button is created (which should be done as normal).
You can now refer to your button as m_btnWrite (object) rather than m_pbWrite (pointer to object); so you may need to change all references to m_pbWrite.
Note that this always draws the icon in the same place on the button, even when the button is pressed; this will look slightly odd. If you want to cure that, you will have to trap the mouse down and up events and use a BOOL member variable to track the button pressed state. If the button is pressed, adjust the iLeft and iTop parameters by 1 or 2 to the right and down to match the button change. If you want further help with that, let me know at [email protected]
Does this help?