Thread: SetLayeredWindowAttributes

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    SetLayeredWindowAttributes

    Hey everybody, I think I already posted this but I cant seem to find it, must not have pressed the post button , but whatever I will just do it again.

    So my question is, is there a SetLayeredWindowAttributes() type function for child windows.

    I will just tell you what I am doing with it first so you know what I mean.

    Ok so my program creates child windows outside of the WM_CREATE: case so I dont want them to just appear there I want to be be able to fade them in (I want to be able to control the "opaqueness" of the child windows). that is the plan anyway...

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    From MSDN's section entitled Using Layered Windows.
    Using Layered Windows
    To have a dialog box come up as a translucent window, first create the dialog as usual. Then, on WM_INITDIALOG, set the layered bit of the window's extended style and call SetLayeredWindowAttributes with the desired alpha value. The code might look like this:

    // Set WS_EX_LAYERED on this window
    SetWindowLong(hwnd,
    GWL_EXSTYLE,
    GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

    // Make this window 70% alpha
    SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA);
    Note that the third parameter of SetLayeredWindowAttributes is a value that ranges from 0 to 255, with 0 making the window completely transparent and 255 making it completely opaque. This parameter mimics the more versatile BLENDFUNCTION of the AlphaBlend function.

    To make this window completely opaque again, remove the WS_EX_LAYERED bit by calling SetWindowLong and then ask the window to repaint. Removing the bit is desired to let the system know that it can free up some memory associated with layering and redirection. The code might look like this:

    // Remove WS_EX_LAYERED from this window styles
    SetWindowLong(hwnd,
    GWL_EXSTYLE,
    GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);

    // Ask the window and its children to repaint
    RedrawWindow(hwnd,
    NULL,
    NULL,
    RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

Popular pages Recent additions subscribe to a feed