CStatic::SetIcon Transparent.
-
Hi, I am using the API
CStatic::SetIcon
to associate the icon with the static control. I have placed this control onCDialog
, (Dialog background is gradient painted). I want thisCStatic
control to be transparent (so that the background ofCStatic
would be gradient, which is of Dialogs background color). However, icon image is transparent. How could I do this ? Any help would be highly appreciated. Thanks, Paresh. -
Hi, I am using the API
CStatic::SetIcon
to associate the icon with the static control. I have placed this control onCDialog
, (Dialog background is gradient painted). I want thisCStatic
control to be transparent (so that the background ofCStatic
would be gradient, which is of Dialogs background color). However, icon image is transparent. How could I do this ? Any help would be highly appreciated. Thanks, Paresh.See http://www.codeproject.com/KB/static/transparentstatic.aspx[^] does helpful?
-
See http://www.codeproject.com/KB/static/transparentstatic.aspx[^] does helpful?
Hi Hamid, I have seen this article and this does not serve my purpose. Regards, Paresh.
-
Hi Hamid, I have seen this article and this does not serve my purpose. Regards, Paresh.
I think you need to paint alpha blended explicitly, AlphaBlend[^] and make sure you handled OnEraseBkgnd so that Static control doesnot painted its own background have a look at Using the AlphaBlend function[^]
-
I think you need to paint alpha blended explicitly, AlphaBlend[^] and make sure you handled OnEraseBkgnd so that Static control doesnot painted its own background have a look at Using the AlphaBlend function[^]
Thanks Rajkumar, AlphaBlend works for bitmaps. I have to use icons. Regards Paresh
-
Thanks Rajkumar, AlphaBlend works for bitmaps. I have to use icons. Regards Paresh
before that, I would like to know what is the result when you tried with your code.
-
before that, I would like to know what is the result when you tried with your code.
class CMyDialog : public CDialog { ........ CStatic m_StaticIcon; ........ } HBRUSH CMyDialog:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog:OnCtlColor(pDC, pWnd, nCtlColor); switch(nCtlColor) { case CTLCOLOR_STATIC: if (pWnd->m_hWnd == m_StaticIcon.m_hWnd) { pDC->SetBkMode(TRANSPARENT); return (HBRUSH)(GetStockObject(NULL_BRUSH)); } break; } return hbr; } BOOL CMyDialog:OnInitDialog() { ......... m_StaticIcon.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_ICON, CRect(0, 0, 0, 0), this); HICON hIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(ICON_ID)); m_StaticIcon.SetIcon(hIcon); ......... }
In CMyDialog:OnPaint(), I have painted a region of CDialog with GradientFill(...), and on this area I want a static icon, which needs to be transparent. So that I can see the background of CStatic icon as gradient. But this icon has been painted with background of rest of dialog and not the region, which I have painted with GradientFill. I hope I have mentioned the problem correctly. Regards, Paresh. -
class CMyDialog : public CDialog { ........ CStatic m_StaticIcon; ........ } HBRUSH CMyDialog:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog:OnCtlColor(pDC, pWnd, nCtlColor); switch(nCtlColor) { case CTLCOLOR_STATIC: if (pWnd->m_hWnd == m_StaticIcon.m_hWnd) { pDC->SetBkMode(TRANSPARENT); return (HBRUSH)(GetStockObject(NULL_BRUSH)); } break; } return hbr; } BOOL CMyDialog:OnInitDialog() { ......... m_StaticIcon.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_ICON, CRect(0, 0, 0, 0), this); HICON hIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(ICON_ID)); m_StaticIcon.SetIcon(hIcon); ......... }
In CMyDialog:OnPaint(), I have painted a region of CDialog with GradientFill(...), and on this area I want a static icon, which needs to be transparent. So that I can see the background of CStatic icon as gradient. But this icon has been painted with background of rest of dialog and not the region, which I have painted with GradientFill. I hope I have mentioned the problem correctly. Regards, Paresh.I would suggest very simple approach as you want to display only the icon, use DrawIcon, inplace of doing a lot of stuff over CStatic. 1) add the icon as member variable.
class CMyDialog : public CDialog
{
........
HICON m_hStaticIcon;// CStatic m_StaticIcon;
........
}- load the icon
BOOL CMyDialog:OnInitDialog()
{
.........
/*m_StaticIcon.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_ICON,
CRect(0, 0, 0, 0), this);*/m_hStaticIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(ICON_ID));
/*m_StaticIcon.SetIcon(hIcon);*/
.........
}Paresh Chitte wrote:
In CMyDialog:OnPaint(), I have painted a region of CDialog with GradientFill(...), and on this area I want a static icon,
- Now you draw the icon over the painted region.
void CMyDialog::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
.....
.....
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
// you painted the region with gradient fill
// after that draw the icon over the gradient region
dc.DrawIcon(100, 100, m_hStaticIcon);
}
} -
I would suggest very simple approach as you want to display only the icon, use DrawIcon, inplace of doing a lot of stuff over CStatic. 1) add the icon as member variable.
class CMyDialog : public CDialog
{
........
HICON m_hStaticIcon;// CStatic m_StaticIcon;
........
}- load the icon
BOOL CMyDialog:OnInitDialog()
{
.........
/*m_StaticIcon.Create(_T(""), WS_CHILD | WS_VISIBLE | SS_ICON,
CRect(0, 0, 0, 0), this);*/m_hStaticIcon = ::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(ICON_ID));
/*m_StaticIcon.SetIcon(hIcon);*/
.........
}Paresh Chitte wrote:
In CMyDialog:OnPaint(), I have painted a region of CDialog with GradientFill(...), and on this area I want a static icon,
- Now you draw the icon over the painted region.
void CMyDialog::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
.....
.....
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
// you painted the region with gradient fill
// after that draw the icon over the gradient region
dc.DrawIcon(100, 100, m_hStaticIcon);
}
}Hi Rajkumar, The solution you have provided is perfectly working for me. Thank you very much for your kind help. :) Regards, Paresh.