Transparent Window
In some cases it's nice to have a window that not always looks the same (I mean rectangular). So I created this CWnd based window class which implements a transparent window. The transparency information is supplied by a mask bitmap.
To include this transparent window type into your application copy the Transparent Window class files to your project's directory. Create an object of this class and call the CreateTransparent() method for it. The parameters are a window caption, a RECT structure an ID for the mask bitmap and annother one for the background bitmap.
So your InitInstance() function should look like this:
BOOL CDrawtestApp::InitInstance()
{
CRect rect(0, 0, 320, 150);
CTransparentWnd* pTWnd = new CTransparentWnd;
m_pMainWnd = pTWnd;
pFrame->CreateTransparent("Transparent Test", rect,
IDB_MASK, IDB_BACK);
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
The mechanism this window is working with is to set a proper window-region. This window region is setup by the function SetupRegion(). The process is to load the specified monochrome mask bitmap and to check for each pixel if it is set or not. For each set pixel add this pixel's position to the window region. This is very slow, but only done once at startup. Therefore this window is not resizeable.
The code for the SetupRegion() function:
void CTransparentWnd::SetupRegion(CDC *pDC, unsigned short
MaskID)
{
CDC
memDC;
CBitmap cBitmap;
CBitmap* pOldMemBmp = NULL;
COLORREF col;
CRect
cRect;
int
x, y;
CRgn
m_Rgn, rgnTemp;
GetWindowRect(&cRect);
cBitmap.LoadBitmap(MaskID);
memDC.CreateCompatibleDC(pDC);
pOldMemBmp = memDC.SelectObject(&cBitmap);
m_Rgn.CreateRectRgn(0, 0, cRect.Width(), cRect.Height());
for(x=0; x<=cRect.Width(); x++)
{
for(y=0; y<=cRect.Height(); y++)
{
col = memDC.GetPixel(x,
y);
if(col == 0)
{
rgnTemp.CreateRectRgn(x, y, x+1, y+1);
m_Rgn.CombineRgn(&m_Rgn, &rgnTemp, RGN_XOR);
rgnTemp.DeleteObject();
}
}
}
if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
SetWindowRgn((HRGN)m_Rgn, TRUE);
}
The next proble is to make this window moveable. There are two possibilities. You can override the ONNcHittest() function and redirect all HTCLIENT results to HTCAPTION result, or you only override the OnLButtonDown() function and send the correct hittest result to the framework.
void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint
point)
{
CWnd::OnLButtonDown(nFlags, point);
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}
Now you can move the window by clicking anywere inside the window region.

Comments
Add text
Posted by Legacy on 07/29/2002 12:00amOriginally posted by: jo
How is it possible to add text control instead of the bitmap ? I would like to develop an On Screen Display app, so it's just what I need (it is to display text for a karaoke app).
ReplyHow to add another stuff
Posted by Legacy on 07/11/2002 12:00amOriginally posted by: Abdulrahman
Could anyone tell me how to add others control like, Buttons
ReplyEdit and TRANSPARENT RichEdir control. Thanks for all you guys Specially the author...
WM_LBUTTONUP
Posted by Legacy on 02/03/2002 12:00amOriginally posted by: Jzh
ReplyAdd two lines to CreateTransparent()
Posted by Legacy on 01/24/2002 12:00amOriginally posted by: Yoo, TaiHyung
I found why the cursor is busy in the first stage.
It seems that the author left out a line in CreateTransparent().
DC gained by GetWindowDC() must be released.
Save DC pointer before calling SetupRegion()
and Call ReleaeDC() after SetupRegion().
Reply
Exelent ! "But .. !" Please Help !!
Posted by Legacy on 01/03/2001 12:00amOriginally posted by: alok anndan
I am not able to run it on Windows'95.
Replyactually problem is not running the application, but it is not making image transparent . How can i get the image only on desktop,running on Win 95.
regards
alok
Busy Mouse Cursor
Posted by Legacy on 11/15/2000 12:00amOriginally posted by: Aydin Mir Mohammadi
There is an minor bug in BOOL CTransparentWnd::CreateTransparent
Replace
AfxRegisterWndClass(0)
with
AfxRegisterWndClass(CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW |CS_CLASSDC,::LoadCursor(NULL,IDC_ARROW),(HBRUSH) (COLOR_WINDOW + 1))
Aydin
ReplyCan I load gif instead of bitmap?
Posted by Legacy on 07/27/2000 12:00amOriginally posted by: Jackie
Hi, All!
This project you load a bitmap, but I would like to load a .gif or .jpg images. Does someone know how do it? Can you show me?
Thanks very much.
ReplyJackie
How can I create a transparent window?
Posted by Legacy on 07/26/2000 12:00amOriginally posted by: Jackie
Hi, I'd like to create a transparent window, but I don't know how to start. Can you show me how I create this program? I try to do by my own, step by step, but I could not create. Did you use MFC(EXE) OR MFC(DLL) in Visual C++ or something else?
Thank you very much.
ReplyJackie
Why is the mouse cursor always busy when it's within the window region ??
Posted by Legacy on 02/21/2000 12:00amOriginally posted by: stareyes
First, I should thank you very much to the creater of this demo. You did a very userful job!
ReplyBut, the proble I am having is, that the mouse cursor is always busy when it's within the window region. I'm using windows 98 when this occurs.
When I run the application under NT, I see the same thing too. Yet, there is slightly different. If I move the mouser cursor to the window, it's busy; then I move the mouser cursor out of that window and then move it back, the busy cursor is gone. However, this is not the end of the story.
I hit "ctr+alt+delete" to bring up the task manager in NT, I look at the CPU usage, it's 100% used! I'm sure it's by the application I'm running, any idea ???
Transparent OLE Server Window?
Posted by Legacy on 09/12/1999 12:00amOriginally posted by: Wayne R
Found your stuff interesting but was wondering if anyone has tried doing something similar in the context of an OLE server (to allow for a transparent in-place active editting session or the like)?
Actually I want to have the server function as a remote controlled cursor (inserted via ole) and need it drawn transparently (as an 'always-active' ole server).
Any thoughts (and help) on this would be appreciated. Esp how to fit this code into the doc/view architecture and get rid of those damn menus et al while still using the other benefits of mfc's doc/view.
ReplyLoading, Please Wait ...