Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a dialog box with a bitmap as background.

I am trying to create transparent treeview so I need to grab the underlying bitmap from treeview's WM_PAINT handler, but I do not know how to do this.

I assume that I will need ScreenToClient and ClientToScreen APIs, but I do not know how to use them.

In my WM_PAINT handler, I have this:
C++
memBmp = CreateCompatibleBitmap( hdc,  // Tree's HDC, got from BeginPaint()
    rcClient.right - rcClient.left,      // tree's client rectangle
    rcClient.bottom - rcClient.top );    // obtained with GetClientRect()

bmpOldFinal = (HBITMAP)SelectObject( memDC, // HDC compatible with tree's HDC
    memBmp );

// capture parent bitmap
HDC pDC = GetDC( GetParent(hwnd) );

// a desperate try to get proper coordinates, 
// based on an example of mouse usage from MSDN
POINT ptTreeUL, ptTreeLR;

ptTreeUL.x = rcClient.left;
ptTreeUL.y = rcClient.top;

ptTreeLR.x = rcClient.right + 1;
ptTreeLR.y = rcClient.bottom + 1;

ClientToScreen( hwnd, &ptTreeUL );
ClientToScreen( hwnd, &ptTreeLR );

// Blit the parent DC into memory DC, here things go wrong
BitBlt( memDC, 0, 0, 
    rcClient.right - rcClient.left, 
    rcClient.bottom - rcClient.top, 
    pDC, 
    ptTreeLR.x - ptTreeUL.x,
    ptTreeLR.y - ptTreeUL.y, SRCCOPY ); 

ReleaseDC( GetParent(hwnd), pDC ); 


Unfortunately, the coordinates I get are not good, and although I get a part of the bitmap Blited into memory DC it is not the correct part.

How can I blit the underlying portion of the parent background into treeview's memory DC ?

Thank you.

Best regards.
Posted

1 solution

you must transform with the right HWND
C#
HWND hParent = GetParent(hwnd);
ClientToScreen( hwnd, &ptTreeUL );
ClientToScreen( hwnd, &ptTreeLR );


//dont write such code ;-)
ReleaseDC( GetParent(hwnd), pDC );

tip: create the bitmap globally once to improve performance
 
Share this answer
 
v2
Comments
AlwaysLearningNewStuff 4-Apr-14 14:25pm    
I have solved my problem:

// capture parent bitmap
HDC pDC = GetDC( GetParent(hwnd) );

HWND parent = GetParent(hwnd);

POINT ptTreeUL, ptParentUL; // upper left corner coordinates

ptTreeUL.x = rcClient.left;
ptTreeUL.y = rcClient.top;

ptParentUL.x = rcParent.left;
ptParentUL.y = rcParent.right;

ClientToScreen( hwnd, &ptTreeUL );
ClientToScreen( parent, &ptParentUL );

// Blit the parent DC into memory DC
BitBlt( memDC, 0, 0,
rcClient.right - rcClient.left,
rcClient.bottom - rcClient.top,
pDC,
ptParentUL.x + ptTreeUL.x,
ptParentUL.y + ptTreeUL.y, SRCCOPY );

ReleaseDC( parent, pDC );


You should properly edit your solution so it can be useful to the future generations and those who are fairly inexperienced.

Thank you for the advice ( my 5 as a token of gratitude ).

Best regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900