SetLayeredWindowAttributes 和 BitBlt 之间的 C++ WinAPI 冲突

C++ WinAPI Conflict between SetLayeredWindowAttributes and BitBlt

我使用 DWM 创建了自定义 window。我使用 PaintCustomCaption() ,which is an example from MSDN. It worked properly until I added SetLayeredWindowAttributes().

绘制了标题

Window 前加

SetLayeredWindowAttributes(hWnd,RGB(0,0,1),0,LWA_COLORKEY);

添加后

我尝试更改 RGB 值但它仍然是黑色,除了 RGB(0,0,0)。

我想知道 BitBlt() 是否正常工作。

已编辑:

我之所以添加SetLayeredWindow属性就是为了解决这个问题

你有其他方法来绘制标题吗?

case WM_ACTIVATE: {
    DwmExtendFrameIntoClientArea(hWnd,&m); // m={-1,-1,-1,-1};
    break;
}
case WM_INITDIALOG: {
    SetWindowPos(hWnd,NULL,0,0,500,500,SWP_NOMOVE|SWP_FRAMECHANGED);
    SetWindowLongPtr(hWnd,GWL_STYLE,WS_VISIBLE|WS_OVERLAPPEDWINDOW);
    SetWindowLongPtr(hWnd,GWL_EXSTYLE,WS_EX_LAYERED);
    SetLayeredWindowAttributes(hWnd,RGB(0,0,1),0,LWA_COLORKEY);
    RedrawWindow(hWnd,NULL,NULL,RDW_INVALIDATE|RDW_ERASE);
    return true;
}
case WM_PAINT: {
    hdc=BeginPaint(hWnd,&paintstruct);
    PaintCustomCaption(hWnd,hdc)
    EndPaint(hWnd,&paintstruct);
    break;
}

首先,使用 RGB(200,201,202) 作为透明度键而不是 RGB(0,0,1)。

您可以尝试其他值,但这是迄今为止我测试过的最好的值。

然后,在 PaintCustomCaption() 中的 HBITMAP hbmOld=(HBITMAP)SelectObject(hdcPaint,hbm); 之后添加:

FillRect(hdcPaint,&rcClient,CreateSolidBrush(RGB(200,201,202)));

如果保留 window 边框,则无需自己绘制标题,除非您想向标题添加内容。

即正常处理WM_NCCALCSIZEWM_NCHITTEST