ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • How To Use UpdateLayeredWindow
    프로그래밍/Windows API 2012. 3. 5. 09:52

    In this post I will briefly explain how to use layered windows and specifically how to use UpdateLayeredWindow.

    The first thing you need to do is add the WS_EX_LAYERED style to your window. This can for example be done with a call to CreateWindowEx:

    hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0,
             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

    After your window is created we will load a PNG file with an alpha channel and use UpdateLayeredWindow to render the PNG on the window using the alpha channel of the PNG file as the transparency level for the window. This is done as follows:

    // Load our PNG image
    CImage img;
    img.Load("circle.png");
    // Get dimensions
    int iWidth = img.GetWidth();
    int iHeight = img.GetHeight();
    // Make mem DC + mem  bitmap
    HDC hdcScreen = GetDC(NULL);
    HDC hDC = CreateCompatibleDC(hdcScreen);
    HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);
    HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
    // Draw image to memory DC
    img.Draw(hDC, 0, 0, iWidth, iHeight, 0, 0, iWidth, iHeight);
    
    // Call UpdateLayeredWindow
    BLENDFUNCTION blend = {0};
    blend.BlendOp = AC_SRC_OVER;
    blend.SourceConstantAlpha = 255;
    blend.AlphaFormat = AC_SRC_ALPHA;
    POINT ptPos = {0, 0};
    SIZE sizeWnd = {iWidth, iHeight};
    POINT ptSrc = {0, 0};
    UpdateLayeredWindow(hWnd, hdcScreen, &ptPos, &sizeWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
    
    SelectObject(hDC, hBmpOld);
    DeleteObject(hBmp);
    DeleteDC(hDC);
    ReleaseDC(NULL, hdcScreen);

    Because I’m using CImage, you need to include the atlimage.h header.

    That’s all that is required for the basics of UpdateLayeredWindow.

    NOTE: The example above does not include any error checking. That is left for the reader as an excercise.

    출처-http://www.nuonsoft.com/blog/2009/05/27/how-to-use-updatelayeredwindow/ 

    댓글