/// <summary> /// /// </summary> /// <param name="graphics"></param> /// <param name="rect"></param> private void DrawBackground(Graphics graphics, Rectangle rect) { Image imageToDisplay = this.Image; ControlRenderer.PaintBackGround(graphics, rect, BackColor, true); //paint the image if (imageToDisplay != null) { using (Region imageRegion = new Region(rect)) { Region r = graphics.Clip; graphics.Clip = imageRegion; Rectangle imageRect = new Rectangle(rect.X, rect.Y, imageToDisplay.Size.Width, imageToDisplay.Height); if (HasBorder) { imageRect.X++; imageRect.Y++; } ControlRenderer.DrawImage(graphics, imageRect, imageToDisplay, 0, 0); graphics.Clip = r; } } }
/// <summary> Paints the control. </summary> private void PaintNotFocusedControl() { Debug.Assert(!Focused); using (Graphics gr = Graphics.FromHwnd(this.Handle)) { //Simulate Transparency if (IsTransparent || BackColor.A < 255) { if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = this; } System.Drawing.Drawing2D.GraphicsContainer g = gr.BeginContainer(); Rectangle translateRect = this.Bounds; int borderWidth = (this.Width - this.ClientSize.Width) / 2; gr.TranslateTransform(-(Left + borderWidth), -(Top + borderWidth)); PaintEventArgs pe = new PaintEventArgs(gr, translateRect); this.InvokePaintBackground(Parent, pe); this.InvokePaint(Parent, pe); gr.ResetTransform(); gr.EndContainer(g); pe.Dispose(); if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = null; } } Rectangle rect = this.ClientRectangle; //In case of Fixed Single border style, we get same client rectangle & bounds. So we need to reduce them to draw the 2D border. if (BorderStyle == BorderStyle.FixedSingle) { rect.Inflate(-2, -2); } ControlRenderer.PaintBackGround(gr, rect, (IsTransparent ? Color.Transparent : this.BackColor), Enabled || IsTransparent); PaintForeground(gr, rect, ForeColor); } }