Exemplo n.º 1
0
        /// <summary>
        ///     Invokes the default window procedure associated with this window.
        /// </summary>
        /// <param name="m">A <see cref="System.Windows.Forms.Message" /> that is associated with the current Windows message. </param>
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
            //Do all painting in WM_PAINT to reduce flicker.
            case (int)WindowsMessage.WM_ERASEBKGND:
                return;

            case (int)WindowsMessage.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.
                var paintStruct = new PAINTSTRUCT();
                var screenHdc   = NativeMethods.BeginPaint(m.HWnd, ref paintStruct);

                using (var screenGraphics = Graphics.FromHdc(screenHdc))
                {
                    // Get the area to be updated.
                    var clipRect = new Rectangle(
                        paintStruct.rcPaint.Left,
                        paintStruct.rcPaint.Top,
                        paintStruct.rcPaint.Right - paintStruct.rcPaint.Left,
                        paintStruct.rcPaint.Bottom - paintStruct.rcPaint.Top);

                    // Double-buffer by painting everything to an image and
                    // then drawing the image.
                    var width  = (MdiClient.ClientRectangle.Width > 0 ? MdiClient.ClientRectangle.Width : 0);
                    var height = (MdiClient.ClientRectangle.Height > 0 ? MdiClient.ClientRectangle.Height : 0);
                    using (Image i = new Bitmap(width, height))
                    {
                        using (var 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.
                            var hdc = g.GetHdc();
                            var printClientMessage =
                                Message.Create(m.HWnd, (int)WindowsMessage.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);
                    }
                }

                NativeMethods.EndPaint(m.HWnd, ref paintStruct);
                return;

            case (int)WindowsMessage.WM_SIZE:

                // Repaint on every resize.
                MdiClient.Invalidate();
                break;


            case (int)WindowsMessage.WM_NCCALCSIZE:

                // If AutoScroll is set to false, hide the scrollbars when the control
                // calculates its non-client area.
                if (!autoScroll)
                {
                    ShowScrollBar(m.HWnd, SB.SB_BOTH, 0 /*false*/);
                }
                break;
            }

            base.WndProc(ref m);
        }