无法在 WM_NCPAINT 上的标题栏中绘制? [英] Can't draw in title bar on WM_NCPAINT?

查看:30
本文介绍了无法在 WM_NCPAINT 上的标题栏中绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于没有WS_CAPTION,我试图在标题栏区域绘制一些东西来表示X,它只使用了WS_EX_TOOLWINDOW |WS_EX_TOPMOSTWS_POPUP|WS_THICKFRAME.但我不能在任何地方画任何东西.我在下面做了一个测试,只是用红色填充它,但没有任何改变.我做错了什么或遗漏了什么?

I'm trying to draw something in the title bar area to represent an X since there is no WS_CAPTION, it just uses WS_EX_TOOLWINDOW | WS_EX_TOPMOST and WS_POPUP|WS_THICKFRAME. But I can't get anything to draw anywhere. I did a test below to just fill it all in red, but nothing changed. What am I doing wrong or missing?

case WM_NCACTIVATE:
case WM_NCPAINT:
{
  // call default handler (I've tried it both ways, with and without DefWindowProc)
  ::DefWindowProc(hwnd, umsg, wparam, lparam);
  HDC hdc;
  if ((hdc=::GetWindowDC(hwnd))!=NULL) { 
    // Paint into this DC 
    RECT rcwin;
    if (::GetWindowRect(hwnd, &rcwin)) {
      HBRUSH hbrush=::CreateSolidBrush(RGB(255, 0, 0));
      if (hbrush) {
        rcwin.right-=rcwin.left;
        rcwin.bottom-=rcwin.top;
        rcwin.left=rcwin.top=0;
        ::FillRect(hdc, &rcwin, hbrush);
        ::DeleteObject(hbrush);
      }
    }
    ::ReleaseDC(hwnd, hdc);
  }
  return 0;
}

推荐答案

基于 Remy 关于邪恶 WM_NCPAINT 的链接,从 pascal 版本转换为下面的 C++ 版本.它与 stackoverflow 中的链接一样有效,但同样,前提是提供了 WS_CAPTION.我只是在这里张贴完整性.

Based on a Link from Remy about the evil WM_NCPAINT, converted from the pascal version to the C++ version below. It works as well as the link in stackoverflow but again, only if WS_CAPTION is provided. I'm just posting here for completeness.

case WM_NCPAINT:
{
    #ifndef DCX_USESTYLE
      #define DCX_USESTYLE 0x00010000
    #endif

  HDC hdc=::GetDCEx(hwnd, 0, DCX_WINDOW|DCX_USESTYLE);
  if (hdc) {
    RECT rcclient;
    ::GetClientRect(hwnd, &rcclient);
    RECT rcwin;
    ::GetWindowRect(hwnd, &rcwin);
    POINT ptupleft;
    ptupleft.x=rcwin.left;
    ptupleft.y=rcwin.top;
    ::MapWindowPoints(0, hwnd, (LPPOINT) &rcwin, (sizeof(RECT)/sizeof(POINT)));
    ::OffsetRect(&rcclient, -rcwin.left, -rcwin.top);
    ::OffsetRect(&rcwin, -rcwin.left, -rcwin.top);

    HRGN rgntemp=NULL;
    if (wparam==NULLREGION || wparam==ERROR) {
      ::ExcludeClipRect(hdc, rcclient.left, rcclient.top, rcclient.right, rcclient.bottom);
    }
    else {
      rgntemp=::CreateRectRgn(rcclient.left+ptupleft.x, rcclient.top+ptupleft.y, rcclient.right+ptupleft.x, rcclient.bottom+ptupleft.y);
      if (::CombineRgn(rgntemp, (HRGN) wparam, rgntemp, RGN_DIFF)==NULLREGION) {
         // nothing to paint
      }
      ::OffsetRgn(rgntemp, -ptupleft.x, -ptupleft.y);
      ::ExtSelectClipRgn(hdc, rgntemp, RGN_AND);
    }

    HBRUSH hbrush = ::CreateSolidBrush(RGB(255, 0, 0));
    ::FillRect(hdc, &rcwin, hbrush);
    ::DeleteObject(hbrush);

    ::ReleaseDC(hwnd, hdc);
    if (rgntemp!=0) {
      ::DeleteObject(rgntemp);
    }
  }
  return 0;
}

这篇关于无法在 WM_NCPAINT 上的标题栏中绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆