public void DrawText (TextFormatFlags flags) { // Text adjustments Rectangle text_bounds = new Rectangle (bounds.X + 8, bounds.Y, bounds.Width - 13, bounds.Height); TextRenderer.DrawText (graphics, subItem.Text, subItem.Font, text_bounds, subItem.ForeColor, flags); }
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // Prepaint DataGridViewPaintParts pre = DataGridViewPaintParts.Background | DataGridViewPaintParts.SelectionBackground; pre = pre & paintParts; base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, pre); // Paint content if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground) { Color color = Selected ? cellStyle.SelectionForeColor : cellStyle.ForeColor; TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl; Rectangle contentbounds = cellBounds; contentbounds.Height -= 2; contentbounds.Width -= 2; if (formattedValue != null) { TextRenderer.DrawText(graphics, formattedValue.ToString(), cellStyle.Font, contentbounds, color, flags); } Point loc = new Point(cellBounds.Right - 14, cellBounds.Y + ((cellBounds.Height - 4) / 2)); if (sortGlyphDirection == SortOrder.Ascending) { using (Pen p = new Pen(color)) { graphics.DrawLine(p, loc.X + 4, loc.Y + 1, loc.X + 4, loc.Y + 2); graphics.DrawLine(p, loc.X + 3, loc.Y + 2, loc.X + 5, loc.Y + 2); graphics.DrawLine(p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3); graphics.DrawLine(p, loc.X + 1, loc.Y + 4, loc.X + 7, loc.Y + 4); graphics.DrawLine(p, loc.X + 0, loc.Y + 5, loc.X + 8, loc.Y + 5); } } else if (sortGlyphDirection == SortOrder.Descending) { using (Pen p = new Pen(color)) { graphics.DrawLine(p, loc.X + 4, loc.Y + 5, loc.X + 4, loc.Y + 4); graphics.DrawLine(p, loc.X + 3, loc.Y + 4, loc.X + 5, loc.Y + 4); graphics.DrawLine(p, loc.X + 2, loc.Y + 3, loc.X + 6, loc.Y + 3); graphics.DrawLine(p, loc.X + 1, loc.Y + 2, loc.X + 7, loc.Y + 2); graphics.DrawLine(p, loc.X + 0, loc.Y + 1, loc.X + 8, loc.Y + 1); } } } // Postpaint DataGridViewPaintParts post = DataGridViewPaintParts.Border; if (this is DataGridViewTopLeftHeaderCell) { post |= DataGridViewPaintParts.ErrorIcon; } post = post & paintParts; base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, post); }
/// <summary> /// 关闭Tab之前触发事件 /// </summary> /// <summary> /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (!Visible) { return; } //画选点击选项卡, if (TabCount > 0) { var g = e.Graphics; Share.GraphicSetup(g); // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // alpha = Alpha.Normal; var sb = new SolidBrush(Share.BackColor); var pen = new Pen(Share.BorderColor); // g.FillPath(sb, GetPath(ClientRectangle, 5)); //画整体边框 // g.DrawPath(pen, GetPath(ClientRectangle, 5)); //画整体背景 var tabRect = Rectangle.Empty; var selecttabRect = Rectangle.Empty; // Point cursorPoint = this.PointToClient(MousePosition); TabPage page = null; TabPage selectPage = null; GraphicsPath gp = null; GraphicsPath selectgp = null; for (var i = 0; i < TabCount; i++) { page = TabPages[i]; tabRect = GetTabRect(i); tabRect.X += 2; tabRect.Y += 2; gp = DrawHelper.GetGoogleTabPath(tabRect, 8); if (SelectedIndex != i) { // alpha = Alpha.MoveOrUp; page.ForeColor = Color.CornflowerBlue; // sb = new SolidBrush(color); // pen = new Pen(color); sb.Color = Share.DisabelBackColor; g.FillPath(sb, gp); //画边框 g.DrawPath(pen, gp); //画背景 gp.Dispose(); // sb.Dispose(); //渲染选项卡文字 tabRect.X += 10; //让文字往右偏移10个像素.因为谷歌的tab 是个梯形的 tabRect.Width -= 20; TextRenderer.DrawText(g, page.Text, page.Font, tabRect, page.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis | TextFormatFlags.HorizontalCenter); } else { selectPage = page; selectgp = gp; selecttabRect = tabRect; } if (i == TabCount - 1) { selectPage.ForeColor = Color.Orange; // alpha = Alpha.a200; // sb = new SolidBrush(color); // pen = new Pen(color); sb.Color = Share.BackColor; g.FillPath(sb, selectgp); //画背景 g.DrawPath(pen, selectgp); //画边框 selectgp.Dispose(); // sb.Dispose(); //画叉叉 ,叉叉是在最上层的, 所以要最后画 Bitmap clsBitmap; if (closeState == MouseState.press) { clsBitmap = Properties.Resources.icon_tbclose_press; } else if (closeState == MouseState.move) { clsBitmap = Properties.Resources.icon_tbclose_hover; } else { clsBitmap = Properties.Resources.icon_tbclose_normal; } g.DrawImage(clsBitmap, GetCloseRect(selecttabRect)); clsBitmap.Dispose(); //渲染选项卡文字 selecttabRect.X += 10; //让文字往右偏移10个像素.因为谷歌的tab 是个梯形的 selecttabRect.Width -= 20; TextRenderer.DrawText(g, selectPage.Text, selectPage.Font, selecttabRect, selectPage.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis | TextFormatFlags.HorizontalCenter); var rect = new Rectangle(GetTabRect(0).X + 2, GetTabRect(0).Bottom + 2, TabPages[0].DisplayRectangle.Width + 6, TabPages[0].DisplayRectangle.Height + 6); var path = GetPath(rect, 5); pen.Color = Share.BorderColor; g.DrawPath(pen, path); //画页面边框 path.Dispose(); } } sb.Dispose(); pen.Dispose(); } }
/// <summary> /// Draws the item's text (overloaded) - useful only when View != View.Details - takes a TextFormatFlags argument. /// </summary> public void DrawText(TextFormatFlags flags) { TextRenderer.DrawText(Graphics, Item.Text, Item.Font, UpdateBounds(Bounds, drawText: true), Item.ForeColor, flags); }
private void CustomDrawNode(DrawTreeNodeEventArgs e) { const int int_CheckBoxWidth = 14; const int int_RowWidth = 9; const int int_ExpandeboxWidth = 15; //if (this.CheckBoxes) //{ TreeNode node = e.Node; int currt_X = node.Bounds.X; Pen penText = new Pen(Color.Red, 1); Pen penDotline = new Pen(Color.Gray, 1); //文字部分 Font font = new Font("SimSun", 12F, GraphicsUnit.Pixel); Brush brush = new SolidBrush(node.TreeView.BackColor); byte[] sarr = System.Text.Encoding.Default.GetBytes(node.Text); int textlength = sarr.Length; Rectangle rcText = new Rectangle(node.Bounds.Left + 2, e.Bounds.Top + 2, textlength * 6 + 5, e.Bounds.Height - 3); if (e.Node.TreeView.SelectedNode == node) { if (e.State == TreeNodeStates.Selected) { brush = new SolidBrush(Color.FromArgb(236, 233, 216)); e.Graphics.FillRectangle(brush, new Rectangle(rcText.Location, new Size(textlength * 6 + 5, rcText.Height))); TextRenderer.DrawText(e.Graphics, node.Text, font, rcText, Color.Red, Color.Transparent, TextFormatFlags.Left); } else { brush = new SolidBrush(Color.FromArgb(10, 36, 106)); e.Graphics.FillRectangle(brush, new Rectangle(rcText.Location, new Size(textlength * 6 + 5, rcText.Height))); Pen penDotline2 = new Pen(Color.Black, 1); penDotline2.DashStyle = DashStyle.Dot; e.Graphics.DrawRectangle(penDotline2, new Rectangle(node.Bounds.Left + 1, e.Bounds.Top + 1, textlength * 6 + 6, e.Bounds.Height - 2)); TextRenderer.DrawText(e.Graphics, node.Text, font, rcText, Color.White, Color.Transparent, TextFormatFlags.Left); } } else { TextRenderer.DrawText(e.Graphics, node.Text, font, rcText, Color.Red, Color.Transparent, TextFormatFlags.Left); } //画image if (this.ImageList != null && this.ImageList.Images.Count != 0) { //画image Rectangle imagebox = new Rectangle(currt_X - 3 - 16, node.Bounds.Y + 1, 16, 14); int index = node.ImageIndex; string imagekey = node.ImageKey; if (imagekey != "" && this.ImageList.Images.ContainsKey(imagekey)) { e.Graphics.DrawImage(this.ImageList.Images[imagekey], imagebox); } else { if (index < 0) { index = 0; } else if (index > this.ImageList.Images.Count - 1) { index = 0; } e.Graphics.DrawImage(this.ImageList.Images[index], imagebox); } e.Graphics.DrawRectangle(new Pen(Color.Black, 1), imagebox); currt_X -= 19; } //画checkbox if (this.CheckBoxes) { Rectangle rcCheck = new Rectangle(node.Bounds.X - int_CheckBoxWidth, node.Bounds.Y, int_CheckBoxWidth, int_CheckBoxWidth); using (Bitmap bmp = new Bitmap(node.Checked ?Properties.Resources.check : Properties.Resources.expan1)) { e.Graphics.DrawImage(bmp, rcCheck); //DrawUtil.DrawBitmapWithTransparent(e.Graphics, bmp, rcCheck, 0, bmp.Height / 4, bmp.Width, bmp.Height / 4, Color.Magenta, true); } currt_X = currt_X - int_CheckBoxWidth - 3; } //画线 if (node.TreeView.ShowLines) { penDotline.DashStyle = DashStyle.Dot; e.Graphics.DrawLine(penDotline, currt_X, node.Bounds.Top + node.Bounds.Height / 2 + 1, currt_X - int_RowWidth, node.Bounds.Top + node.Bounds.Height / 2 + 1); if (node.NextNode != null) { if (node.TreeView.Nodes[0] == node) { e.Graphics.DrawLine(penDotline, currt_X - 9, node.Bounds.Bottom, currt_X - 9, node.Bounds.Bottom - node.Bounds.Height / 2 - 1); } else { e.Graphics.DrawLine(penDotline, currt_X - 9, node.Bounds.Top, currt_X - 9, node.Bounds.Bottom); } } else if (node.TreeView.Nodes[0] == node) { } else { e.Graphics.DrawLine(penDotline, currt_X - 9, node.Bounds.Top, currt_X - 9, node.Bounds.Top + node.Bounds.Height / 2 + 2); } int level = node.Level; TreeNode parentnode = new TreeNode(); if (level != 0) { parentnode = node.Parent; while (level > 0) { if (parentnode.NextNode != null) { e.Graphics.DrawLine(penDotline, parentnode.Bounds.X - (node.Bounds.X - currt_X + 9), parentnode.Bounds.Bottom, parentnode.Bounds.X - (node.Bounds.X - currt_X + 9), parentnode.NextNode.Bounds.Top); } parentnode = parentnode.Parent; level--; } } currt_X = currt_X - 9; } //画+ -框 if (e.Node.Nodes.Count > 0) { Rectangle rcExpandebox = new Rectangle(currt_X - int_ExpandeboxWidth / 2, node.Bounds.Y + int_ExpandeboxWidth / 2, int_ExpandeboxWidth, int_ExpandeboxWidth); //e.Graphics.FillRectangle(new SolidBrush(node.TreeView.BackColor), new Rectangle(rcExpandebox.X, rcExpandebox.Y, 9, 9)); //penDotline.DashStyle = DashStyle.Solid; //e.Graphics.DrawRectangle(penDotline, rcExpandebox); //e.Graphics.DrawLine(penText, rcExpandebox.Left + 2, rcExpandebox.Top + 4, rcExpandebox.Right - 2, rcExpandebox.Top + 4); //if (!node.IsExpanded) //{ // e.Graphics.DrawLine(penText, rcExpandebox.Left + 4, rcExpandebox.Top + 2, rcExpandebox.Left + 4, rcExpandebox.Top + 6); //} if (!node.IsExpanded) { e.Graphics.DrawImage(Properties.Resources.unexpan, rcExpandebox); } else { e.Graphics.DrawImage(Properties.Resources.expan, rcExpandebox); } } //} //else { e.DrawDefault = true; } }
public static void DrawCheckBox(Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, CheckBoxState state) { Rectangle bounds = new Rectangle(glyphLocation, GetGlyphSize(g, state)); if (Application.RenderWithVisualStyles || always_use_visual_styles == true) { VisualStyleRenderer vsr = GetCheckBoxRenderer(state); vsr.DrawBackground(g, bounds); if (image != null) { vsr.DrawImage(g, imageBounds, image); } if (focused) { ControlPaint.DrawFocusRectangle(g, textBounds); } if (checkBoxText != String.Empty) { if (state == CheckBoxState.CheckedDisabled || state == CheckBoxState.MixedDisabled || state == CheckBoxState.UncheckedDisabled) { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.GrayText, flags); } else { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.ControlText, flags); } } } else { switch (state) { case CheckBoxState.CheckedDisabled: case CheckBoxState.MixedDisabled: case CheckBoxState.MixedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Inactive | ButtonState.Checked); break; case CheckBoxState.CheckedHot: case CheckBoxState.CheckedNormal: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Checked); break; case CheckBoxState.CheckedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Pushed | ButtonState.Checked); break; case CheckBoxState.MixedHot: case CheckBoxState.MixedNormal: ControlPaint.DrawMixedCheckBox(g, bounds, ButtonState.Checked); break; case CheckBoxState.UncheckedDisabled: case CheckBoxState.UncheckedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Inactive); break; case CheckBoxState.UncheckedHot: case CheckBoxState.UncheckedNormal: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Normal); break; } if (image != null) { g.DrawImage(image, imageBounds); } if (focused) { ControlPaint.DrawFocusRectangle(g, textBounds); } if (checkBoxText != String.Empty) { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.ControlText, flags); } } }
/// <summary> /// Draws the text (overloaded) - takes a TextFormatFlags argument. /// </summary> public void DrawText(TextFormatFlags flags) { TextRenderer.DrawText(Graphics, ToolTipText, Font, Bounds, _foreColor, flags); }
// PaintPrivate is used in three places that need to duplicate the paint code: // 1. DataGridViewCell::Paint method // 2. DataGridViewCell::GetContentBounds // 3. DataGridViewCell::GetErrorIconBounds // // if computeContentBounds is true then PaintPrivate returns the contentBounds // else if computeErrorIconBounds is true then PaintPrivate returns the errorIconBounds // else it returns Rectangle.Empty; private Rectangle PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle != null); // If computeContentBounds == TRUE then resultBounds will be the contentBounds. // If computeErrorIconBounds == TRUE then resultBounds will be the error icon bounds. // Else resultBounds will be Rectangle.Empty; Rectangle resultBounds = Rectangle.Empty; if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle borderWidths = BorderWidths(advancedBorderStyle); Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; SolidBrush br; Point ptCurrentCell = this.DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellEdited = cellCurrent && this.DataGridView.EditingControl != null; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; if (DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected && !cellEdited) { br = this.DataGridView.GetCachedBrush(cellStyle.SelectionBackColor); } else { br = this.DataGridView.GetCachedBrush(cellStyle.BackColor); } if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255 && valBounds.Width > 0 && valBounds.Height > 0) { graphics.FillRectangle(br, valBounds); } if (cellStyle.Padding != Padding.Empty) { if (this.DataGridView.RightToLeftInternal) { valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } valBounds.Width -= cellStyle.Padding.Horizontal; valBounds.Height -= cellStyle.Padding.Vertical; } if (paint && cellCurrent && !cellEdited) { // Draw focus rectangle if (DataGridViewCell.PaintFocus(paintParts) && this.DataGridView.ShowFocusCues && this.DataGridView.Focused && valBounds.Width > 0 && valBounds.Height > 0) { ControlPaint.DrawFocusRectangle(graphics, valBounds, Color.Empty, br.Color); } } Rectangle errorBounds = valBounds; string formattedString = formattedValue as string; if (formattedString != null && ((paint && !cellEdited) || computeContentBounds)) { // Font independent margins int verticalTextMarginTop = cellStyle.WrapMode == DataGridViewTriState.True ? DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithWrapping : DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithoutWrapping; valBounds.Offset(DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft, verticalTextMarginTop); valBounds.Width -= DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft + DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginRight; valBounds.Height -= verticalTextMarginTop + DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginBottom; if (valBounds.Width > 0 && valBounds.Height > 0) { TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(this.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); if (paint) { if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(graphics, formattedString, cellStyle.Font, valBounds, cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor, flags); } } else { resultBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedString, flags, cellStyle); } } } else if (computeErrorIconBounds) { if (!String.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } } else { Debug.Assert(cellEdited || formattedString == null); Debug.Assert(paint || computeContentBounds); } if (this.DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(graphics, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } return(resultBounds); }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { Rectangle empty; Point currentCellAddress = base.DataGridView.CurrentCellAddress; bool flag = (elementState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None; bool flag2 = (currentCellAddress.X == base.ColumnIndex) && (currentCellAddress.Y == rowIndex); string text = formattedValue as string; SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag) ? cellStyle.SelectionBackColor : cellStyle.BackColor); SolidBrush brush2 = base.DataGridView.GetCachedBrush(flag ? cellStyle.SelectionForeColor : cellStyle.ForeColor); if (paint && DataGridViewCell.PaintBorder(paintParts)) { this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle rect = cellBounds; Rectangle rectangle3 = this.BorderWidths(advancedBorderStyle); rect.Offset(rectangle3.X, rectangle3.Y); rect.Width -= rectangle3.Right; rect.Height -= rectangle3.Bottom; if ((rect.Height <= 0) || (rect.Width <= 0)) { return(Rectangle.Empty); } if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, rect); } if (cellStyle.Padding != Padding.Empty) { if (base.DataGridView.RightToLeftInternal) { rect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { rect.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } rect.Width -= cellStyle.Padding.Horizontal; rect.Height -= cellStyle.Padding.Vertical; } Rectangle cellValueBounds = rect; if (((rect.Height <= 0) || (rect.Width <= 0)) || (!paint && !computeContentBounds)) { if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { empty = base.ComputeErrorIconBounds(cellValueBounds); } else { empty = Rectangle.Empty; } } else { empty = Rectangle.Empty; } goto Label_06AD; } if ((this.FlatStyle == System.Windows.Forms.FlatStyle.Standard) || (this.FlatStyle == System.Windows.Forms.FlatStyle.System)) { if (base.DataGridView.ApplyVisualStylesToInnerCells) { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { PushButtonState normal = PushButtonState.Normal; if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { normal = PushButtonState.Pressed; } else if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { normal = PushButtonState.Hot; } if ((DataGridViewCell.PaintFocus(paintParts) && flag2) && (base.DataGridView.ShowFocusCues && base.DataGridView.Focused)) { normal |= PushButtonState.Default; } DataGridViewButtonCellRenderer.DrawButton(g, rect, (int)normal); } empty = rect; rect = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetBackgroundContentRectangle(g, rect); } else { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { ControlPaint.DrawBorder(g, rect, SystemColors.Control, (this.ButtonState == System.Windows.Forms.ButtonState.Normal) ? ButtonBorderStyle.Outset : ButtonBorderStyle.Inset); } empty = rect; rect.Inflate(-SystemInformation.Border3DSize.Width, -SystemInformation.Border3DSize.Height); } goto Label_06AD; } if (this.FlatStyle != System.Windows.Forms.FlatStyle.Flat) { rect.Inflate(-1, -1); if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { ButtonBaseAdapter.ColorData data2 = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, data2.options.highContrast ? data2.windowText : data2.windowFrame, true); ControlPaint.DrawBorder(g, rect, data2.options.highContrast ? data2.windowText : data2.buttonShadow, ButtonBorderStyle.Solid); } else if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, colors.options.highContrast ? colors.windowText : colors.buttonShadow, false); ButtonBaseAdapter.Draw3DLiteBorder(g, rect, colors, true); } else { ButtonBaseAdapter.ColorData data4 = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, data4.options.highContrast ? data4.windowText : data4.buttonShadow, false); ButtonBaseAdapter.DrawFlatBorder(g, rect, data4.options.highContrast ? data4.windowText : data4.buttonShadow); } } empty = rect; goto Label_06AD; } rect.Inflate(-1, -1); if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { ButtonBaseAdapter.DrawDefaultBorder(g, rect, brush2.Color, true); if (cachedBrush.Color.A == 0xff) { if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { ButtonBaseAdapter.ColorData data = ButtonBaseAdapter.PaintFlatRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); IntPtr hdc = g.GetHdc(); try { using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc)) { WindowsBrush brush3; if (data.options.highContrast) { brush3 = new WindowsSolidBrush(graphics.DeviceContext, data.buttonShadow); } else { brush3 = new WindowsSolidBrush(graphics.DeviceContext, data.lowHighlight); } try { ButtonBaseAdapter.PaintButtonBackground(graphics, rect, brush3); } finally { brush3.Dispose(); } } goto Label_04CF; } finally { g.ReleaseHdc(); } } if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { IntPtr hDc = g.GetHdc(); try { using (WindowsGraphics graphics2 = WindowsGraphics.FromHdc(hDc)) { Color controlDark = SystemColors.ControlDark; using (WindowsBrush brush4 = new WindowsSolidBrush(graphics2.DeviceContext, controlDark)) { ButtonBaseAdapter.PaintButtonBackground(graphics2, rect, brush4); } } } finally { g.ReleaseHdc(); } } } } Label_04CF: empty = rect; Label_06AD: if (((paint && DataGridViewCell.PaintFocus(paintParts)) && (flag2 && base.DataGridView.ShowFocusCues)) && ((base.DataGridView.Focused && (rect.Width > ((2 * SystemInformation.Border3DSize.Width) + 1))) && (rect.Height > ((2 * SystemInformation.Border3DSize.Height) + 1)))) { if ((this.FlatStyle == System.Windows.Forms.FlatStyle.System) || (this.FlatStyle == System.Windows.Forms.FlatStyle.Standard)) { ControlPaint.DrawFocusRectangle(g, Rectangle.Inflate(rect, -1, -1), Color.Empty, SystemColors.Control); } else if (this.FlatStyle == System.Windows.Forms.FlatStyle.Flat) { if (((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) || ((base.DataGridView.CurrentCellAddress.Y == rowIndex) && (base.DataGridView.CurrentCellAddress.X == base.ColumnIndex))) { ButtonBaseAdapter.ColorData data5 = ButtonBaseAdapter.PaintFlatRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); string str2 = (text != null) ? text : string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonFlatAdapter.PaintFlatLayout(g, true, SystemInformation.HighContrast, 1, rect, Padding.Empty, false, cellStyle.Font, str2, base.DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), base.DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData data6 = options.Layout(); ButtonBaseAdapter.DrawFlatFocus(g, data6.focus, data5.options.highContrast ? data5.windowText : data5.constrastButtonShadow); } } else if (((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) || ((base.DataGridView.CurrentCellAddress.Y == rowIndex) && (base.DataGridView.CurrentCellAddress.X == base.ColumnIndex))) { bool up = this.ButtonState == System.Windows.Forms.ButtonState.Normal; string str3 = (text != null) ? text : string.Empty; ButtonBaseAdapter.LayoutOptions options2 = ButtonPopupAdapter.PaintPopupLayout(g, up, SystemInformation.HighContrast ? 2 : 1, rect, Padding.Empty, false, cellStyle.Font, str3, base.DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), base.DataGridView.RightToLeft); options2.everettButtonCompat = false; ButtonBaseAdapter.LayoutData data7 = options2.Layout(); ControlPaint.DrawFocusRectangle(g, data7.focus, cellStyle.ForeColor, cellStyle.BackColor); } } if (((text != null) && paint) && DataGridViewCell.PaintContentForeground(paintParts)) { rect.Offset(2, 1); rect.Width -= 4; rect.Height -= 2; if ((((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) && (this.FlatStyle != System.Windows.Forms.FlatStyle.Flat)) && (this.FlatStyle != System.Windows.Forms.FlatStyle.Popup)) { rect.Offset(1, 1); rect.Width--; rect.Height--; } if ((rect.Width > 0) && (rect.Height > 0)) { Color color; if (base.DataGridView.ApplyVisualStylesToInnerCells && ((this.FlatStyle == System.Windows.Forms.FlatStyle.System) || (this.FlatStyle == System.Windows.Forms.FlatStyle.Standard))) { color = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetColor(ColorProperty.TextColor); } else { color = brush2.Color; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); TextRenderer.DrawText(g, text, cellStyle.Font, rect, color, flags); } } if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts)) { base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, cellValueBounds, errorText); } return(empty); }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { if (paint && DataGridViewCell.PaintBorder(paintParts)) { this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle empty = Rectangle.Empty; Rectangle rectangle2 = this.BorderWidths(advancedBorderStyle); Rectangle rect = cellBounds; rect.Offset(rectangle2.X, rectangle2.Y); rect.Width -= rectangle2.Right; rect.Height -= rectangle2.Bottom; Point currentCellAddress = base.DataGridView.CurrentCellAddress; bool flag = (currentCellAddress.X == base.ColumnIndex) && (currentCellAddress.Y == rowIndex); bool flag2 = (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None; SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag2) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, rect); } if (cellStyle.Padding != Padding.Empty) { if (base.DataGridView.RightToLeftInternal) { rect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { rect.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } rect.Width -= cellStyle.Padding.Horizontal; rect.Height -= cellStyle.Padding.Vertical; } Rectangle rectangle = rect; string text = formattedValue as string; if ((text != null) && (paint || computeContentBounds)) { rect.Offset(1, 1); rect.Width -= 3; rect.Height -= 2; if ((cellStyle.Alignment & anyBottom) != DataGridViewContentAlignment.NotSet) { rect.Height--; } Font linkFont = null; Font hoverLinkFont = null; LinkUtilities.EnsureLinkFonts(cellStyle.Font, this.LinkBehavior, ref linkFont, ref hoverLinkFont); TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); if (paint) { if ((rect.Width > 0) && (rect.Height > 0)) { Color activeLinkColor; if ((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) { Rectangle rectangle5 = DataGridViewUtilities.GetTextBounds(rect, text, flags, cellStyle, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont); if ((cellStyle.Alignment & anyLeft) != DataGridViewContentAlignment.NotSet) { rectangle5.X--; rectangle5.Width++; } else if ((cellStyle.Alignment & anyRight) != DataGridViewContentAlignment.NotSet) { rectangle5.X++; rectangle5.Width++; } rectangle5.Height += 2; ControlPaint.DrawFocusRectangle(g, rectangle5, Color.Empty, cachedBrush.Color); } if ((this.LinkState & System.Windows.Forms.LinkState.Active) == System.Windows.Forms.LinkState.Active) { activeLinkColor = this.ActiveLinkColor; } else if (this.LinkVisited) { activeLinkColor = this.VisitedLinkColor; } else { activeLinkColor = this.LinkColor; } if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != TextFormatFlags.Default) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(g, text, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont, rect, activeLinkColor, flags); } } else if (((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) && ((rectangle.Width > 0) && (rectangle.Height > 0))) { ControlPaint.DrawFocusRectangle(g, rectangle, Color.Empty, cachedBrush.Color); } } else { empty = DataGridViewUtilities.GetTextBounds(rect, text, flags, cellStyle, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont); } linkFont.Dispose(); hoverLinkFont.Dispose(); } else if (paint || computeContentBounds) { if (((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) && ((paint && (rect.Width > 0)) && (rect.Height > 0))) { ControlPaint.DrawFocusRectangle(g, rect, Color.Empty, cachedBrush.Color); } } else if (computeErrorIconBounds && !string.IsNullOrEmpty(errorText)) { empty = base.ComputeErrorIconBounds(rectangle); } if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts)) { base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, rectangle, errorText); } return(empty); }
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // The button cell is disabled, so paint the border, // background, and disabled button for the cell. if (!this.enabledValue) { // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) { SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor); graphics.FillRectangle(cellBackground, cellBounds); cellBackground.Dispose(); } // Draw the cell borders, if specified. if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border) { PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } // Calculate the area in which to draw the button. Rectangle buttonArea = cellBounds; Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle); buttonArea.X += buttonAdjustment.X; buttonArea.Y += buttonAdjustment.Y; buttonArea.Height -= buttonAdjustment.Height; buttonArea.Width -= buttonAdjustment.Width; // Draw the disabled button. ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled); // Draw the disabled button text. if (this.FormattedValue is String) { TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, Color.Gray); } } else { // The button cell is enabled, so let the base class // handle the painting. base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } }
/// <summary> /// Paints the control. /// </summary> /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param> protected virtual void PaintControl(PaintEventArgs e) { var cbi = NativeMethods.COMBOBOXINFO.FromComboBox(this); var itemText = SelectedIndex >= 0 ? GetItemText(SelectedItem) : string.Empty; var state = Enabled ? currentState : ComboBoxState.Disabled; Rectangle tr = cbi.rcItem; /*Rectangle tr = this.ClientRectangle; * tr.Width -= (SystemInformation.VerticalScrollBarWidth + 2); * tr.Inflate(0, -2); * tr.Offset(1, 0);*/ Rectangle br = cbi.rcButton; var vsSuccess = false; if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles) { /*Rectangle r = Rectangle.Inflate(this.ClientRectangle, 1, 1); * if (this.DropDownStyle != ComboBoxStyle.DropDownList) * { * e.Graphics.Clear(this.BackColor); * ComboBoxRenderer.DrawTextBox(e.Graphics, r, itemText, this.Font, tr, tff, state); * ComboBoxRenderer.DrawDropDownButton(e.Graphics, br, state); * } * else*/ { try { var vr = new VisualStyleRenderer("Combobox", DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state); vr.DrawParentBackground(e.Graphics, ClientRectangle, this); vr.DrawBackground(e.Graphics, ClientRectangle); if (DropDownStyle != ComboBoxStyle.DropDownList) { br.Inflate(1, 1); } var cr = DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br; vr.SetParameters("Combobox", 7, (int)(br.Contains(PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal)); vr.DrawBackground(e.Graphics, br, cr); if (Focused && State != ComboBoxState.Pressed) { var sz = TextRenderer.MeasureText(e.Graphics, "Wg", Font, tr.Size, TextFormatFlags.Default); var fr = Rectangle.Inflate(tr, 0, (sz.Height - tr.Height) / 2 + 1); ControlPaint.DrawFocusRectangle(e.Graphics, fr); } var fgc = Enabled ? ForeColor : SystemColors.GrayText; TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); vsSuccess = true; } catch { } } } if (!vsSuccess) { Diagnostics.Debug.WriteLine($"CR:{ClientRectangle};ClR:{e.ClipRectangle};Foc:{Focused};St:{state};Tx:{itemText}"); var focusedNotPressed = Focused && state != ComboBoxState.Pressed; var bgc = Enabled ? (focusedNotPressed ? SystemColors.Highlight : BackColor) : SystemColors.Control; var fgc = Enabled ? (focusedNotPressed ? SystemColors.HighlightText : ForeColor) : SystemColors.GrayText; e.Graphics.Clear(bgc); ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, Border3DStyle.Sunken); ControlPaint.DrawComboButton(e.Graphics, br, Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive); tr = new Rectangle(tr.X + 3, tr.Y + 1, tr.Width - 4, tr.Height - 2); TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); if (focusedNotPressed) { var fr = Rectangle.Inflate(cbi.rcItem, -1, -1); fr.Height++; fr.Width++; ControlPaint.DrawFocusRectangle(e.Graphics, fr, fgc, bgc); e.Graphics.DrawRectangle(SystemPens.Window, cbi.rcItem); } } }
public static void DrawGroupBox(Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state) { Size font_size = TextRenderer.MeasureText(groupBoxText, font); if (Application.RenderWithVisualStyles || always_use_visual_styles == true) { VisualStyleRenderer vsr; Rectangle new_bounds; switch (state) { case GroupBoxState.Normal: default: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1); break; case GroupBoxState.Disabled: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Disabled); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2); break; } if (groupBoxText == String.Empty) { vsr.DrawBackground(g, bounds); } else { vsr.DrawBackgroundExcludingArea(g, new_bounds, new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height)); } if (textColor == Color.Empty) { textColor = vsr.GetColor(ColorProperty.TextColor); } if (groupBoxText != String.Empty) { TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } else { // MS has a pretty big bug when rendering the non-visual styles group box. Instead of using the height // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different // places. Rather than emulate this bug, we do it correctly. After googling for a while, I don't think // anyone has ever actually used this class for anything, so it should be fine. :) Rectangle new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2), bounds.Width, bounds.Height - (int)(font_size.Height / 2)); // Don't paint over the background where we are going to put the text Region old_clip = g.Clip; g.SetClip(new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude); ControlPaint.DrawBorder3D(g, new_bounds, Border3DStyle.Etched); g.Clip = old_clip; if (groupBoxText != String.Empty) { if (textColor == Color.Empty) { textColor = state == GroupBoxState.Normal ? SystemColors.ControlText : SystemColors.GrayText; } TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } }
private static void DrawThemedGroupBoxWithText( IDeviceContext deviceContext, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state) { InitializeRenderer((int)state); // Calculate text area, and render text inside it Rectangle textBounds = bounds; textBounds.Width -= 2 * BoxHeaderWidth; Size measuredBounds = TextRenderer.MeasureText( deviceContext, groupBoxText, font, new Size(textBounds.Width, textBounds.Height), flags); textBounds.Width = measuredBounds.Width; textBounds.Height = measuredBounds.Height; if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right) { // +1 to account for the margin built in the MeasureText result textBounds.X = bounds.Right - textBounds.Width - BoxHeaderWidth + 1; } else { // -1 to account for the margin built in the MeasureText result textBounds.X += BoxHeaderWidth - 1; } TextRenderer.DrawText(deviceContext, groupBoxText, font, textBounds, textColor, flags); // Calculate area for background box Rectangle boxBounds = bounds; boxBounds.Y += font.Height / 2; boxBounds.Height -= font.Height / 2; // Break box into three segments, that don't overlap the text area Rectangle clipLeft = boxBounds; Rectangle clipMiddle = boxBounds; Rectangle clipRight = boxBounds; clipLeft.Width = BoxHeaderWidth; clipMiddle.Width = Math.Max(0, textBounds.Width - 3); // -3 to account for the margin built in the MeasureText result if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right) { clipLeft.X = boxBounds.Right - BoxHeaderWidth; clipMiddle.X = clipLeft.Left - clipMiddle.Width; clipRight.Width = clipMiddle.X - boxBounds.X; } else { clipMiddle.X = clipLeft.Right; clipRight.X = clipMiddle.Right; clipRight.Width = boxBounds.Right - clipRight.X; } clipMiddle.Y = textBounds.Bottom; clipMiddle.Height -= (textBounds.Bottom - boxBounds.Top); Debug.Assert(textBounds.Y <= boxBounds.Y, "if text below box, need to render area of box above text"); // Render clipped portion of background in each segment t_visualStyleRenderer.DrawBackground(deviceContext, boxBounds, clipLeft); t_visualStyleRenderer.DrawBackground(deviceContext, boxBounds, clipMiddle); t_visualStyleRenderer.DrawBackground(deviceContext, boxBounds, clipRight); }
public void DrawText(TextFormatFlags flags) { TextRenderer.DrawText(graphics, item.Text, item.Font, bounds, item.ForeColor, flags); }
private static void DrawUnthemedGroupBoxWithText( IDeviceContext deviceContext, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags) { // Calculate text area, and render text inside it Rectangle textBounds = bounds; textBounds.Width -= TextOffset; Size measuredBounds = TextRenderer.MeasureText( deviceContext, groupBoxText, font, new Size(textBounds.Width, textBounds.Height), flags); textBounds.Width = measuredBounds.Width; textBounds.Height = measuredBounds.Height; if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right) { textBounds.X = bounds.Right - textBounds.Width - TextOffset; } else { textBounds.X += TextOffset; } TextRenderer.DrawText(deviceContext, groupBoxText, font, textBounds, textColor, flags); // Pad text area to stop background from touching text if (textBounds.Width > 0) { textBounds.Inflate(2, 0); } int boxTop = bounds.Top + font.Height / 2; using var hdc = new DeviceContextHdcScope(deviceContext); ReadOnlySpan <int> darkLines = stackalloc int[] { bounds.Left, boxTop - 1, bounds.Left, bounds.Height - 2, // Left bounds.Left, bounds.Height - 2, bounds.Width - 1, bounds.Height - 2, // Right bounds.Left, boxTop - 1, textBounds.X - 3, boxTop - 1, // Top-left textBounds.X + textBounds.Width + 2, boxTop - 1, bounds.Width - 2, boxTop - 1, // Top-right bounds.Width - 2, boxTop - 1, bounds.Width - 2, bounds.Height - 2 // Right }; using var hpenDark = new Gdi32.CreatePenScope(SystemColors.ControlDark); hdc.DrawLines(hpenDark, darkLines); ReadOnlySpan <int> lightLines = stackalloc int[] { bounds.Left + 1, boxTop, bounds.Left + 1, bounds.Height - 1, // Left bounds.Left, bounds.Height - 1, bounds.Width, bounds.Height - 1, // Right bounds.Left + 1, boxTop, textBounds.X - 2, boxTop, // Top-left textBounds.X + textBounds.Width + 1, boxTop, bounds.Width - 1, boxTop, // Top-right bounds.Width - 1, boxTop, bounds.Width - 1, bounds.Height - 1 // Right }; using var hpenLight = new Gdi32.CreatePenScope(SystemColors.ControlLight); hdc.DrawLines(hpenLight, lightLines); }
/// <include file='doc\DrawListViewItemEventArgs.uex' path='docs/doc[@for="DrawListViewItemEventArgs.DrawText1"]/*' /> /// <devdoc> /// Draws the item's text (overloaded) - useful only when View != View.Details - takes a TextFormatFlags argument. /// </devdoc> public void DrawText(TextFormatFlags flags) { TextRenderer.DrawText(graphics, item.Text, item.Font, UpdateBounds(bounds, true /*drawText*/), item.ForeColor, flags); }
private Rectangle PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle is not null); // If computeContentBounds == TRUE then resultBounds will be the contentBounds. // If computeErrorIconBounds == TRUE then resultBounds will be the error icon bounds. // Else resultBounds will be Rectangle.Empty; Rectangle resultBounds = Rectangle.Empty; Rectangle valBounds = cellBounds; Rectangle borderWidths = BorderWidths(advancedBorderStyle); valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; if (paint && PaintBackground(paintParts)) { if (DataGridView.ApplyVisualStylesToHeaderCells) { // Theming int state = (int)HeaderItemState.Normal; if (ButtonState != ButtonState.Normal) { Debug.Assert(ButtonState == ButtonState.Pushed); state = (int)HeaderItemState.Pressed; } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex) { state = (int)HeaderItemState.Hot; } valBounds.Inflate(16, 16); DataGridViewTopLeftHeaderCellRenderer.DrawHeader(graphics, valBounds, state); valBounds.Inflate(-16, -16); } else { Color brushColor = PaintSelectionBackground(paintParts) && cellSelected ? cellStyle.SelectionBackColor : cellStyle.BackColor; if (!brushColor.HasTransparency()) { using var brush = brushColor.GetCachedSolidBrushScope(); graphics.FillRectangle(brush, valBounds); } } } if (paint && PaintBorder(paintParts)) { PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } if (cellStyle.Padding != Padding.Empty) { if (DataGridView.RightToLeftInternal) { valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } valBounds.Width -= cellStyle.Padding.Horizontal; valBounds.Height -= cellStyle.Padding.Vertical; } Rectangle errorBounds = valBounds; string formattedValueStr = formattedValue as string; // Font independent margins valBounds.Offset(DATAGRIDVIEWTOPLEFTHEADERCELL_horizontalTextMarginLeft, DATAGRIDVIEWTOPLEFTHEADERCELL_verticalTextMargin); valBounds.Width -= DATAGRIDVIEWTOPLEFTHEADERCELL_horizontalTextMarginLeft + DATAGRIDVIEWTOPLEFTHEADERCELL_horizontalTextMarginRight; valBounds.Height -= 2 * DATAGRIDVIEWTOPLEFTHEADERCELL_verticalTextMargin; if (valBounds.Width > 0 && valBounds.Height > 0 && !string.IsNullOrEmpty(formattedValueStr) && (paint || computeContentBounds)) { Color textColor; if (DataGridView.ApplyVisualStylesToHeaderCells) { textColor = DataGridViewTopLeftHeaderCellRenderer.VisualStyleRenderer.GetColor(ColorProperty.TextColor); } else { textColor = cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); if (paint) { if (PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(graphics, formattedValueStr, cellStyle.Font, valBounds, textColor, flags); } } else { Debug.Assert(computeContentBounds); resultBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedValueStr, flags, cellStyle); } } else if (computeErrorIconBounds && !string.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } if (DataGridView.ShowCellErrors && paint && PaintErrorIcon(paintParts)) { PaintErrorIcon(graphics, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } return(resultBounds); }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle != null); if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle resultBounds = Rectangle.Empty; Rectangle borderWidths = BorderWidths(advancedBorderStyle); Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; Point ptCurrentCell = DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; SolidBrush br = DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } if (cellStyle.Padding != Padding.Empty) { if (DataGridView.RightToLeftInternal) { valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } valBounds.Width -= cellStyle.Padding.Horizontal; valBounds.Height -= cellStyle.Padding.Vertical; } Rectangle errorBounds = valBounds; if (formattedValue is string formattedValueStr && (paint || computeContentBounds)) { // Font independent margins valBounds.Offset(DATAGRIDVIEWLINKCELL_horizontalTextMarginLeft, DATAGRIDVIEWLINKCELL_verticalTextMarginTop); valBounds.Width -= DATAGRIDVIEWLINKCELL_horizontalTextMarginLeft + DATAGRIDVIEWLINKCELL_horizontalTextMarginRight; valBounds.Height -= DATAGRIDVIEWLINKCELL_verticalTextMarginTop + DATAGRIDVIEWLINKCELL_verticalTextMarginBottom; if ((cellStyle.Alignment & AnyBottom) != 0) { valBounds.Height -= DATAGRIDVIEWLINKCELL_verticalTextMarginBottom; } Font linkFont = null; Font hoverFont = null; LinkUtilities.EnsureLinkFonts(cellStyle.Font, LinkBehavior, ref linkFont, ref hoverFont); TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); // paint the focus rectangle around the link if (paint) { if (valBounds.Width > 0 && valBounds.Height > 0) { if (cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts)) { Rectangle focusBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedValueStr, flags, cellStyle, LinkState == LinkState.Hover ? hoverFont : linkFont); if ((cellStyle.Alignment & AnyLeft) != 0) { focusBounds.X--; focusBounds.Width++; } else if ((cellStyle.Alignment & AnyRight) != 0) { focusBounds.X++; focusBounds.Width++; } focusBounds.Height += 2; ControlPaint.DrawFocusRectangle(g, focusBounds, Color.Empty, br.Color); } Color linkColor; if ((LinkState & LinkState.Active) == LinkState.Active) { linkColor = ActiveLinkColor; } else if (LinkVisited) { linkColor = VisitedLinkColor; } else { linkColor = LinkColor; } if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(g, formattedValueStr, LinkState == LinkState.Hover ? hoverFont : linkFont, valBounds, linkColor, flags); } } else if (cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts) && errorBounds.Width > 0 && errorBounds.Height > 0) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, errorBounds, Color.Empty, br.Color); } } else { Debug.Assert(computeContentBounds); resultBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedValueStr, flags, cellStyle, LinkState == LinkState.Hover ? hoverFont : linkFont); } linkFont.Dispose(); hoverFont.Dispose(); }
/// <summary> /// 自定义绘制节点 /// </summary> /// <param name="e"></param> private void DrawNodeItem(DrawTreeNodeEventArgs e) { TreeNode tn = e.Node; if (tn.Level == 0) { using (Graphics g = e.Graphics) { //绘制分组的背景 RenderBackgroundInternalRate(g, e.Bounds, GroupBgColor, GroupBgColor, Color.FromArgb(200, 255, 255, 255), 0.45f, true, 300); //绘制展开按扭 g.FillEllipse(new SolidBrush(ExpandButtonColor), ExpandButtonBounds(e.Bounds)); g.DrawEllipse(new Pen(Color.LightGray), ExpandButtonBounds(e.Bounds)); Point p1; Point p2; Point p3; if (tn.IsExpanded) { p1 = new Point(ExpandButtonBounds(e.Bounds).X + 3, ExpandButtonBounds(e.Bounds).Bottom - 4); p2 = new Point(ExpandButtonBounds(e.Bounds).X + (ExpandButtonSize.Width) / 2, ExpandButtonBounds(e.Bounds).Top + 5); p3 = new Point(ExpandButtonBounds(e.Bounds).Right - 3, ExpandButtonBounds(e.Bounds).Bottom - 4); } else { p1 = new Point(ExpandButtonBounds(e.Bounds).X + 3, ExpandButtonBounds(e.Bounds).Y + 4); p2 = new Point(ExpandButtonBounds(e.Bounds).X + (ExpandButtonSize.Width) / 2, ExpandButtonBounds(e.Bounds).Bottom - 5); p3 = new Point(ExpandButtonBounds(e.Bounds).Right - 3, ExpandButtonBounds(e.Bounds).Y + 4); } GraphicsPath gp = new GraphicsPath(); gp.AddLine(p1, p2); gp.AddLine(p2, p3); g.DrawPath(new Pen(Color.FromArgb(255, 150, 0, 0), 2f), gp); //绘制分组的文本 TextRenderer.DrawText(g, e.Node.Text, this.Font, GroupTitleBounds(e.Bounds), this.GroupTitleColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis); } } else if (tn.Level == 1) { //e.DrawDefault = true; using (Graphics g = e.Graphics) { if (tn.IsSelected) { TextRenderer.DrawText(g, e.Node.Text, new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Underline), e.Bounds, OverForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter); } else { TextRenderer.DrawText(g, e.Node.Text, this.Font, e.Bounds, this.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter); } } } }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle != null); Point ptCurrentCell = DataGridView.CurrentCellAddress; bool cellSelected = (elementState & DataGridViewElementStates.Selected) != 0; bool cellCurrent = (ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex); Rectangle resultBounds; string formattedString = formattedValue as string; Color backBrushColor = PaintSelectionBackground(paintParts) && cellSelected ? cellStyle.SelectionBackColor : cellStyle.BackColor; Color foreBrushColor = cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor; if (paint && PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle valBounds = cellBounds; Rectangle borderWidths = BorderWidths(advancedBorderStyle); valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (valBounds.Height <= 0 || valBounds.Width <= 0) { return(Rectangle.Empty); } if (paint && PaintBackground(paintParts) && !backBrushColor.HasTransparency()) { using var backBrush = backBrushColor.GetCachedSolidBrushScope(); g.FillRectangle(backBrush, valBounds); } if (cellStyle.Padding != Padding.Empty) { if (DataGridView.RightToLeftInternal) { valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } valBounds.Width -= cellStyle.Padding.Horizontal; valBounds.Height -= cellStyle.Padding.Vertical; } Rectangle errorBounds = valBounds; if (valBounds.Height > 0 && valBounds.Width > 0 && (paint || computeContentBounds)) { switch (FlatStyle) { case FlatStyle.Standard: case FlatStyle.System: if (DataGridView.ApplyVisualStylesToInnerCells) { if (paint && PaintContentBackground(paintParts)) { PushButtonState pbState = VisualStyles.PushButtonState.Normal; if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { pbState = PushButtonState.Pressed; } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { pbState = PushButtonState.Hot; } if (PaintFocus(paintParts) && cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused) { pbState |= PushButtonState.Default; } DataGridViewButtonCellRenderer.DrawButton(g, valBounds, (int)pbState); } resultBounds = valBounds; valBounds = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetBackgroundContentRectangle(g, valBounds); } else { if (paint && PaintContentBackground(paintParts)) { ControlPaint.DrawBorder( g, valBounds, SystemColors.Control, (ButtonState == ButtonState.Normal) ? ButtonBorderStyle.Outset : ButtonBorderStyle.Inset); } resultBounds = valBounds; valBounds.Inflate(-SystemInformation.Border3DSize.Width, -SystemInformation.Border3DSize.Height); } break; case FlatStyle.Flat: // ButtonBase::PaintFlatDown and ButtonBase::PaintFlatUp paint the border in the same way valBounds.Inflate(-1, -1); if (paint && PaintContentBackground(paintParts)) { ButtonBaseAdapter.DrawDefaultBorder(g, valBounds, backBrushColor, isDefault: true); if (!backBrushColor.HasTransparency()) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintFlatRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); using var hdc = new DeviceContextHdcScope(g); using var hbrush = new Gdi32.CreateBrushScope( colors.options.HighContrast ? colors.buttonShadow : colors.lowHighlight); hdc.FillRectangle(valBounds, hbrush); } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { using var hdc = new DeviceContextHdcScope(g); using var hbrush = new Gdi32.CreateBrushScope(SystemColors.ControlDark); hdc.FillRectangle(valBounds, hbrush); } } } resultBounds = valBounds; break; default: Debug.Assert(FlatStyle == FlatStyle.Popup, "FlatStyle.Popup is the last flat style"); valBounds.Inflate(-1, -1); if (paint && PaintContentBackground(paintParts)) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { // paint down ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder( g, valBounds, colors.options.HighContrast ? colors.windowText : colors.windowFrame, isDefault: true); ControlPaint.DrawBorder( g, valBounds, colors.options.HighContrast ? colors.windowText : colors.buttonShadow, ButtonBorderStyle.Solid); } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { // paint over ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder( g, valBounds, colors.options.HighContrast ? colors.windowText : colors.buttonShadow, isDefault: false); ButtonBaseAdapter.Draw3DLiteBorder(g, valBounds, colors, true); } else { // paint up ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder( g, valBounds, colors.options.HighContrast ? colors.windowText : colors.buttonShadow, isDefault: false); ControlPaint.DrawBorderSimple( g, valBounds, colors.options.HighContrast ? colors.windowText : colors.buttonShadow); } } resultBounds = valBounds; break; } } else if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } else { resultBounds = Rectangle.Empty; } } else { Debug.Assert(valBounds.Height <= 0 || valBounds.Width <= 0); resultBounds = Rectangle.Empty; } if (paint && PaintFocus(paintParts) && cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && valBounds.Width > 2 * SystemInformation.Border3DSize.Width + 1 && valBounds.Height > 2 * SystemInformation.Border3DSize.Height + 1) { // Draw focus rectangle if (FlatStyle == FlatStyle.System || FlatStyle == FlatStyle.Standard) { ControlPaint.DrawFocusRectangle(g, Rectangle.Inflate(valBounds, -1, -1), Color.Empty, SystemColors.Control); } else if (FlatStyle == FlatStyle.Flat) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 || (DataGridView.CurrentCellAddress.Y == rowIndex && DataGridView.CurrentCellAddress.X == ColumnIndex)) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintFlatRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); string text = formattedString ?? string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonFlatAdapter.PaintFlatLayout( true, SystemInformation.HighContrast, 1, valBounds, Padding.Empty, false, cellStyle.Font, text, DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData layout = options.Layout(); ButtonBaseAdapter.DrawFlatFocus( g, layout.focus, colors.options.HighContrast ? colors.windowText : colors.constrastButtonShadow); } } else { Debug.Assert(FlatStyle == FlatStyle.Popup, "FlatStyle.Popup is the last flat style"); if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 || (DataGridView.CurrentCellAddress.Y == rowIndex && DataGridView.CurrentCellAddress.X == ColumnIndex)) { // If we are painting the current cell, then paint the text up. // If we are painting the current cell and the current cell is pressed down, then paint the text down. bool paintUp = (ButtonState == ButtonState.Normal); string text = formattedString ?? string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonPopupAdapter.PaintPopupLayout( paintUp, SystemInformation.HighContrast ? 2 : 1, valBounds, Padding.Empty, false, cellStyle.Font, text, DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData layout = options.Layout(); ControlPaint.DrawFocusRectangle( g, layout.focus, cellStyle.ForeColor, cellStyle.BackColor); } } } if (formattedString != null && paint && PaintContentForeground(paintParts)) { // Font independent margins valBounds.Offset(DATAGRIDVIEWBUTTONCELL_horizontalTextMargin, DATAGRIDVIEWBUTTONCELL_verticalTextMargin); valBounds.Width -= 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin; valBounds.Height -= 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin; if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 && FlatStyle != FlatStyle.Flat && FlatStyle != FlatStyle.Popup) { valBounds.Offset(1, 1); valBounds.Width--; valBounds.Height--; } if (valBounds.Width > 0 && valBounds.Height > 0) { Color textColor; if (DataGridView.ApplyVisualStylesToInnerCells && (FlatStyle == FlatStyle.System || FlatStyle == FlatStyle.Standard)) { textColor = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetColor(ColorProperty.TextColor); } else { textColor = foreBrushColor; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment( DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); TextRenderer.DrawText( g, formattedString, cellStyle.Font, valBounds, textColor, flags); } } if (DataGridView.ShowCellErrors && paint && PaintErrorIcon(paintParts)) { PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } return(resultBounds); }
private void DrawObjects(Graphics g, List <Map.Object> objects, bool shadow = false) { Pen pen; PointF center, ptf, topLeft; ThingDb.Thing tt; Map.Object underCursor = null; if (MapInterface.CurrentMode == EditMode.OBJECT_SELECT || MainWindow.Instance.mapView.picking) { underCursor = MainWindow.Instance.mapView.GetObjectUnderCursor(); } if (mapRenderer.proDefault && underCursor == null) { MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Default; } bool drawExtents3D = EditorSettings.Default.Draw_Extents_3D; if (EditorSettings.Default.Draw_Objects) { foreach (Map.Object oe in objects) { var customRender = oe as CustomRenderObject; if (customRender != null) { customRender.Render(g); continue; } ptf = oe.Location; float x = ptf.X, y = ptf.Y; tt = ThingDb.Things[oe.Name]; center = new PointF(x, y); topLeft = new PointF(center.X - objectSelectionRadius, center.Y - objectSelectionRadius); pen = mapRenderer.ColorLayout.Objects; // Object facing helper if (EditorSettings.Default.Draw_ObjectFacing) { float deg = -1F; if (tt.Xfer == "MonsterXfer") { MonsterXfer xfer = oe.GetExtraData <MonsterXfer>(); deg = (float)MonsterXfer.NOX_DIRECT_VALS[xfer.DirectionId] / 256F * 360F; } if (tt.Xfer == "NPCXfer") { NPCXfer xfer = oe.GetExtraData <NPCXfer>(); deg = (float)MonsterXfer.NOX_DIRECT_VALS[xfer.DirectionId] / 256F * 360F; } if (deg >= 0F) { using (var m = new System.Drawing.Drawing2D.Matrix()) { m.RotateAt(deg, center); g.Transform = m; g.DrawLine(objMoveablePen, center.X, center.Y, center.X + 20, center.Y); g.ResetTransform(); } } // Sentry ray if (tt.Xfer == "SentryXfer") { SentryXfer sentry = oe.GetExtraData <SentryXfer>(); float targX = x + ((float)Math.Cos(sentry.BasePosRadian) * 80F); float targY = y + ((float)Math.Sin(sentry.BasePosRadian) * 80F); // show sentry ray direction g.DrawLine(sentryPen, x, y, targX, targY); } } // invisible triggers and pressure plates if (tt.DrawType == "TriggerDraw" || tt.DrawType == "PressurePlateDraw") { if (shadow) { DrawObjectExtent(g, oe, drawExtents3D); } //else // DrawTriggerExtent(g, oe, underCursor); continue; } // black powder if (EditorSettings.Default.Edit_PreviewMode) // Visual Preview { if (tt.DrawType == "BlackPowderDraw") { /* Rectangle bp = new Rectangle((int)x - 2, (int)y - 2, 4, 4); * g.FillRectangle(new SolidBrush(Color.Gray), bp);*/ continue; } if (tt.Xfer == "InvisibleLightXfer") { /*bool isUnderCursor = false; * * Pen Penlight = new Pen(Color.Yellow, 1); * if (underCursor != null) isUnderCursor = underCursor.Equals(oe); * * if (isUnderCursor) * Penlight = new Pen(Color.Orange, 1); * * bool isSelected = mapRenderer.SelectedObjects.Items.Contains(oe); * bool isSelected2 = MapInterface.RecSelected.Contains(oe); * if (isSelected && isSelected2 && MapInterface.KeyHelper.ShiftKey) * { * isSelected = false; * isSelected2 = false; * } * * * if (isSelected || isSelected2) * { * Penlight = Pens.DarkOrange; * if (mapRenderer.proHand && isUnderCursor) * MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Hand; * * } * * g.DrawEllipse(Penlight, new RectangleF(center.X - 9, center.Y - 9, 18, 18)); * g.DrawEllipse(Penlight, new RectangleF(center.X - 13, center.Y - 13, 26, 26)); * g.DrawEllipse(Penlight, new RectangleF(center.X - 17, center.Y - 17, 34, 34)); */ continue; } Bitmap image = GetObjectImage(oe, shadow); /*if (tt.Name.StartsWith("Amb") && image == null) * { * image = mapRenderer.VideoBag.GetBitmap(tt.SpriteMenuIcon); * drawOffsetX = 82; * drawOffsetY = 122; * }*/ if (image == null || tt.DrawType == "NoDraw") { // in case of failure draw only the extent //DrawObjectExtent(g, oe, drawExtents3D); } else { int sizeX = tt.SizeX / 2; int sizeY = tt.SizeY / 2; x -= (sizeX - drawOffsetX); y -= (sizeY - drawOffsetY) + tt.Z; // no blurring int ix = Convert.ToInt32(x); int iy = Convert.ToInt32(y); // recolor in case it is being selected bool isSelected = mapRenderer.SelectedObjects.Items.Contains(oe); bool isSelected2 = MapInterface.RecSelected.Contains(oe); if (isSelected && isSelected2 && MapInterface.KeyHelper.ShiftKey) { isSelected = false; isSelected2 = false; } bool isUnderCursor = false; if (underCursor != null) { isUnderCursor = underCursor.Equals(oe); } // draw the image if (isSelected || isUnderCursor || shadow || isSelected2) { // highlight selection var shader = new BitmapShader(image); shader.LockBitmap(); var hltColor = mapRenderer.ColorLayout.Selection; if (isSelected || isSelected2) { if (mapRenderer.proHand && isUnderCursor) { MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Hand; } shader.ColorShade(hltColor, 0.5F); } else if (isUnderCursor) { hltColor = Color.PaleGreen; if (MapInterface.CurrentMode == EditMode.OBJECT_PLACE && !MainWindow.Instance.mapView.picking) { hltColor = mapRenderer.ColorLayout.Removing; } shader.ColorGradWaves(hltColor, 7F, Environment.TickCount); } if (shadow) { shader.MakeSemitransparent(165); } image = shader.UnlockBitmap(); g.DrawImage(image, ix, iy, image.Width, image.Height); image.Dispose(); } else { g.DrawImage(image, ix, iy, image.Width, image.Height); } } } else { if (mapRenderer.SelectedObjects.Items.Contains(oe)) { pen = Pens.Green; } if (MapInterface.RecSelected.Contains(oe)) { pen = Pens.Green; } if ((mapRenderer.SelectedObjects.Items.Contains(oe) && MapInterface.RecSelected.Contains(oe))) { pen = mapRenderer.ColorLayout.Objects; } g.DrawEllipse(pen, new RectangleF(topLeft, new Size(2 * objectSelectionRadius, 2 * objectSelectionRadius)));//55 // If is a door if ((tt.Class & ThingDb.Thing.ClassFlags.DOOR) == ThingDb.Thing.ClassFlags.DOOR) { DoorXfer door = oe.GetExtraData <DoorXfer>(); if (door.Direction == DoorXfer.DOORS_DIR.South) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X - 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X - 20, (int)center.Y - 20), new Point((int)center.X - 20, (int)center.Y - 60)); } } else if (door.Direction == DoorXfer.DOORS_DIR.West) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X + 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X + 20, (int)center.Y - 20), new Point((int)center.X + 20, (int)center.Y - 60)); } } else if (door.Direction == DoorXfer.DOORS_DIR.North) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y + 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X + 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X + 20, (int)center.Y + 20), new Point((int)center.X + 20, (int)center.Y - 20)); } } else if (door.Direction == DoorXfer.DOORS_DIR.East) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y + 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X - 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X - 20, (int)center.Y + 20), new Point((int)center.X - 20, (int)center.Y - 20)); } } } } if (EditorSettings.Default.Draw_Extents) { if (!(!EditorSettings.Default.Draw_AllExtents && oe.HasFlag(ThingDb.Thing.FlagsFlags.NO_COLLIDE))) { DrawObjectExtent(g, oe, drawExtents3D); } } } // Draw labels on the separate cycle to prevent layer glitching if (EditorSettings.Default.Draw_AllText) { foreach (Map.Object oe in objects) { Point textLocaton = new Point(Convert.ToInt32(oe.Location.X), Convert.ToInt32(oe.Location.Y)); textLocaton.X -= objectSelectionRadius; textLocaton.Y -= objectSelectionRadius; if (EditorSettings.Default.Draw_ObjCustomLabels && oe.Team > 0) { // Draw team Point loc = new Point(textLocaton.X, textLocaton.Y - 12); TextRenderer.DrawText(g, String.Format(FORMAT_OBJECT_TEAM, oe.Team), objectExtentFont, loc, Color.LightPink); } if (EditorSettings.Default.Draw_ObjCustomLabels && oe.Scr_Name.Length > 0) { // Draw custom label Size size = TextRenderer.MeasureText(oe.ScrNameShort, objectExtentFont); textLocaton.X -= size.Width / 3; TextRenderer.DrawText(g, oe.ScrNameShort, objectExtentFont, textLocaton, Color.Cyan); } else if (EditorSettings.Default.Draw_ObjThingNames) { // Draw thing name Size size = TextRenderer.MeasureText(oe.Name, objectExtentFont); textLocaton.X -= size.Width / 3; TextRenderer.DrawText(g, oe.Name, objectExtentFont, textLocaton, Color.Green); } else if (!EditorSettings.Default.Edit_PreviewMode && oe.Extent >= 0 && !(EditorSettings.Default.Draw_Extents || EditorSettings.Default.Draw_AllExtents)) { TextRenderer.DrawText(g, oe.Extent.ToString(), objectExtentFont, textLocaton, Color.Purple); } } } } }
public void DrawText(TextFormatFlags flags) { TextRenderer.DrawText(this.graphics, this.item.Text, this.item.Font, this.UpdateBounds(this.bounds, true), this.item.ForeColor, flags); }