-->

C++ WinAPI Conflict between SetLayeredWindowAttrib

2019-09-04 13:02发布

问题:

I have created a custom window using DWM. I painted the caption by using PaintCustomCaption() ,which is an example from MSDN. It worked properly until I added SetLayeredWindowAttributes().

Window before adding

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

After adding

I tried changing RGB values but it was still black except RGB(0,0,0).

I wonder if BitBlt() works properly.

Edited:

The reason I added SetLayeredWindowAttributes is to solve this problem

Do you have other ways to paint the caption?

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;
}

回答1:

If you keep the window border, you don't need to paint the caption yourself unless you want to add something to your caption.

That is, handle WM_NCCALCSIZE and WM_NCHITTEST normally.



回答2:

First, use RGB(200,201,202) as the transparency key instead of RGB(0,0,1).

You may try other values but it is the best one so far I have tested.

Then, add this after HBITMAP hbmOld=(HBITMAP)SelectObject(hdcPaint,hbm); in PaintCustomCaption():

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


标签: c++ winapi dwm