我正在构建一个派生自System.Windows.Forms.ContainerControl
的控件,该控件具有我需要绘制自己的边框区域。由于没有OnPaintNonClientArea
覆盖,我自己就像这样构建它(处理其他消息,例如WM_NCCALCSIZE
,WM_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 +是独立分辨率的,那里有任何问题吗?