现在的位置: 首页 > 综合 > 正文

UpdateLayeredWindow方式实现异型窗口

2019年05月08日 ⁄ 综合 ⁄ 共 1309字 ⁄ 字号 评论关闭

使用该种方法要注意几个问题:

1. 窗口属性必须是Top Window,子窗口(Win8之前的操作系统)不支持实现异形窗口

2. Layer Window没有WM_PAINT消息,需要自己调用OnPaint

3. 窗口属性可设置为

WS_POPUP | WS_VISIBLE, WS_EX_TOOLWINDOW | WS_EX_LAYERED

实现代码:

        CDC hdc = GetDC();  // Get the display device context (DC)
        CDC hdcSrc = CreateCompatibleDC(hdc); // creates a memory device context (DC) compatible with the display device context

        CRect rc;
        GetClientRect(&rc);

        // 创建DIB
        BITMAPINFO    bmInfo; 
        ZeroMemory (&bmInfo, sizeof(bmInfo));
        bmInfo.bmiHeader.biSize		= sizeof(BITMAPINFOHEADER);
        bmInfo.bmiHeader.biWidth	= rc.Width();
        bmInfo.bmiHeader.biHeight	= rc.Height();
        bmInfo.bmiHeader.biPlanes	= 1;
        bmInfo.bmiHeader.biBitCount = 32;

        UINT* pBits = NULL;		
        CBitmap hBmp = CreateDIBSection (hdcSrc, (BITMAPINFO*)&bmInfo, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
        HBITMAP hBmpOld = hdcSrc.SelectBitmap(hBmp);
        RECT rcDraw = {0, 0, c_n_popwnd_width, c_n_popwnd_height};

        BLENDFUNCTION blendFunction;
        blendFunction.AlphaFormat = AC_SRC_ALPHA;
        blendFunction.BlendFlags = 0;
        blendFunction.BlendOp = AC_SRC_OVER;
        blendFunction.SourceConstantAlpha = 255;
        POINT pptDst = {m_ptWnd.x, m_ptWnd.y};
        SIZE pDesSize = {c_n_popwnd_width, c_n_popwnd_height};
        POINT ptSrc = { 0, 0 };
        ////////////////////////////////

        m_bmpBkg.StretchBlt(hdcSrc, 0, 0, rc.Width(), rc.Height(), 0);  // 这里是要绘制的异形窗口背景
        BOOL bRet = ::UpdateLayeredWindow(m_hWnd, NULL, &pptDst, &pDesSize, hdcSrc, &ptSrc, CLR_NONE, &blendFunction, ULW_ALPHA);
        hdcSrc.SelectBitmap(hBmpOld);
        ReleaseDC(hdc);

抱歉!评论已关闭.