Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CStatic::SetIcon Transparent.

CStatic::SetIcon Transparent.

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paresh Chitte
    wrote on last edited by
    #1

    Hi, I am using the API CStatic::SetIcon to associate the icon with the static control. I have placed this control on CDialog, (Dialog background is gradient painted). I want this CStatic control to be transparent (so that the background of CStatic 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.

    H 1 Reply Last reply
    0
    • P Paresh Chitte

      Hi, I am using the API CStatic::SetIcon to associate the icon with the static control. I have placed this control on CDialog, (Dialog background is gradient painted). I want this CStatic control to be transparent (so that the background of CStatic 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.

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      See http://www.codeproject.com/KB/static/transparentstatic.aspx[^] does helpful?

      P 1 Reply Last reply
      0
      • H Hamid Taebi

        See http://www.codeproject.com/KB/static/transparentstatic.aspx[^] does helpful?

        P Offline
        P Offline
        Paresh Chitte
        wrote on last edited by
        #3

        Hi Hamid, I have seen this article and this does not serve my purpose. Regards, Paresh.

        R 1 Reply Last reply
        0
        • P Paresh Chitte

          Hi Hamid, I have seen this article and this does not serve my purpose. Regards, Paresh.

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          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[^]

          P 1 Reply Last reply
          0
          • R Rajkumar R

            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[^]

            P Offline
            P Offline
            Paresh Chitte
            wrote on last edited by
            #5

            Thanks Rajkumar, AlphaBlend works for bitmaps. I have to use icons. Regards Paresh

            R 1 Reply Last reply
            0
            • P Paresh Chitte

              Thanks Rajkumar, AlphaBlend works for bitmaps. I have to use icons. Regards Paresh

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              before that, I would like to know what is the result when you tried with your code.

              P 1 Reply Last reply
              0
              • R Rajkumar R

                before that, I would like to know what is the result when you tried with your code.

                P Offline
                P Offline
                Paresh Chitte
                wrote on last edited by
                #7

                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.

                R 1 Reply Last reply
                0
                • P Paresh Chitte

                  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.

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #8

                  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;
                  ........
                  }

                  1. 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,

                  1. 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);
                  }
                  }

                  P 1 Reply Last reply
                  0
                  • R Rajkumar R

                    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;
                    ........
                    }

                    1. 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,

                    1. 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);
                    }
                    }

                    P Offline
                    P Offline
                    Paresh Chitte
                    wrote on last edited by
                    #9

                    Hi Rajkumar, The solution you have provided is perfectly working for me. Thank you very much for your kind help. :) Regards, Paresh.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups