为什么DrawImageUnscaled在使用WM_NCPAINT时会导致闪烁?

时间:2011-06-06 08:31:28

标签: c# gdi+ gdi

我正在构建一个派生自System.Windows.Forms.ContainerControl的控件,该控件具有我需要绘制自己的边框区域。由于没有OnPaintNonClientArea覆盖,我自己就像这样构建它(处理其他消息,例如WM_NCCALCSIZEWM_NCHITTEST等等,为了简洁而删除了:

protected override void WndProc(ref Message m)
{
  switch (m.Msg)
  {
    case WM_NCPAINT:
      IntPtr hDC = NativeApi.Methods.GetWindowDC(m.HWnd);
      if (hDC != IntPtr.Zero)
      {
        using (Graphics canvas = Graphics.FromHdc(hDC))
        {
          if (Width > 0 && Height > 0)
            using (PaintEventArgs e = new PaintEventArgs(canvas, new Rectangle(0, 0, Width, Height)))
            {
              OnPaintNonClientArea(e);
            }
        }
        NativeApi.Methods.ReleaseDC(m.HWnd, hDC);
      }
      m.Result = IntPtr.Zero;
      break;
  }
  base.WndProc(ref m);
}

OnPaintNonClientArea内,我做了:

private void OnPaintNonClientArea(PaintEventArgs e)
{
  if (_ncBuffer == null)
  {
    _ncBuffer = new Bitmap(Width, Height);
  }

  using (Graphics g = Graphics.FromImage(_ncBuffer))
  {
    // painting occurs here ...
  }
  // this causes flickering
  e.Graphics.DrawImageUnscaled(_ncBuffer, 0, 0, Width, Height);
}

保持OnPaintNonClientArea不变,这会消除闪烁:

protected override void WndProc(ref Message m)
{
  switch (m.Msg)
  {
    case WM_NCPAINT:
      using(Bitmap ncBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
      {
        using(Graphics ncGraphics = Graphics.FromImage(ncBitmap))
        {
          using (PaintEventArgs e = new PaintEventArgs(ncGraphics, new Rectangle(0, 0, Width, Height)))
          {
            OnPaintNonClientArea(e);
            IntPtr hDCWin = NativeApi.Methods.GetWindowDC(m.HWnd);
            IntPtr hDCImg = ncGraphics.GetHdc();
            IntPtr hBmp = ncBitmap.GetHbitmap();
            IntPtr hBmpOld = NativeApi.Methods.SelectObject(hDCImg, hBmp);
            Padding p = GetNonClientArea();
            NativeApi.Methods.ExcludeClipRect(hDCWin, p.Left, p.Top,Width- p.Right, Height-p.Bottom);
            NativeApi.Methods.BitBlt(hDCWin, 0, 0, Width, Height, hDCImg, 0, 0,NativeApi.TernaryRasterOperations.SRCCOPY);
            NativeApi.Methods.SelectObject(hDCImg, hBmpOld);
            NativeApi.Methods.DeleteObject(hBmp);
            ncGraphics.ReleaseHdc(hDCImg);
            NativeApi.Methods.ReleaseDC(m.HWnd, hDCWin);
          }
        }
      }
      m.Result = IntPtr.Zero;
      break;
  }
  base.WndProc(ref m);
}

那么,为什么DrawImageUnscaled导致这种闪烁?在绘制缓冲区之前,它似乎用白色画笔擦除了它所使用的区域。我没有在文档中找到任何澄清这个问题的内容。如果它只是控件周围的一个小边框,那就没那么重要了,但NC区域内会显示文字,所以区域清晰可见,因此闪烁真的很明显,很烦人。

相关问题:我是否正确使用本机GDI,或者是否存在我现在看不到的潜在问题?此外,在创建ncBitmap时,我使用的是控件的宽度和高度,但GDI +是独立分辨率的,那里有任何问题吗?

1 个答案:

答案 0 :(得分:2)

为了避免在UserControl中出现闪烁,我对BufferedGraphics类运气好了。

MSDN

这是一个选择吗?