-
October 6th, 2004, 12:54 PM
#1
Buttons and Regions
I am using Regions(HRGN) to create irregular buttons but this doesn't work. Can anybody explain why?
Mitesh.
-
October 7th, 2004, 01:45 AM
#2
Re: Buttons and Regions
Dear Mitesh,
I have done similar thing by the following method.
I have subclassed CButton to CBMButton. I have defined a variable for CBMButton with name pRgn as pointer to CRgn. I have also defined a function SetRgn which takes address of a CRgn variable as its argument.
Code:
// Implementation
public:
CRgn* pRgn;
void SetRgn(CRgn&);
virtual ~CBMButton();
For implementation, I have used the virtual functions DrawItem and PreCreateWindow of CButton. Those functions and the function SetRgn are as given below.
Code:
BOOL CBMButton::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
// Sets style as Owner Draw
cs.style |= BS_OWNERDRAW;
// Sets default region as null
pRgn = NULL;
return CButton::PreCreateWindow(cs);
}
void CBMButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
if (pRgn)
{
SelectClipRgn(lpDrawItemStruct->hDC, HRGN(*pRgn));
SetWindowRgn(HRGN(*pRgn), TRUE);
}
}
void CBMButton::SetRgn(CRgn& rgn)
{
// If region is already defined, resets it
if (pRgn)
delete pRgn;
pRgn = new CRgn;
// Copies region to Button Region
if (pRgn && pRgn->CreateRectRgn(0, 0, 0, 0))
if (pRgn->CopyRgn(&rgn) != NULLREGION)
{
// Offsets button region to button origin
CRect R;
pRgn->GetRgnBox(R);
pRgn->OffsetRgn(-R.left, -R.top);
// Positions & sizes button according to its region
MoveWindow(R.left, R.top, R.Width(), R.Height());
Invalidate();
}
}
If painting is required than the default gray background or if bitmap has to be displayed, it can be done by properly coding the function DrawItem.
Please note that the region has to be translated to its zero origin before setting as window region of CButton.
I have created the button in the view and set its region immediately after creation.
Please rate this post, if it has been useful to you.
Best luck.
Pravin.
07-10-2004.
-
October 7th, 2004, 02:23 PM
#3
Re: Buttons and Regions
Thank you for the valuable code and information
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|