/// <summary> /// /// </summary> /// <param name="pevent"></param> protected override void OnPaintBackground(PaintEventArgs pevent) { //Simulate Transparency if (BackColor == Color.Transparent || BackColor.A < 255) { if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = this; } System.Drawing.Drawing2D.GraphicsContainer g = pevent.Graphics.BeginContainer(); Rectangle translateRect = this.Bounds; pevent.Graphics.TranslateTransform(-Left, -Top); PaintEventArgs pe = new PaintEventArgs(pevent.Graphics, translateRect); this.InvokePaintBackground(Parent, pe); this.InvokePaint(Parent, pe); pevent.Graphics.ResetTransform(); pevent.Graphics.EndContainer(g); pe.Dispose(); if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = null; } } ControlRenderer.FillRectAccordingToGradientStyle(pevent.Graphics, this.ClientRectangle, BackColor, ForeColor, ControlStyle.NoBorder, false, GradientColor, GradientStyle); if (TransparentChild != null && DesignMode) { ControlRenderer.RenderOwnerDrawnControlsOfParent(pevent.Graphics, TransparentChild); } }
/// <summary> /// /// </summary> /// <param name="graphics"></param> /// <param name="rect"></param> private void DrawBackground(Graphics graphics, Rectangle rect) { Color bgColorToUse = (Parent is TableControl) ? ((TableControl)Parent).GetBGColorToUse(this) : BackColor; ControlRenderer.FillRectAccordingToGradientStyle(graphics, rect, bgColorToUse, ForeColor, Style, false, GradientColor, GradientStyle, BorderType); }
/// <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> /// /// </summary> /// <param name="e"></param> protected override void OnPaintBackground(PaintEventArgs e) { if (BackColor == Color.Transparent || BackColor.A < 255) { if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = this; } int hScrollHeight = (this.HScroll ? SystemInformation.HorizontalScrollBarHeight : 0); int vScrollWidth = (this.VScroll ? SystemInformation.VerticalScrollBarWidth : 0); int borderWidth = ((this.Width - this.ClientSize.Width) - vScrollWidth) / 2; System.Drawing.Drawing2D.GraphicsContainer g = e.Graphics.BeginContainer(); Rectangle translateRect = this.Bounds; e.Graphics.TranslateTransform(-(Left + borderWidth), -(Top + borderWidth)); PaintEventArgs pe = new PaintEventArgs(e.Graphics, translateRect); this.InvokePaintBackground(Parent, pe); this.InvokePaint(Parent, pe); e.Graphics.ResetTransform(); e.Graphics.EndContainer(g); pe.Dispose(); if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = null; } } ControlRenderer.PaintMgPanel(this, e.Graphics); }
private static void DrawTabText(MgTabControl tabControl, Graphics graphics, GraphicsPath tabPath, int index) { Rectangle tabBounds = GetTabTextRect(tabControl, tabPath, index); Color textColor; // For selected tab // When 'SelectedTabColor' is set TextColor is SelectedTabFgColor else it is Tab's ForeColor if (tabControl.SelectedIndex == index) { textColor = tabControl.SelectedTabFgColor != Color.Empty ? tabControl.SelectedTabFgColor : tabControl.ForeColor; } else { textColor = tabControl.TitleFgColor; // Text color it is Foreground of Title Color for non selected tabs } int orientation = GetOrientation(tabControl); if (tabControl.RightToLeftLayout) { Rectangle clipRect = tabControl.GetTabRect(index); clipRect.X = tabControl.Width - clipRect.Right; // Set clip so that image is not painted outside tab bounds graphics.SetClip(clipRect); } FontDescription font = new FontDescription(tabControl.Font); ControlRenderer.PrintText(graphics, tabBounds, textColor, font, GetTabText(tabControl, orientation, index), false, ContentAlignment.MiddleCenter, true, false, false, false, (tabControl.RightToLeft == RightToLeft.Yes), false, orientation, true); }
protected override void OnPaint(PaintEventArgs e) { //Simulate Transparency if (BackColor == Color.Transparent) { #if !PocketPC if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = this; } System.Drawing.Drawing2D.GraphicsContainer g = e.Graphics.BeginContainer(); Rectangle translateRect = this.Bounds; e.Graphics.TranslateTransform(-Left, -Top); PaintEventArgs pe = new PaintEventArgs(e.Graphics, translateRect); this.InvokePaintBackground(Parent, pe); this.InvokePaint(Parent, pe); //fixed bug#:796041, the forgound not need to display //this.InvokePaint(Parent, pe); e.Graphics.ResetTransform(); e.Graphics.EndContainer(g); pe.Dispose(); if (Parent is ISupportsTransparentChildRendering) { ((ISupportsTransparentChildRendering)Parent).TransparentChild = null; } #endif } ControlRenderer.FillRectAccordingToGradientStyle(e.Graphics, ClientRectangle, BackColor, LinkColor, 0, false, GradientColor, GradientStyle); ContentAlignment newTextAlign = ControlUtils.GetOrgContentAligment(RightToLeft, TextAlign); FontDescription font = new FontDescription(Font); ControlRenderer.PrintText(e.Graphics, ClientRectangle, LinkColor, font, Text, false, newTextAlign, Enabled, true, !UseMnemonic, false, (RightToLeft == RightToLeft.Yes)); //fixed bug #:765815 : when parking on hypertext button,focus need to seen on text on the button (not on entire button) if (Focused) { Size textExt = Utils.GetTextExt(Font, Text, this); // get the display the focus on the text of the control Rectangle textRect = ControlUtils.GetFocusRect(this, ClientRectangle, newTextAlign, textExt); //ass offset, it will look as in online textRect.Inflate(2, 2); textRect.Width -= 2; textRect.Height -= 1; textRect.X = Math.Max(1, textRect.X); textRect.Y = Math.Max(1, textRect.Y); textRect.Width = Math.Min(textRect.Width, ClientRectangle.Width - textRect.X); textRect.Height = Math.Min(textRect.Height, ClientRectangle.Height - textRect.Y); ControlPaint.DrawFocusRectangle(e.Graphics, textRect); } }
/// <summary> /// Draw the control on the given Graphics at the specified rectangle. /// </summary> /// <param name="graphics"></param> /// <param name="rect"></param> public void Draw(Graphics graphics, Rectangle rect) { ControlStyle style = (BorderStyle == BorderStyle.Fixed3D ? ControlStyle.Windows : ControlStyle.NoBorder); ControlRenderer.PaintBackgroundAndBorder(graphics, rect, (IsTransparent ? Color.Transparent : this.BackColor), ForeColor, style, true, Enabled || IsTransparent); rect.Inflate((ClientRectangle.Width - Bounds.Width) / 2, (ClientRectangle.Height - Bounds.Height) / 2); PaintForeground(graphics, rect, ForeColor); }
/// <summary> /// Creates pen for border painting /// </summary> /// <returns></returns> public static Pen InitPenForPaint(Color foreColor, ControlStyle controlStyle, CtrlLineType lineType, int lineWidth) { Pen pen = new Pen(foreColor); // set properties of pen pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; // Paint from inside when pen width is greater than 1 if (controlStyle == ControlStyle.TwoD) { pen.Width = lineWidth; float[] dashPattern = ControlRenderer.GetDashPattern(lineType); if (dashPattern != null) { pen.DashPattern = dashPattern; } } return(pen); }
/// <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); } }
/// <summary> /// Paints the Line control /// TODO : Move this method to LineRenderer /// </summary> public static void PaintLineForeGround(Graphics g, Color fgColor, ControlStyle style, int lineWidth, CtrlLineType lineStyle, Point pt1, Point pt2) { #if !PocketPC SmoothingMode orgmode = g.SmoothingMode; g.SmoothingMode = SmoothingMode.AntiAlias; #endif Pen pen = new Pen(fgColor, lineWidth); // TODO : use new pen's cache here float[] dashPattern = ControlRenderer.GetDashPattern(lineStyle); if (dashPattern != null) { pen.DashPattern = dashPattern; } LineRenderer.Draw(g, pen, pt1, pt2, style); #if !PocketPC g.SmoothingMode = orgmode; #endif }
/// <summary> paint control backgound and image</summary> /// <param name="control"></param> /// <param name="graphics"></param> /// <param name="useImageSize" - do the image will be display on all the control (for button image)></param> /// <param name="DisplayRect" - the display rect of the color></param> public static void PaintBackgoundColorAndImage(Control control, Graphics graphics, bool useImageSize, Rectangle DisplayRect) { Image backgroundImage = null; GradientColor gradientColor = ControlUtils.GetGradientColor(control); GradientStyle gradientStyle = ControlUtils.GetGradientStyle(control); ControlRenderer.FillRectAccordingToGradientStyle(graphics, DisplayRect, control.BackColor, control.ForeColor, ControlStyle.NoBorder, false, gradientColor, gradientStyle); #if !PocketPC backgroundImage = control.BackgroundImage; #else if (control is ButtonBase) { ButtonBase button = (ButtonBase)control; backgroundImage = button.BackgroundImage; } else if (control is MgPanel) { MgPanel panel = (MgPanel)control; backgroundImage = panel.BackGroundImage; } #endif if (backgroundImage != null) { Rectangle rectImage; if (useImageSize) { rectImage = new Rectangle(DisplayRect.X, DisplayRect.Y, backgroundImage.Width, backgroundImage.Height); } else { rectImage = control.ClientRectangle; } DrawImage(graphics, rectImage, backgroundImage, 0, 0); } }
/// <summary> Renders the text on the Control. </summary> /// <param name="g"></param> /// <param name="rect"></param> /// <param name="fgColor"></param> protected virtual void PaintForeground(Graphics g, Rectangle rect, Color fgColor) { Rectangle drawRect = rect; String str = GetNotFocusedText(); if (UseSystemPasswordChar && str != null && !HasHintWithoutText()) { StringBuilder sb = new StringBuilder(); sb.Append(PasswordChar, str.Length); str = sb.ToString(); } else if (Multiline) { // find the character near the top left corner of the control. int charIdx = GetCharIndexFromPosition(new Point(ClientRectangle.Left, ClientRectangle.Top + 2)); str = str.Substring(charIdx); } ControlRenderer.PrintText(g, drawRect, GetNotFocusedColor(), FontDescription, str, Multiline, TextAlign, Enabled, WordWrap, true, false, RightToLeft == RightToLeft.Yes); }
/// <summary>paint the backround button</summary> /// <param name="mgButton"></param> /// <param name="e"></param> private static void PaintBackground(Control buttonBase, PaintEventArgs e) { Rectangle displayRect = buttonBase.ClientRectangle; // when button is image , the display rect is on all the button, else the display rect is on the tect rect if (!ControlUtils.IsImageButton(buttonBase)) { displayRect = GetTextRect(buttonBase); } ControlRenderer.PaintBackgoundColorAndImage(buttonBase, e.Graphics, false, displayRect); #if PocketPC // Draw the border for the button-appearance checkbox if (buttonBase is MgCheckBox) { Rectangle rect = buttonBase.ClientRectangle; rect.Inflate(-1, -1); Color fgColor = buttonBase.ForeColor; ControlStyle borderStyle = (((MgCheckBox)buttonBase).CheckState == CheckState.Unchecked) ? ControlStyle.ThreeD : ControlStyle.TwoD; BorderRenderer.PaintBorder(e.Graphics, rect, fgColor, borderStyle, false); } #endif }
/// <summary> /// /// </summary> /// <param name="graphics"></param> /// <param name="rect"></param> protected virtual void DrawForeground(Graphics graphics, Rectangle rect) { ControlRenderer.PrintText(graphics, rect, ForeColor, FontDescription, Text, Multiline, TextAlign, Enabled, true, !UseMnemonic, false, (RightToLeft == RightToLeft.Yes), FontOrientation); }
/// <summary>paint the push button control /// Can be called from : /// 1. push control with property 'ButtonStyle' = button /// 2. CheckBox control with property appearance = button /// 3. Radio control with property appearance = button /// </summary> /// <param name="mgButton"></param> /// <param name="e"></param> private static void DrawText(Control control, PaintEventArgs e) { Debug.Assert(control.Visible); bool restoreClip = false; Region r = e.Graphics.Clip; bool isImage = false; ContentAlignment textAlign = ContentAlignment.MiddleLeft; RightToLeft rightToLeft = RightToLeft.No; String Text = ((IDisplayInfo)control).TextToDisplay; if (Text == null) { return; } Rectangle displayRect = control.ClientRectangle; #if PocketPC if (control is MgButtonBase) { isImage = ((MgButtonBase)control).BackgroundImage != null; textAlign = ((MgButtonBase)control).TextAlign; rightToLeft = ((MgButtonBase)control).RightToLeft; } else if (control is MgCheckBox) { isImage = ((MgCheckBox)control).Image != null; textAlign = ((MgCheckBox)control).TextAlign; rightToLeft = ((MgCheckBox)control).RightToLeft; } else { Debug.Assert(false); } #else if (control is ButtonBase) { if (control is IImageProperty) { isImage = ((IImageProperty)control).Image != null; } else { isImage = ((ButtonBase)control).BackgroundImage != null; } textAlign = ((ButtonBase)control).TextAlign; rightToLeft = ((ButtonBase)control).RightToLeft; } else { Debug.Assert(false); } #endif // The runtime engine sends the alignment in reverse order if rightToLeft=Yes. // This is because the .net framework need it to be in the reverse order when rightToLeft=Yes. // So, when we paint the control, we should convert it back to the original alignment. textAlign = ControlUtils.GetOrgContentAligment(rightToLeft, textAlign); if (!isImage) { //2. get the display rect displayRect = GetTextRect(control); if (control is MgPushButton) { restoreClip = true; using (Region TextRegion = new Region(displayRect)) { e.Graphics.Clip = TextRegion; } } } bool isMultiLine = ControlUtils.GetIsMultiLine(control); bool RTL = rightToLeft == RightToLeft.Yes ? true : false; //4. display the text of the control FontDescription font = new FontDescription(control.Font); ControlRenderer.PrintText(e.Graphics, displayRect, control.ForeColor, font, Text, isMultiLine, textAlign, control.Enabled, false, false, false, RTL, true); if (restoreClip) { e.Graphics.Clip = r; } //focus rect should not be drawn for image button. if (!ControlUtils.IsImageButton(control)) { if (control.Focused) { displayRect.Inflate(-1, -1); ControlPaint.DrawFocusRectangle(e.Graphics, displayRect); } } }
/// <summary> /// Draw the text and the focus rectangle on a CheckBox/RadioButon control. /// </summary> /// <param name="control"></param> /// <param name="e"></param> /// <param name="textToDisplay"></param> public static void DrawTextAndFocusRect(ButtonBase control, PaintEventArgs e, String textToDisplay, Rectangle rectangle, int textOffset) { bool isMultiLine = ControlUtils.GetIsMultiLine(control); Rectangle textRect = new Rectangle(); Size textSize = new Size(); ContentAlignment NewTextAli = ContentAlignment.MiddleCenter; FontDescription font = new FontDescription(control.Font); if (!String.IsNullOrEmpty(textToDisplay)) { textRect = rectangle; NewTextAli = ControlUtils.GetOrgContentAligment(control.RightToLeft, control.TextAlign); textSize = Utils.GetTextExt(control.Font, textToDisplay, control); ControlUtils.AlignmentInfo AlignmentInfo = ControlUtils.GetAlignmentInfo(control.TextAlign); int Offset = (int)((float)(BOX_WIDTH + textOffset) * Utils.GetDpiScaleRatioX(control)); switch (AlignmentInfo.HorAlign) { case AlignmentTypeHori.Left: if (control.RightToLeft == RightToLeft.No) { textRect.X += Offset; textRect.Width -= Offset; } if (control.RightToLeft == RightToLeft.Yes) { textRect.X -= Offset; } break; case AlignmentTypeHori.Right: if (isMultiLine && control.RightToLeft == RightToLeft.No) { textRect.X += Offset; textRect.Width -= Offset; } break; case AlignmentTypeHori.Center: if (control.RightToLeft == RightToLeft.No) { textRect.X += Offset; textRect.Width -= Offset; // if the text is bigger then the display rect. if (!isMultiLine) { if (textSize.Width > textRect.Width) { textRect.X += (int)(((textRect.Width - textSize.Width) / 2)); textRect.Width = Math.Max(textRect.Width, textSize.Width); } } } else if (control.RightToLeft == RightToLeft.Yes) { textRect.X -= Offset; textRect.Width += Offset; } break; } ControlRenderer.PrintText(e.Graphics, textRect, control.ForeColor, font, textToDisplay, isMultiLine, NewTextAli, control.Enabled, true, false, true, control.RightToLeft == RightToLeft.Yes, true); } // display the focus on the text of the control if (control.Focused) { if (!String.IsNullOrEmpty(textToDisplay)) { if (isMultiLine) { textSize = GetTextSize(e, textToDisplay, font, ref textRect, isMultiLine); } // get the display the focus on the text of the control textRect = ControlUtils.GetFocusRect(control, textRect, NewTextAli, textSize); ControlPaint.DrawFocusRectangle(e.Graphics, textRect); } else { //For CheckBox, if the text is not available, focus rect should be drawn on the Glyph. if (control is MgCheckBox) { DrawFocusRectOnCheckBoxGlyph((MgCheckBox)control, e.Graphics); } } } }
/// <summary> /// draw header section title /// </summary> /// <param name="hdc"></param> /// <param name="rc"></param> internal void DrawTitle(IntPtr hdc, NativeWindowCommon.RECT rc, int index, bool supportsMultilineText, bool addEndEllipsesFlag, bool rightToLeftLayout) { Header.HeaderRenderer.GetTitleTextRect(index, ref rc); using (Graphics gr = Graphics.FromHdc(hdc)) { //draw sort icon if (SortMark != HeaderSectionSortMarks.Non && !HasFilter) { int iconWindth = SortIconWidth(); DrawSortMark(hdc, rc, gr); rc.right -= iconWindth + Header.SORT_ICON_LEFT_RIGHT_MARGIN; } if (HasFilter) { rc.right -= HeaderSection.FILTER_WIDTH + Header.SORT_ICON_LEFT_RIGHT_MARGIN; } //draw ... in the end of text if width is too short int width = rc.right - rc.left; int format = NativeWindowCommon.DT_EDITCONTROL | NativeWindowCommon.DT_EXTERNALLEADING; if (supportsMultilineText) { format |= NativeWindowCommon.DT_WORDBREAK; } StringBuilder stringBuilder = new StringBuilder(Text); if (addEndEllipsesFlag) { //for windows CE && orientated fonts DT_END_ELLIPSIS style is not supported //http://support.microsoft.com/kb/249678 format |= NativeWindowCommon.DT_END_ELLIPSIS; } if (Environment.OSVersion.Platform == PlatformID.WinCE) { if (Text.IndexOf("\n") != -1) { SizeF size = gr.MeasureString(text, Font); int cur, len = cur = text.Length; while (size.Width > width && cur > 1) { cur = --len; stringBuilder.Length = len; while (cur > 1 && len - cur < 3) { stringBuilder[--cur] = '.'; } size = gr.MeasureString(stringBuilder.ToString(), Font); } } } NativeWindowCommon.SetBkMode(hdc, NativeWindowCommon.TRANSPARENT); NativeWindowCommon.SetTextColor(hdc, ColorTranslator.ToWin32(Color)); #if !PocketPC if (FontOrientation != 0) { Rectangle rectangle = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); ControlRenderer.PrintRotatedText(hdc, FontDescription, FontOrientation, stringBuilder.ToString(), rectangle, ContentAlignment, rc, rightToLeftLayout); } else #endif { //text flags are exactly the same as DrawText DT_*** flags format |= (int)Utils.ContentAlignment2TextFlags(ContentAlignment); if (isSingleLine()) { format |= NativeWindowCommon.DT_SINGLELINE; } IntPtr hFont = FontDescription.FontHandle; NativeWindowCommon.SelectObject(hdc, hFont); NativeWindowCommon.DrawText(hdc, stringBuilder.ToString(), stringBuilder.Length, ref rc, format); } #if !PocketPC //Set the rectangle back if (HasFilter) { rc.right += HeaderSection.FILTER_WIDTH + Header.SORT_ICON_LEFT_RIGHT_MARGIN; DrawFilter(hdc, rc); } #endif } }