Transparent Dialog


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


This code makes use of some code from Zafir Anjum, specifically the code to load a background image.

Ok. Every window has a Region. This is how Windows knows what area of the screen to draw on when something changes in your program. Normally a windows Region is square, but you can make one that is non-square (basically any shape you want) so that Windows will only draw the part of your window that is within the non-square Region. What I have done is scan through the window pixel by pixel looking for the color I want to make transparent. Everytime I find that color, I add it to a temporary Region. I collect all the transparent spaces, then invert it and assign it to the window. The CRgn class, as it turns out, it a very powerful little class.

This code starts up very slow, the process I use for finding the transparent region is inefficient to say the least. I will pick it up again later and work on it if no one else does in the mean time. this took a lot of my brain on a very hot day in August so I hope you credit me when and if you use this.

Download demo project - 156 KB


  • This is proposed by MSDN to create a transparent dialog of sorts

    Posted by Legacy on 09/08/2002 07:00am

    Originally posted by: Bernd G. Scheu

    quote ->

    "Using a Dialog Bar with a Rebar Control

    As mentioned in Rebar Controls and Bands, each band can contain only one child window (or control). This might be a limitation if you want to have more than one child window per band. A convenient workaround is to create a dialog bar resource with multiple controls and then add a rebar band (containing the dialog bar) to the rebar control.

    Normally, if you wanted the dialog bar band to appear transparent, you would set the WM_EX_TRANSPARENT extended style for the dialog bar object. However, because WM_EX_TRANSPARENT has some issues with properly painting the background of a dialog bar, you will need to do a little extra work to achieve the desired effect.

    The following procedure details the steps necessary to achieve transparency without using the WM_EX_TRANSPARENT extended style.

    To implement a transparent dialog bar in a rebar band

    Using ClassWizard, add a new class (for example, CMyDlgBar) that implements your dialog bar object.

    Add a handler for the WM_ERASEBKGND message.

    In the new handler, modify the existing code to match the following example:
    BOOL CMyDlgBar::OnEraseBkgnd( CDC* pDC )
    CWnd* pParent = GetParent();
    CPoint pt(0, 0);
    MapWindowPoints(pParent, &pt, 1);
    pt = pDC->OffsetWindowOrg(pt.x, pt.y);
    LRESULT lResult = pParent->SendMessage(WM_ERASEBKGND,
    (WPARAM)pDC->m_hDC, 0L);
    pDC->SetWindowOrg(pt.x, pt.y);
    return lResult;

    Add a handler for the WM_MOVE message.

    In the new handler, modify the existing code to match the following example:
    BOOL CMyDlgBar::OnMove( int cx, int cy )

    The new handlers simulate the transparency of the dialog bar by forwarding the WM_ERASEBKGND message to the parent window and forcing a repaint every time the dialog bar object is moved."

    -> unquote

    This works fine with my code as far as I've implemented it yet. Seems to be the best way to go ... at least for me. :) ;) Have fun and good luck.

  • function to calculate the region I think it is faster

    Posted by Legacy on 03/02/2000 08:00am

    Originally posted by: Tamer Mokhtar

    this fuction to calculate the region : -
    void CMyDialog::CalcRgn(CDC * pDC)
    if ( ok )
    BITMAP bm;
    HDC hdc = pDC->GetSafeHdc();
    COLORREF cr;
    CRgn working,temp;
    for(int x = 0;x < bm.bmWidth;x++)
    for(int y = 0 ;y< bm.bmHeight;y++)
    int oldy = y;
    cr = GetPixel(hdc,x,y);
    while( cr != RGB(255,255,255))//0x00FFFFFF)
    cr = GetPixel(hdc,x,y);
    working.CombineRgn( &working, &temp,RGN_OR);



    ok = FALSE;

    I think it will be faster than yours I hope so, but of course your idea is my first step

    ok is BOOL in the declaration part of the CMyDialog
    and on the OnPaint () use the Draw() function in some book
    then call the CalcRgn() ,

  • Window Shaping Class Up FOr Grabs...

    Posted by Legacy on 01/28/2000 08:00am

    Originally posted by: Gareth Clarke

    If anyone is interested I have examined the code given here and created a class that handles saving out region data based on a colour from a bitmap. You can then set any window to this shape by simply loading up the region data (very fast).

    The class is very simple, nothing to it. If you want a copy email me.


  • Totally Transparent

    Posted by Legacy on 09/27/1999 07:00am

    Originally posted by: Scott Corbin

    Can this be used to make a totally transparent window?
    or is there a better way to make a totally transparent dialog?

    I'm writing a program using OpenGL and i want a totally transparent window that is the size of the desktop that i can use for OpenGL effects on the desktop.

    is there something better i can use or would this be my best bet????


  • Easy Work around

    Posted by Legacy on 07/13/1999 07:00am

    Originally posted by: Anish Mistry

    An easy work around for making the image not flick to transparent when the algo is done is to make the dialog transparent and when it appears it will be transparent.

  • Faster algorithm for locating transparent pixel

    Posted by Legacy on 05/12/1999 07:00am

    Originally posted by: Oren Zamir

    Well done Jason!

    I was curious on how these type of application work so I took a look at your program. From what I saw it's a lot simpler than I expected.
    Any how, I noticed that you wished that the algorithm could be a little quicker. So I worked on it for a couple of hours. Instead of pixel by pixel examination, I used vectors displacement to locate transparent pixels. I also cut down on api calls. The final result shows. Using the old algorithm, on the bitmap provided, took as long as 8 seconds while the new algorithm took less then a second.
    Anyway I hope this helps a little bit with efficiency.

    Here is the code I used:

    //Only do this once because it takes some time.
    //maybe should pre-make a seperate file to hold region data?
    if (FirstRun){
    bool Set = false;
    int l_YVector=0;
    //begin pixel by pixel scan for transparent color
    //Get the color
    C = GetPixel(hdc,X,Y);
    // *** passive region.
    if(C == 0x00FFFFFF){
    if (l_YVector){
    // *** create the region for the window to be active.
    if (Temp.CreateRectRgn(X,l_YVector,X+1,Y)){
    // *** Add vector of pixel to region
    Working.CombineRgn( &Working, &Temp,RGN_OR );
    // *** last reference point to begin new vector.
    l_YVector = 0;
    // *** active region.
    if (!Set) {
    //First, init the working region
    //never delete Working, the OS does not store the value, only a pointer to it.
    if (!l_YVector) // *** found an active region, record it.
    l_YVector =Y;
    FirstRun = 0;
    if (Set) SetWindowRgn((HRGN)Working,TRUE);
    //redraw just incase.. trust me.. leave this here

  • Web Site

    Posted by Legacy on 11/04/1998 08:00am

    Originally posted by: Jason Wylie


  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date