dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] Issues drawing background bitmap in edit box common control

  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    [RESOLVED] Issues drawing background bitmap in edit box common control

    I was trying to subclass common-control's edit box to display a custom background bitmap.

    The brush for drawing is created as such:

    Code:
    CBitmap bmp1;
    CBrush brush1;
    
    VERIFY(bmp1.LoadBitmap(IDB_BITMAP1));
    VERIFY(brush1.CreatePatternBrush(&bmp1));
    Then I override OnCtlColor():

    Code:
    HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    	// TODO:  Change any attributes of the DC here
    	if(pWnd->GetDlgCtrlID() == edt1.GetDlgCtrlID())
    	{
    		//Use special brush
    		hbr = edt1.brush1;
    
    		pDC->SetBkMode(TRANSPARENT);
    	}
    
    	// TODO:  Return a different brush if the default is not desired
    	return hbr;
    }
    This works but only if I set ES_MULTILINE style for the edit box. For a single line control when I move it quickly off the screen and then back it creates this visual artifact.

    I made a small MFC app to illustrate the issue. This is a screenshot of what happens:

    Name:  ask_img.png
Views: 89
Size:  2.1 KB

    So what am I doing wrong?


    PS. I tried to override OnEraseBkgnd() to redraw background there (see my sample project) using FillRect() but it still didn't fix the issue.
    Attached Files Attached Files

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,581

    Re: Issues drawing background bitmap in edit box common control

    Why do you override the CMyDialog::OnCtlColor, not the CEdit one?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Issues drawing background bitmap in edit box common control

    Quote Originally Posted by VictorN View Post
    Why do you override the CMyDialog::OnCtlColor, not the CEdit one?
    Because OnCtlColor is sent to the parent window, is it not?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,581

    Re: Issues drawing background bitmap in edit box common control

    Quote Originally Posted by dc_2000 View Post
    Because OnCtlColor is sent to the parent window, is it not?
    Yes.
    For the derived from CEdit class you should handle the ON_WM_CTLCOLOR_REFLECT message.
    See example here.
    Victor Nijegorodov

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: Issues drawing background bitmap in edit box common control

    I'd try something like this
    Code:
    HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        if(pWnd->GetDlgCtrlID() == edt1.GetDlgCtrlID())
        {
    	pDC->SetBkMode(TRANSPARENT);
    	return edt1.brush1;
        }
    
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        return hbr;
    }

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Issues drawing background bitmap in edit box common control

    Victor, I get the same result if I override WM_CTRLCOLOR from my subclass of CEdit class.

    You see the difference between my example and the one you linked to is that they use solid color brush that doesn't show this artifact that I'm seeing with my example.

    Quote Originally Posted by GCDEF View Post
    I'd try something like this...
    This doesn't change anything either.

    I tried it on another PC and it seems to produce the same result.

    Can you try it on your end?

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: Issues drawing background bitmap in edit box common control

    Here's code from one of my apps that works.
    m_pBkBrush is a member of my view class and is created in the view's constructor like this.

    m_pBkBrush = new CBrush(RGB(192, 192, 192));

    Code:
    	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);	
    	if(pWnd->GetDlgCtrlID() == IDC_STATIC || pWnd->GetDlgCtrlID() == IDC_INPUT_STATIC)
    		return hbr;
            pDC->SetTextColor(RGB(0, 0, 128));
    	if(pWnd->GetDlgCtrlID() == IDC_FORMSLIST || pWnd->GetDlgCtrlID() == IDC_ENGINELIST)
    	{
    		pDC->SetBkColor(RGB(192, 192, 192));
    		return *m_pBkBrush;
    	}
    	return hbr;

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Issues drawing background bitmap in edit box common control

    Quote Originally Posted by GCDEF View Post
    Here's code from one of my apps that works...
    Again. You're creating a solid color brush. That is why you don't see that artifact in your app.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: Issues drawing background bitmap in edit box common control

    It looks like an undocumented behavior or something. When WM_PAINT occurs due to control clipping or being off-screen, the brush is used for clipped region only (which may be explained by a sort of optimization, or may be not). The result is the artifacts you observed. Funny enough that when mouse cursor hovers the control, the entire background gets re-painted.

    An evident conclusion should be like single line edit box expects a solid brush only. No fine art is welcomed.

    PS. Many years ago I came to conclusion that buttons, edit boxes, list boxes and combo boxes (the ones came from Win16 ages) should be used strictly as those normally are. I call those control types "stubborn" because of their ability to oppose your any attempt of more or less fancy customization.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: [RESOLVED] Issues drawing background bitmap in edit box common control

    Yes, good point, Igor. Looks like old (and abandoned) Microsoft code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)