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. ATL / WTL / STL
  4. Static controls transparency problem

Static controls transparency problem

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestionc++
5 Posts 2 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.
  • M Offline
    M Offline
    mirano
    wrote on last edited by
    #1

    Hi, I have a problem with getting a transparent static control to draw correctly on a WTL dialog. Everything I do works, but only if the text color is black. Message handler is like this: MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic) Then I have a method to handle it: LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); and in the code I do like this: LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { HWND hWnd = (HWND)lParam; HDC hDc = (HDC)wParam; COLORREF m_crTextColorMaroon; m_crTextColorMaroon = RGB( 128, 0, 0 ); HBRUSH hbr; hbr = (HBRUSH)m_crTextColorMaroon; if( hWnd == (HWND)GetDlgItem(IDC_FILE) || hWnd == (HWND)GetDlgItem(IDC_FILESIZE)) { ::SetTextColor( hDc, m_crTextColorMaroon ); ::SetBkMode(hDc, TRANSPARENT); return (LRESULT) GetStockObject (HOLLOW_BRUSH); } return 0; } So far so good, everything works. I am only changing the color of the controls that will represent a label, the other static controls on the dialog should display text in the black color. So this works perfectly when the dialog gets displayed. Now the user changes the language on the menu, and I do this: SetDlgItemText( IDC_FILE, (LPCTSTR) SomeNewText); and the text gets drawn over the previous text, making it indecipherable. What is wrong with this code? Why doesn't the static controls clear its content before the new text is drawn. If I use SetDlgItemText(...) with any other static control that is not handled in the OnCtlColorStatic method, it gets drawn correctly. Thanks for your help.

    Sarajevo, Bosnia

    M 1 Reply Last reply
    0
    • M mirano

      Hi, I have a problem with getting a transparent static control to draw correctly on a WTL dialog. Everything I do works, but only if the text color is black. Message handler is like this: MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic) Then I have a method to handle it: LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); and in the code I do like this: LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { HWND hWnd = (HWND)lParam; HDC hDc = (HDC)wParam; COLORREF m_crTextColorMaroon; m_crTextColorMaroon = RGB( 128, 0, 0 ); HBRUSH hbr; hbr = (HBRUSH)m_crTextColorMaroon; if( hWnd == (HWND)GetDlgItem(IDC_FILE) || hWnd == (HWND)GetDlgItem(IDC_FILESIZE)) { ::SetTextColor( hDc, m_crTextColorMaroon ); ::SetBkMode(hDc, TRANSPARENT); return (LRESULT) GetStockObject (HOLLOW_BRUSH); } return 0; } So far so good, everything works. I am only changing the color of the controls that will represent a label, the other static controls on the dialog should display text in the black color. So this works perfectly when the dialog gets displayed. Now the user changes the language on the menu, and I do this: SetDlgItemText( IDC_FILE, (LPCTSTR) SomeNewText); and the text gets drawn over the previous text, making it indecipherable. What is wrong with this code? Why doesn't the static controls clear its content before the new text is drawn. If I use SetDlgItemText(...) with any other static control that is not handled in the OnCtlColorStatic method, it gets drawn correctly. Thanks for your help.

      Sarajevo, Bosnia

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      You're returning HOLLOW_BRUSH which paints nothing. When the control asks you what brush to use as the background brush, you give it a brush that doesn't paint, so the old contents of the control aren't erased. I'd do this:

      LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
      {
      HWND hWnd = (HWND) lParam;
      HDC hDc = (HDC) wParam;

      if(hWnd == GetDlgItem(IDC_FILE) || hWnd == GetDlgItem(IDC_FILESIZE))
      {
      SetTextColor(hDc, RGB(128,0,0));
      SetBkMode(hDc, TRANSPARENT);
      }

      bHandled = false;
      return 0;
      }

      This code changes the attributes of the DC that you care about, then lets the message go on to the default window proc, which will set the right background color.

      --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

      M 1 Reply Last reply
      0
      • M Michael Dunn

        You're returning HOLLOW_BRUSH which paints nothing. When the control asks you what brush to use as the background brush, you give it a brush that doesn't paint, so the old contents of the control aren't erased. I'd do this:

        LRESULT MainDialog::OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
        {
        HWND hWnd = (HWND) lParam;
        HDC hDc = (HDC) wParam;

        if(hWnd == GetDlgItem(IDC_FILE) || hWnd == GetDlgItem(IDC_FILESIZE))
        {
        SetTextColor(hDc, RGB(128,0,0));
        SetBkMode(hDc, TRANSPARENT);
        }

        bHandled = false;
        return 0;
        }

        This code changes the attributes of the DC that you care about, then lets the message go on to the default window proc, which will set the right background color.

        --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

        M Offline
        M Offline
        mirano
        wrote on last edited by
        #3

        Thanks for your help, Mike, but this does not work for me. Now all static controls display a black colored text. Yes, I can change the text and not to get it write over the previous one, but the text is not redish ( RGB(128, 0, 0)) but black. Can I send you the app, it's only one dialog, just a few lines of code above the normal code generated for the ATL exe app? Thanks.

        Sarajevo, Bosnia

        M 1 Reply Last reply
        0
        • M mirano

          Thanks for your help, Mike, but this does not work for me. Now all static controls display a black colored text. Yes, I can change the text and not to get it write over the previous one, but the text is not redish ( RGB(128, 0, 0)) but black. Can I send you the app, it's only one dialog, just a few lines of code above the normal code generated for the ATL exe app? Thanks.

          Sarajevo, Bosnia

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          OK, this time I actually tested the code that I'm going to post. ;)

          LRESULT CMainDlg::OnCtlcolorstatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
          {
          LRESULT res = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
          HDC hdc = (HDC) wParam;
          HWND hwnd = (HWND) lParam;

          if(hwnd == GetDlgItem(IDC\_STATIC1) || hwnd == GetDlgItem(IDC\_STATIC2))
              {
              SetTextColor(hdc, RGB(255,0,0));
              SetBkMode(hdc, TRANSPARENT);
              }
          
          return res;
          

          }

          The first line gets the default brush so the background looks right.


          Last modified: 8hrs 3mins after originally posted --

          --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

          M 1 Reply Last reply
          0
          • M Michael Dunn

            OK, this time I actually tested the code that I'm going to post. ;)

            LRESULT CMainDlg::OnCtlcolorstatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
            {
            LRESULT res = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
            HDC hdc = (HDC) wParam;
            HWND hwnd = (HWND) lParam;

            if(hwnd == GetDlgItem(IDC\_STATIC1) || hwnd == GetDlgItem(IDC\_STATIC2))
                {
                SetTextColor(hdc, RGB(255,0,0));
                SetBkMode(hdc, TRANSPARENT);
                }
            
            return res;
            

            }

            The first line gets the default brush so the background looks right.


            Last modified: 8hrs 3mins after originally posted --

            --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

            M Offline
            M Offline
            mirano
            wrote on last edited by
            #5

            Haha...okay, thanks a lot, you are right of course. Thank you again.

            Sarajevo, Bosnia

            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