//Recreate internal graphics object protected override void OnResize( System.EventArgs e ) { if( internalBitmap == null || internalBitmap.Width != Width || internalBitmap.Height != Height ) { if( Width != 0 && Height != 0 ) { DisposeInternal(); internalBitmap = new Bitmap( Width, Height ); internalGraphics = Graphics.FromImage( internalBitmap ); } } } |
//Draw Internal Graphics IntPtr hdc = internalGraphics.GetHdc(); Message printClientMessage = Message.Create( Handle, WM_PRINTCLIENT, hdc, IntPtr.Zero ); DefWndProc( ref printClientMessage ); internalGraphics.ReleaseHdc( hdc ); |
//Add the missing OnPaint() call OnPaint( new PaintEventArgs( internalGraphics, Rectangle.FromLTRB( updateRect.left, updateRect.top, updateRect.right, updateRect.bottom ) ) ); |
//Draw Screen Graphics screenGraphics.DrawImage( internalBitmap, 0, 0 ); WM_ERASEBKGND消息被过滤掉,什么都不做。 case WM_ERASEBKGND: //removes flicker return; |
Bitmap temp = new Bitmap(internalBitmap, internalBitmap.Size); // 建立一个临时的位图temp,保存前面绘好的界面 temp.MakeTransparent(Color.White); // 设置白色为透明色 internalGraphics.FillRectangle(Brushes.White, 0, 0, this.Bounds.Width, this.Bounds.Height); // 在原来的内部位图对象上,用白色重画背景 if (image != null) // 如果设置了背景图,就在内部对象上画背景 internalGraphics.DrawImage (image, 0, 0, image.Width, image.Height); internalGraphics.DrawImage(temp, 0, 0, temp.Width, temp.Height);// 把前面绘好的界面按白色为透明色复合到内部位图上 screenGraphics.DrawImage( internalBitmap, 0, 0 ); // 把合成的临时位图刷到屏幕上 |
图一 测试程序 |