CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Posts
    280

    remove the background of static text

    As showing in attached picture, I want to remove the background of static text ("Quality : 100" and "Resoultion...").
    The program I write is following:
    Code:
    // Print Prompt Text
             hwndStaticTextQuality = CreateWindowEx(0L, "STATIC", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT , 
    		      FrameX+5, FrameY, 80, 15, hwnd, (HMENU) ID_STATIC_LABEL_QUALITY, GetModuleHandle(NULL), 0 );
             sprintf(prompt_text,"Quality:%d",QUALITY_DEFAULT);
             SetWindowText(hwndStaticTextQuality, prompt_text);
    
             hwndStaticTextSpatial = CreateWindowEx(0L, "STATIC", NULL, WS_CHILD | WS_VISIBLE | ES_LEFT , 
    		      FrameX+5, FrameY+36, 100, 15, hwnd, (HMENU) ID_STATIC_LABEL_SPATIAL, GetModuleHandle(NULL), 0 );
    
             sprintf(prompt_text,"Resolution:1/%d",SPATIAL_DEFAULT);
             SetWindowText(hwndStaticTextSpatial, prompt_text);
    What should I modify?
    Thank you.
    Attached Images Attached Images

  2. #2
    Join Date
    May 2004
    Posts
    28

    Re: remove the background of static text

    Does:
    HDC hdc = GetDC(hwnd);
    SetBkMode(hdc,TRANSPARENT);
    work?

    Just posting it because that's what I'm using when I output text using TextOut for example...

  3. #3
    Join Date
    Jan 2005
    Location
    germany
    Posts
    160

    Re: remove the background of static text

    Did you check

    www.codeguru.com

    on that? Looking in C++ -> Controls -> Static you will find exactly what you have asked for.

    eg.
    http://www.codeguru.com/cpp/controls...cle.php/c8829/

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: remove the background of static text

    Simply use WM_CTLCOLORSTATIC to change background color of a static Control

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: remove the background of static text

    Just you have to handle WM_CTLCOLORSTATIC message which is sent to parent window when the static control is to be redrawn. Inside the handler function, set backround mode TRANSPARENT and return a NULL (HOLLOW) brush.
    Here is a brief example for all static controls from a dialog.
    You can adapt it to your needs.
    Code:
    #include <WINDOWSX.H>
    // ...
    HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type);
    // ...
    // dialog procedure
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
       BOOL bRet = TRUE; // handled
       switch(uMsg)
       {
    // ...
       case WM_CTLCOLORSTATIC:
          // //  NOTE: HANDLE_WM_CTLCOLORSTATIC is defined in <WINDOWSX.H>
          bRet = HANDLE_WM_CTLCOLORSTATIC(hwndDlg, wParam, lParam, Dlg_OnCtlColor);
          break;
    // ...
       }
       return bRet;
    }
    // WM_CTLCOLORSTATIC message handler
    HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type)
    {
       SetBkMode(hdc, TRANSPARENT);
       return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
    }
    Note: WM_CTLCOLORSTATIC is also sent for read-only or disabled edit controls.
    Last edited by ovidiucucu; January 18th, 2006 at 04:05 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Sep 2003
    Posts
    280

    Re: remove the background of static text

    Quote Originally Posted by ovidiucucu
    Just you have to handle WM_CTLCOLORSTATIC message which is sent to parent window when the static control is to be redrawn. Inside the handler function, set backround mode TRANSPARENT and return a NULL (HOLLOW) brush.
    Here is a brief example for all static controls from a dialog.
    You can adapt it to your needs.
    Code:
    #include <WINDOWSX.H>
    // ...
    HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type);
    // ...
    // dialog procedure
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
       BOOL bRet = TRUE; // handled
       switch(uMsg)
       {
    // ...
       case WM_CTLCOLORSTATIC:
          // //  NOTE: HANDLE_WM_CTLCOLORSTATIC is defined in <WINDOWSX.H>
          bRet = HANDLE_WM_CTLCOLORSTATIC(hwndDlg, wParam, lParam, Dlg_OnCtlColor);
          break;
    // ...
       }
       return bRet;
    }
    // WM_CTLCOLORSTATIC message handler
    HBRUSH Dlg_OnCtlColor(HWND hwnd, HDC hdc, HWND hwndChild, int type)
    {
       SetBkMode(hdc, TRANSPARENT);
       return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
    }
    Note: WM_CTLCOLORSTATIC is also sent for read-only or disabled edit controls.
    I add the following code:
    Code:
     case WM_CTLCOLORSTATIC:
             hdc = GetDC(hwnd);
             SetBkMode(hdc, TRANSPARENT);
             GetStockObject(NULL_BRUSH);
             ReleaseDC(hwnd,hdc);
             break;
    
      case WM_SIZE:
          ...
    But the result as attached picture.
    Attached Images Attached Images

  7. #7
    Join Date
    May 2005
    Posts
    4,954

    Re: remove the background of static text

    try that:

    Quote Originally Posted by MSDN
    wParam
    Handle to the device context for the static control window.
    Code:
    case WM_CTLCOLORSTATIC:  
             SetBkMode((HDC)wParam, TRANSPARENT);
             return GetStockObject(NULL_BRUSH);
    break;
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: remove the background of static text

    Just an aside note:
    I used in my example a message cracker macro (defined in WINDOWSX.H) with the corresponding message handler function, in order to make the code easier to read/understand/develop/maintain.
    Imagine a window procedure handling tens of messages with all stuff inside it. It becomes huge and can make the programmer's life a mess.

    Returning to the subject:
    As golanshahar already showed in his example, and also I said (maybe not enough underlined) you have to return a valid brush handle to be used by the system for painting the background of the control (in our case a null brush also known as hollow brush).

    EDIT:
    See this EXCELLENT ARTICLE.
    Last edited by ovidiucucu; January 20th, 2006 at 06:35 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: remove the background of static text

    This code works :

    Code:
    HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	HBRUSH hbr;
    
    	if ( nCtlColor == CTLCOLOR_STATIC ) 
    	{
    		pDC->SetBkMode(TRANSPARENT);
    		hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
    	}
    
    	else
    	{
    		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    	}
    	
    	return hbr;
    }

  10. #10
    Join Date
    Sep 2003
    Posts
    280

    Re: remove the background of static text

    I get the error message:
    error C2440: 'return' : cannot convert from 'void *' to 'long'
    Why?

    Quote Originally Posted by golanshahar
    try that:



    Code:
    case WM_CTLCOLORSTATIC:  
             SetBkMode((HDC)wParam, TRANSPARENT);
             return GetStockObject(NULL_BRUSH);
    break;
    Cheers

  11. #11
    Join Date
    Sep 2003
    Posts
    280

    Re: remove the background of static text

    Like this?
    case WM_CTLCOLORSTATIC:
    HBRUSH hbr;
    SetBkMode((HDC)wParam, TRANSPARENT);
    hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
    break;
    But the problem still exists!
    Quote Originally Posted by FireJocker
    This code works :

    Code:
    HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	HBRUSH hbr;
    
    	if ( nCtlColor == CTLCOLOR_STATIC ) 
    	{
    		pDC->SetBkMode(TRANSPARENT);
    		hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
    	}
    
    	else
    	{
    		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    	}
    	
    	return hbr;
    }

  12. #12
    Join Date
    May 2005
    Posts
    4,954

    Re: remove the background of static text

    Quote Originally Posted by Cooker
    I get the error message:
    error C2440: 'return' : cannot convert from 'void *' to 'long'
    Why?
    I saw that you are using MFC so try this out:

    Code:
    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      
      if ( pWnd->GetDlgCtrlID() == IDC_STATIC2 /*Id of the static control you want to change*/)
      {
    
        pDC->SetBkMode(TRANSPARENT);
        return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
      }
      return hbr;
    }
    Just make sure you are adding the OnCtlColor(..) with wizard.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  13. #13
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: remove the background of static text

    Code I gave you will remove the background of all your static text,

    If you want to remove only some of them, use code like golanshahar wrote :

    Code:
    HBRUSH CDigiDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	HBRUSH hbr;
    
    	if ( ( pWnd->GetDlgCtrlID() == IDC_STATIC1 ) 
               ||( pWnd->GetDlgCtrlID() == IDC_STATIC2 ) ) // etc...
    	{
    		pDC->SetBkMode(TRANSPARENT);
    		hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
    	}
    
    	else
    	{
    		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    	}
    	
    	return hbr;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured