Click here to Skip to main content
15,883,531 members
Please Sign up or sign in to vote.
3.71/5 (5 votes)
See more:
Hello,

I do want to draw round edged button with gradient in back ground

Can anyone help me out?
Posted
Comments
ManjIndian 9-Oct-12 9:43am    
this one is also a single line question.... but seniors will answer for this... not for mine.. what a funny...................

Page 3 of 3


No. it's not working for me.... any other idea?
 
Share this answer
 
Thanks......

Is there any way to make text appearance in button just above bottom border?
 
Share this answer
 
You can not draw something outside of lpDrawItemStruct->rcItem

but you could set a small surface region and a large text region
inside of of lpDrawItemStruct->rcItem :)
 
Share this answer
 
OK.....

One Question: I got everything which i wanted. But now when i am clicking button many times, it is flickering and sometimes some of its area color is appearing grey for very small period of time say in milliseconds.

Can we handle this type of flickering or appearance of grey color?
 
Share this answer
 
v2
Eugen, If i am doing this, I am loosing my Gradient color and i am getting white color in whole button.

So any other idea?
 
Share this answer
 
The idea is already tested:
you should use a memory DC to draw there
and then - copy it to the buttons DC
that is protected with a clipping region :)

Please just copy the code, try it as is
and then modify it (font, ...)

Good luck :)
 
Share this answer
 
I have done exactly same as suggested by you. but i am getting all buttons with white color in background and it is not showing any text on the button.

Tomorrow, again i will check it and do let you know.

Thanks once again for all code which you have given to me.
 
Share this answer
 
Thanks for everything which you have provided to me. :)
 
Share this answer
 
You are welcome :)

(Please implement your validations
of the used pointers and functions results, thank you)
 
Share this answer
 
It is working by me :) :
C++
lpDrawItemStruct->rcItem.top = (lpDrawItemStruct->rcItem.bottom -
                                lpDrawItemStruct->rcItem.top) /2;
strText = _T("this is a long\ntest text...");
pDC->DrawText(strText, &lpDrawItemStruct->rcItem, DT_CENTER|DT_WORDBREAK );
 
Share this answer
 
Here would be the init stage of a button :) :
CRect cRect;
m_cButton.GetWindowRect(cRect);
// CRgn m_cRgn;
m_cRgn.CreateRoundRectRgn(0, 0, cRect.Width(), cRect.Height(), 50, 50);
m_cButton.SetWindowRgn(m_cRgn, TRUE);

Now it is controlled by Windows in this edge.
Secondly, the m_cButton must draw
a gradient surface and a title in the same region :)
 
Share this answer
 
v2
There are two stages :) :
- Setting of the buttons region (ON_WM_CREATE()/at CYourButton::OnCreate())
- Drawing inside the region (CYourButton::OnDrawItem(..))
 
Share this answer
 
No. You do not need to create the buttons explicitly
since they are coming from your resources :)

C++
BEGIN_MESSAGE_MAP(CYouButton, CButton)
    //{{AFX_MSG_MAP(CYourButton)
    ON_WM_CREATE()
...
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()
  
int CYourButton::OnCreate(LPCREATESTRUCT lpcs)
{
  int iResult = CButton::OnCreate(lpcs);
  // Set the buttons region here
...
  return iResult;
}
 
Share this answer
 
Are you sure ? :)
I would use the parents exchange:

class CYourPane : public CBaseOfYourPane
{
  CYourButton m_cButton1;
  CYourButton m_cButton2;
...
};


C++
void CYourPane::DoDataExchange(CDataExchange* pDX)
{
  CBaseOfYourPane::DoDataExchange(pDX);
  // IDC_BUTTON1 is a "property" of the button in the resource view
  DDX_Control(pDX, IDC_BUTTON1, m_cButton1);
  // IDC_BUTTON2 is a "property" of the button in the resource view
  DDX_Control(pDX, IDC_BUTTON2, m_cButton2);
...
}
 
Share this answer
 
Please forget m_cButton :)

int CMyButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CButton::OnCreate(lpCreateStruct) == -1)
    return -1;

  // We are created, now it is time to set our region :)
...
  this->SetWindowRegion(..); // or just SetWindowRegion(..)

  return 0;
}


Please do not modify the buttons IDs in the exchange function -
they must remain the same to "properties" of the buttons in the resource view :)
 
Share this answer
 
Very good :)

You could include the second button in the function
or reset its "Owner Draw" property to "False" :)
 
Share this answer
 
Please check
- The m_Btn1 is from the type CMyButton

- You have the ON_WM_CREATE(); macro in the message map of CMyButton
 
Share this answer
 
Please check
that the property "Owner Draw" is "True"

for the first button only (ID=IDC_BUTTON1) :)
 
Share this answer
 
Eugen, do i have to do all this in "DrawItem()"?
 
Share this answer
 
Eugen,

I having two buttons in my forms. in create(), it is asking for ID. Then i have to call create() for each button by supplying different ID.?
 
Share this answer
 

Page 3 of 3
1 2 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900