简体   繁体   English

如何在窗口周围绘制矩形而不覆盖Win32中的标题栏

[英]How can I paint a rectangle around a window without overriding the title bar in win32

I want to draw a rectangle around my window but I don't want to override the title bar. 我想在窗口周围绘制一个矩形,但是我不想覆盖标题栏。 what I wrote so far in the window callback function is: 到目前为止,我在window回调函数中写的是:

    case WM_NCPAINT:
    {
        HDC hdc;
        RECT rect;
        HPEN pen;

        hdc=GetDCEx(hWnd,(HRGN)wParam,DCX_WINDOW|DCX_CACHE|DCX_INTERSECTRGN|DCX_LOCKWINDOWUPDATE);
        GetWindowRect(hWnd,&rect);
        pen=CreatePen(PS_SOLID, 10, RGB(255, 0, 0));//red pen 10 pixels in size
        SelectObject(hdc,pen);
        Rectangle(hdc,0,0,(rect.right-rect.left),(rect.bottom-rect.top));
        DeleteObject(pen);
        ReleaseDC(hWnd,hdc);
    }
    break;

However, this draws over the window title bar with white brush. 但是,这会使用白色画笔绘制窗口标题栏。

How can I make it not to paint over the title bar? 如何避免不在标题栏上绘画? I'm loosing the title bar text and the menu... 我正在失去标题栏文本和菜单...

I have tried using HOLLOW_BRUSH before creating the pen as follows: 在创建笔之前,我曾尝试使用HOLLOW_BRUSH,如下所示:

        HBRUSH b=CreateSolidBrush(HOLLOW_BRUSH);
        SelectObject(hdc,b);

But that only caused the title bar to not be drawn at all (being black). 但这只会导致标题栏根本无法绘制(呈黑色)。

By handling the WM_NCPAINT message, you are telling the window manager that you are taking responsibility for painting the entire non-client area, and so the window manager will not draw any of it for you. 通过处理WM_NCPAINT消息,您告诉窗口管理器您有责任绘制整个非客户区域,因此窗口管理器将不会为您绘制任何内容。

If you want the original title bar to be drawn then you need to call DefWindowProc() first, then do your own drawing "on top" of what it draws. 如果要绘制原始标题栏,则需要先调用DefWindowProc() ,然后在其绘制的“顶部”进行自己的绘制。

You may also need to use ExcludeClipRect() to prevent the client area from being drawn over if you wish to draw the entire non-client area at once with a single rectangle. 如果您希望使用单个矩形一次绘制整个非客户区域,则可能还需要使用ExcludeClipRect()防止客户区域被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM