Transparent Dialog

-->

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



Comments

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

    Posted by Legacy on 09/08/2002 12: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();
    ASSERT_VALID(pParent);
    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 )
    {
    Invalidate();
    }

    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.

    Reply
  • function to calculate the region I think it is faster

    Posted by Legacy on 03/02/2000 12: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;
    myBitmap.GetObject(sizeof(BITMAP),&bm);
    CRgn working,temp;
    working.CreateRectRgn(0,0,0,0);
    temp.CreateRectRgn(0,0,0,0);
    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)
    {
    y++;
    cr = GetPixel(hdc,x,y);
    };
    temp.SetRectRgn(x,oldy,x+1,y);
    working.CombineRgn( &working, &temp,RGN_OR);

    }
    }

    //temp.SetRectRgn(0,0,1200,1200);
    //working.CombineRgn(&working,&temp,RGN_XOR);

    SetWindowRgn((HRGN)working,TRUE);
    ok = FALSE;
    }else
    NULL;
    }

    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() ,

    Reply
  • Window Shaping Class Up FOr Grabs...

    Posted by Legacy on 01/28/2000 12: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.

    gareth10@globalnet.co.uk

    Reply
  • Totally Transparent

    Posted by Legacy on 09/27/1999 12: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????

    Thanks

    Reply
  • Easy Work around

    Posted by Legacy on 07/13/1999 12: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.

    Reply
  • Faster algorithm for locating transparent pixel

    Posted by Legacy on 05/12/1999 12: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
    for(X=0;X<W;X++){
    l_YVector=0;
    for(Y=0;Y<H;Y++){
    //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;
    }
    Temp.DeleteObject();
    }
    }
    // *** active region.
    else{
    if (!Set) {
    //First, init the working region
    //never delete Working, the OS does not store the value, only a pointer to it.
    Working.CreateRectRgn(X,Y,X+1,Y+1);
    Set=true;
    }
    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
    RedrawWindow();

    Reply
  • Web Site

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

    Originally posted by: Jason Wylie

    http://www.geocities.com/ResearchTriangle/Lab/5376/

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Companies must routinely transfer files and share data to run their business, work with partners, and speed operations. However, many find the traditional approach to file transfer lacks necessary security, is too complex and difficult to manage, does not support the levels of automation needed, and breaks down when addressing the file transfer requirements of new areas like Big Data analytics and mobile applications. This QuinStreet SmartSelect discusses how the changing business environment is making the use …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds