Transparent Window

This article is based on the idea of Jason Wylie, the author of the article Transparent Dialog, but it uses a mask bitmap instead of getting the transparency information out of the bitmap. This way many funny things can be done...

Transparent Window Picture

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.

Download demo project - 23 KB

Download source - 3 KB



Comments

  • Add text

    Posted by Legacy on 07/29/2002 12:00am

    Originally 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).

    Reply
  • How to add another stuff

    Posted by Legacy on 07/11/2002 12:00am

    Originally posted by: Abdulrahman

    Could anyone tell me how to add others control like, Buttons
    Edit and TRANSPARENT RichEdir control. Thanks for all you guys Specially the author...

    Reply
  • WM_LBUTTONUP

    Posted by Legacy on 02/03/2002 12:00am

    Originally posted by: Jzh

    The program will not respond the message "WM_LBUTTONUP" because of posting the "WM_NCLBUTTONDOWN" message in function "OnLButtonDown". How to solve it?
    

    Reply
  • Add two lines to CreateTransparent()

    Posted by Legacy on 01/24/2002 12:00am

    Originally 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:00am

    Originally posted by: alok anndan

    I am not able to run it on Windows'95.
    actually 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

    Reply
  • Busy Mouse Cursor

    Posted by Legacy on 11/15/2000 12:00am

    Originally 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

    Reply
  • Can I load gif instead of bitmap?

    Posted by Legacy on 07/27/2000 12:00am

    Originally 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.
    Jackie

    Reply
  • How can I create a transparent window?

    Posted by Legacy on 07/26/2000 12:00am

    Originally 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.
    Jackie

    Reply
  • Why is the mouse cursor always busy when it's within the window region ??

    Posted by Legacy on 02/21/2000 12:00am

    Originally posted by: stareyes

    First, I should thank you very much to the creater of this demo. You did a very userful job!
    But, 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 ???

    Reply
  • Transparent OLE Server Window?

    Posted by Legacy on 09/12/1999 12:00am

    Originally 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.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds