ComponentFactory.Krypton.Toolkit.KryptonTreeView.InternalTreeView.WmPaint C# (CSharp) Method

WmPaint() private method

private WmPaint ( Message &m ) : void
m System.Windows.Forms.Message
return void
            private void WmPaint(ref Message m)
            {
                IntPtr hdc;
                PI.PAINTSTRUCT ps = new PI.PAINTSTRUCT();

                // Do we need to BeginPaint or just take the given HDC?
                if (m.WParam == IntPtr.Zero)
                    hdc = PI.BeginPaint(Handle, ref ps);
                else
                    hdc = m.WParam;

                // Create bitmap that all drawing occurs onto, then we can blit it later to remove flicker
                Rectangle realRect = CommonHelper.RealClientRectangle(Handle);

                // No point drawing when one of the dimensions is zero
                if ((realRect.Width > 0) && (realRect.Height > 0))
                {
                    IntPtr hBitmap = PI.CreateCompatibleBitmap(hdc, realRect.Width, realRect.Height);

                    // If we managed to get a compatible bitmap
                    if (hBitmap != IntPtr.Zero)
                    {
                        try
                        {
                            // Must use the screen device context for the bitmap when drawing into the
                            // bitmap otherwise the Opacity and RightToLeftLayout will not work correctly.
                            PI.SelectObject(_screenDC, hBitmap);

                            // Easier to draw using a graphics instance than a DC!
                            using (Graphics g = Graphics.FromHdc(_screenDC))
                            {
                                // Ask the view element to layout in given space, needs this before a render call
                                using (ViewLayoutContext context = new ViewLayoutContext(this, _kryptonTreeView.Renderer))
                                {
                                    context.DisplayRectangle = realRect;
                                    _drawPanel.Layout(context);
                                }

                                using (RenderContext context = new RenderContext(this, _kryptonTreeView, g, realRect, _kryptonTreeView.Renderer))
                                    _drawPanel.Render(context);

                                // We can only control the background color by using the built in property and not
                                // by overriding the drawing directly, therefore we can only provide a single color.
                                Color color1 = _drawPanel.GetPalette().GetBackColor1(_drawPanel.State);
                                if (color1 != BackColor)
                                    BackColor = color1;

                                // Replace given DC with the screen DC for base window proc drawing
                                IntPtr beforeDC = m.WParam;
                                m.WParam = _screenDC;
                                DefWndProc(ref m);
                                m.WParam = beforeDC;
                            }

                            // Now blit from the bitmap from the screen to the real dc
                            PI.BitBlt(hdc, 0, 0, realRect.Width, realRect.Height, _screenDC, 0, 0, PI.SRCCOPY);
                        }
                        finally
                        {
                            // Delete the temporary bitmap
                            PI.DeleteObject(hBitmap);
                        }
                    }
                }

                // Do we need to match the original BeginPaint?
                if (m.WParam == IntPtr.Zero)
                    PI.EndPaint(Handle, ref ps);
            }