protected override void WndProc(ref Message m) { switch (m.Msg) { //Do all painting in WM_PAINT to reduce flicker. case (int)Win32.Msgs.WM_ERASEBKGND: return; case (int)Win32.Msgs.WM_PAINT: // This code is influenced by Steve McMahon's article: // "Painting in the MDI Client Area". // http://vbaccelerator.com/article.asp?id=4306 // Use Win32 to get a Graphics object. Win32.PAINTSTRUCT paintStruct = new Win32.PAINTSTRUCT(); IntPtr screenHdc = User32.BeginPaint(m.HWnd, ref paintStruct); using (Graphics screenGraphics = Graphics.FromHdc(screenHdc)) { // Get the area to be updated. Rectangle clipRect = paintStruct.rcPaint; // Double-buffer by painting everything to an image and // then drawing the image. int width = (MdiClient.ClientRectangle.Width > 0 ? MdiClient.ClientRectangle.Width : 0); int height = (MdiClient.ClientRectangle.Height > 0 ? MdiClient.ClientRectangle.Height : 0); if (width > 0 && height > 0) { using (Image i = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(i)) { // This code comes from J Young's article: // "Generating missing Paint event for TreeView and ListView". // http://www.codeproject.com/cs/miscctrl/genmissingpaintevent.asp // Draw base graphics and raise the base Paint event. IntPtr hdc = g.GetHdc(); Message printClientMessage = Message.Create(m.HWnd, (int)Win32.Msgs.WM_PRINTCLIENT, hdc, IntPtr.Zero); DefWndProc(ref printClientMessage); g.ReleaseHdc(hdc); // Draw the image here. if (Image != null) { DrawImage(g, clipRect); } // Call our OnPaint here to draw graphics over the // original and raise our Paint event. OnPaint(new PaintEventArgs(g, clipRect)); } // Now draw all the graphics at once. screenGraphics.DrawImage(i, MdiClient.ClientRectangle); } } } User32.EndPaint(m.HWnd, ref paintStruct); return; case (int)Win32.Msgs.WM_SIZE: // Repaint on every resize. MdiClient.Invalidate(); break; case (int)Win32.Msgs.WM_NCCALCSIZE: // If AutoScroll is set to false, hide the scrollbars when the control // calculates its non-client area. if (!AutoScroll) { User32.ShowScrollBar(m.HWnd, (int)Win32.ScrollBars.SB_BOTH, 0 /*false*/); } break; } base.WndProc(ref m); }
public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
protected override void WndProc(ref Message m) { switch(m.Msg) { //Do all painting in WM_PAINT to reduce flicker. case (int)Win32.Msgs.WM_ERASEBKGND: return; case (int)Win32.Msgs.WM_PAINT: // This code is influenced by Steve McMahon's article: // "Painting in the MDI Client Area". // http://vbaccelerator.com/article.asp?id=4306 // Use Win32 to get a Graphics object. Win32.PAINTSTRUCT paintStruct = new Win32.PAINTSTRUCT(); IntPtr screenHdc = User32.BeginPaint(m.HWnd, ref paintStruct); using(Graphics screenGraphics = Graphics.FromHdc(screenHdc)) { // Get the area to be updated. Rectangle clipRect = paintStruct.rcPaint; // Double-buffer by painting everything to an image and // then drawing the image. int width = (MdiClient.ClientRectangle.Width > 0 ? MdiClient.ClientRectangle.Width : 0); int height = (MdiClient.ClientRectangle.Height > 0 ? MdiClient.ClientRectangle.Height : 0); if (width > 0 && height > 0) { using(Image i = new Bitmap(width, height)) { using(Graphics g = Graphics.FromImage(i)) { // This code comes from J Young's article: // "Generating missing Paint event for TreeView and ListView". // http://www.codeproject.com/cs/miscctrl/genmissingpaintevent.asp // Draw base graphics and raise the base Paint event. IntPtr hdc = g.GetHdc(); Message printClientMessage = Message.Create(m.HWnd, (int)Win32.Msgs.WM_PRINTCLIENT, hdc, IntPtr.Zero); DefWndProc(ref printClientMessage); g.ReleaseHdc(hdc); // Draw the image here. if(Image != null) DrawImage(g, clipRect); // Call our OnPaint here to draw graphics over the // original and raise our Paint event. OnPaint(new PaintEventArgs(g, clipRect)); } // Now draw all the graphics at once. screenGraphics.DrawImage(i, MdiClient.ClientRectangle); } } } User32.EndPaint(m.HWnd, ref paintStruct); return; case (int)Win32.Msgs.WM_SIZE: // Repaint on every resize. MdiClient.Invalidate(); break; case (int)Win32.Msgs.WM_NCCALCSIZE: // If AutoScroll is set to false, hide the scrollbars when the control // calculates its non-client area. if(!AutoScroll) User32.ShowScrollBar(m.HWnd, (int)Win32.ScrollBars.SB_BOTH, 0 /*false*/); break; } base.WndProc(ref m); }