private void OnDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { // Только для первой колонки: рисуем CheckBox с надписью. if (e.ColumnIndex != 0) { e.DrawDefault = true; return; } e.DrawBackground(); bool value = false; var tag = e.Header.Tag; if (null != tag) { value = Convert.ToBoolean(tag); } var checkBoxState = value ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; var glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, checkBoxState); var textX = e.Bounds.Left + 4 + glyphSize.Width; CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.Left + 4, e.Bounds.Top + 4), new Rectangle(textX, e.Bounds.Top + 4, e.Header.Width - textX, FontHeight), e.Header.Text, Font, TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.WordEllipsis | TextFormatFlags.LeftAndRightPadding, false, checkBoxState); }
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) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); var p = new Point(); Size s = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal); p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2); p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2); _cellLocation = cellBounds.Location; _checkBoxLocation = p; _checkBoxSize = s; CheckBoxState checkboxState; if (_checked) { checkboxState = CheckBoxState.CheckedNormal; } else { checkboxState = CheckBoxState.UncheckedNormal; } CheckBoxRenderer.DrawCheckBox(graphics, _checkBoxLocation, checkboxState); }
private void ArgumentsList_MouseClick(object sender, MouseEventArgs e) { ListViewHitTestInfo Info = ArgumentsList.HitTest(e.Location); if (Info.Item.Index == ArgumentsList.Items.Count - 1) { ListViewItem NewItem = new ListViewItem(); NewItem.Checked = true; NewItem = ArgumentsList.Items.Insert(ArgumentsList.Items.Count - 1, NewItem); NewItem.BeginEdit(); } else { using (Graphics Graphics = ArgumentsList.CreateGraphics()) { int LabelOffset = e.X - CheckBoxPadding - CheckBoxRenderer.GetGlyphSize(Graphics, CheckBoxState.CheckedNormal).Width - CheckBoxPadding; if (LabelOffset >= 0 && LabelOffset < TextRenderer.MeasureText(Info.Item.Text, ArgumentsList.Font).Width) { Info.Item.BeginEdit(); } } } }
protected override void OnDrawItem(DrawItemEventArgs e) { var item = Items[e.Index] as CCBoxItem; var e2 = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State, e.ForeColor, item.BackColor); e2.DrawBackground(); var checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.MixedNormal); var dx = (e.Bounds.Height - checkSize.Width) / 2; var checkBoxState = GetItemChecked(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), checkBoxState); var rec = new Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height); using (var sf = new StringFormat { LineAlignment = StringAlignment.Center }) using (var brush = new SolidBrush(e.ForeColor)) { e.Graphics.DrawString(item.Name, Font, brush, rec, sf); } }
protected override void OnDrawNode(DrawTreeNodeEventArgs e) { TreeNode3 n = e.Node as TreeNode3; if (n == null) { e.DrawDefault = true; return; } CheckBoxState cbyTrue = CheckBoxState.CheckedNormal; CheckBoxState cbyFalse = CheckBoxState.UncheckedNormal; Rectangle rect = new Rectangle(e.Bounds.Location, new Size(ClientSize.Width, e.Bounds.Height)); glyph = CheckBoxRenderer.GetGlyphSize(e.Graphics, cbyTrue); int offset = glyph.Width * 3 + Spacing * 2 + LeftPadding; bool selected = n.IsSelected; if (selected) { e.Graphics.FillRectangle(SystemBrushes.MenuHighlight, rect); e.Graphics.DrawString(n.Label, Font, Brushes.White, e.Bounds.X + offset, e.Bounds.Y); } else { CheckBoxRenderer.DrawParentBackground(e.Graphics, e.Bounds, this); e.Graphics.DrawString(n.Label, Font, Brushes.Black, e.Bounds.X + offset, e.Bounds.Y); } CheckBoxState bs1 = n.Check1 ? cbyTrue : cbyFalse; CheckBoxState bs2 = n.Check2 ? cbyTrue : cbyFalse; CheckBoxState bs3 = n.Check3 ? cbyTrue : cbyFalse; CheckBoxRenderer.DrawCheckBox(e.Graphics, cbx(e.Bounds, 0).Location, bs1); CheckBoxRenderer.DrawCheckBox(e.Graphics, cbx(e.Bounds, 1).Location, bs2); CheckBoxRenderer.DrawCheckBox(e.Graphics, cbx(e.Bounds, 2).Location, bs3); }
// Three state checkbox column cell 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 checkBox cell is disabled, so paint the border, background, and disabled checkBox for the cell. if (!this.Enabled) { // 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 checkBox. CheckBoxState state = CheckBoxState.MixedDisabled; Size size = CheckBoxRenderer.GetGlyphSize(graphics, state); Point center = new Point(cellBounds.X, cellBounds.Y); center.X += (cellBounds.Width - size.Width) / 2; center.Y += (cellBounds.Height - size.Height) / 2; // Draw the disabled checkBox. CheckBoxRenderer.DrawCheckBox(graphics, center, state); } else { // The checkBox cell is enabled, so let the base class, handle the painting. base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } }
private int DrawCheckBox(Graphics g, Rectangle r) { int imageIndex = this.m_listItem.StateImageIndex; CheckBoxState boxState = CheckBoxState.UncheckedNormal; int switchValue = (imageIndex << 4); switch (switchValue) { case 0x00: boxState = CheckBoxState.UncheckedNormal; break; case 0x01: boxState = CheckBoxState.UncheckedHot; break; case 0x10: boxState = CheckBoxState.CheckedNormal; break; case 0x11: boxState = CheckBoxState.CheckedHot; break; case 0x20: boxState = CheckBoxState.MixedNormal; break; case 0x21: boxState = CheckBoxState.MixedHot; break; } CheckBoxRenderer.DrawCheckBox(g, new Point(r.X + 2, r.Y + (r.Height / 2) - 6), boxState); return(CheckBoxRenderer.GetGlyphSize(g, boxState).Width + 6); }
private void DrawListSubItem(object sender, DrawListViewSubItemEventArgs e) { ListView list = (ListView)sender; if (list.SelectedIndices.Contains(e.ItemIndex)) { var bounds = e.Bounds; bounds.X += 5; if (e.ColumnIndex == 0 && list.CheckBoxes) { var cbLocation = bounds.Location; cbLocation.X -= 1; cbLocation.Y += 1; var checkState = e.Item.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; CheckBoxRenderer.DrawCheckBox(e.Graphics, cbLocation, checkState); bounds.X += CheckBoxRenderer.GetGlyphSize(e.Graphics, checkState).Width; } if (e.ColumnIndex == 0) { var image = projectImages.Images[e.Item.ImageKey]; if (image != null) { e.Graphics.DrawImage(image, bounds.Location); bounds.X += image.Width; } } e.Graphics.DrawString(e.SubItem.Text, list.Font, SystemBrushes.HighlightText, bounds); } else { e.DrawDefault = true; } }
private void FillData() { var pk = m_table.FindConstraint <IPrimaryKey>(); if (pk != null) { m_pkCols.AddRange(pk.Columns.GetNames()); } foreach (var fk in m_table.GetConstraints <IForeignKey>()) { m_fkCols.AddRange(fk.Columns.GetNames()); } using (var g = Graphics.FromHwnd(IntPtr.Zero)) { m_lineHeight = (int)(g.MeasureString("M", Font).Height + 0.99); if (m_lineHeight < 16) { m_lineHeight = 16; } m_checkWidth = CheckBoxRenderer.GetGlyphSize(g, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal).Width; } panCols.Height = m_table.Columns.Count * m_lineHeight; labTableName.Text = m_table.FullName.ToString(); }
protected override void OnDrawItem(DrawItemEventArgs e) { if (e == null || e.Index < 0 || this.Count <= 0) { return; } Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal); int dx = (e.Bounds.Height - checkSize.Width) / 2; e.DrawBackground(); bool isChecked = GetItemChecked(e.Index);//For some reason e.State doesn't work so we have to do this instead. CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), isChecked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center }) { using (Brush brush = new SolidBrush(isChecked ? CheckedItemColor : ForeColor)) { e.Graphics.DrawString(Items[e.Index].ToString(), Font, brush, new Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height), sf); } } }
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) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); checkboxsize = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal); location.X = cellBounds.X + (cellBounds.Width / 2 - checkboxsize.Width / 2); location.Y = cellBounds.Y + (cellBounds.Height / 2 - checkboxsize.Height / 2); cellboundsLocation = cellBounds.Location; if (ischecked) { state = CheckBoxState.CheckedNormal; } else { state = CheckBoxState.UncheckedNormal; } CheckBoxRenderer.DrawCheckBox(graphics, location, state); }
//绘制列头checkbox protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); Point p = new Point(); Size s = CheckBoxRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); p.X = cellBounds.Location.X + (cellBounds.Width / 8); //列头checkbox的X坐标 p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2); //列头checkbox的Y坐标 _cellLocation = cellBounds.Location; checkBoxLocation = p; checkBoxSize = s; if (_checked) { _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal; } else { _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal; } CheckBoxRenderer.DrawCheckBox (graphics, checkBoxLocation, _cbState); }
/// <summary> /// Paints the specified graphics. /// </summary> /// <param name="graphics">The graphics.</param> /// <param name="clipBounds">The clip bounds.</param> /// <param name="cellBounds">The cell bounds.</param> /// <param name="rowIndex">Index of the row.</param> /// <param name="dataGridViewElementState">State of the data grid view element.</param> /// <param name="value">The value.</param> /// <param name="formattedValue">The formatted value.</param> /// <param name="errorText">The error text.</param> /// <param name="cellStyle">The cell style.</param> /// <param name="advancedBorderStyle">The advanced border style.</param> /// <param name="paintParts">The paint parts.</param> 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) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); Point _point = new Point(); Size _size = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal); _point.X = cellBounds.Location.X + (cellBounds.Width / 2) - (_size.Width / 2); _point.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (_size.Height / 2); cellLocation = cellBounds.Location; checkBoxLocation = _point; checkBoxSize = _size; if (ckstatus) { allCheckedState = CheckBoxState.CheckedNormal; } else { allCheckedState = CheckBoxState.UncheckedNormal; } CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, allCheckedState); }
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) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); Point x = new Point(); System.Drawing.Size glyphSize = CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal); Point location = cellBounds.Location; x.X = location.X + cellBounds.Width / 2 - glyphSize.Width / 2; location = cellBounds.Location; x.Y = location.Y + cellBounds.Height / 2 - glyphSize.Height / 2; this._cellLocation = cellBounds.Location; this.checkBoxLocation = x; this.checkBoxSize = glyphSize; if (!this._checked) { this._cbState = CheckBoxState.UncheckedNormal; } else { this._cbState = CheckBoxState.CheckedNormal; } CheckBoxRenderer.DrawCheckBox(graphics, this.checkBoxLocation, this._cbState); }
private void PackageSourcesListBox_DrawItem(object sender, DrawItemEventArgs e) { var currentListBox = (ListBox)sender; Graphics graphics = e.Graphics; e.DrawBackground(); if (e.Index < 0 || e.Index >= currentListBox.Items.Count) { return; } var currentItem = (Configuration.PackageSource)currentListBox.Items[e.Index]; using (StringFormat drawFormat = new StringFormat()) { using (Brush foreBrush = new SolidBrush(currentListBox.SelectionMode == SelectionMode.None ? SystemColors.WindowText : e.ForeColor)) { drawFormat.Alignment = StringAlignment.Near; drawFormat.Trimming = StringTrimming.EllipsisCharacter; drawFormat.LineAlignment = StringAlignment.Near; drawFormat.FormatFlags = StringFormatFlags.NoWrap; // the margin between the checkbox and the edge of the list box const int edgeMargin = 8; // the margin between the checkbox and the text const int textMargin = 4; // draw the enabled/disabled checkbox CheckBoxState checkBoxState = currentItem.IsEnabled ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; Size checkBoxSize = CheckBoxRenderer.GetGlyphSize(graphics, checkBoxState); CheckBoxRenderer.DrawCheckBox( graphics, new Point(edgeMargin, e.Bounds.Top + edgeMargin), checkBoxState); if (_checkBoxSize.IsEmpty) { // save the checkbox size so that we can detect mouse click on the // checkbox in the MouseUp event handler. // here we assume that all checkboxes have the same size, which is reasonable. _checkBoxSize = checkBoxSize; } GraphicsState oldState = graphics.Save(); try { // turn on high quality text rendering mode graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; // draw each package source as // // [checkbox] Name // Source (italics) int textWidth = e.Bounds.Width - checkBoxSize.Width - edgeMargin - textMargin; SizeF nameSize = graphics.MeasureString(currentItem.Name, e.Font, textWidth, drawFormat); // resize the bound rectangle to make room for the checkbox above var nameBounds = new Rectangle( e.Bounds.Left + checkBoxSize.Width + edgeMargin + textMargin, e.Bounds.Top, textWidth, (int)nameSize.Height); graphics.DrawString(currentItem.Name, e.Font, foreBrush, nameBounds, drawFormat); var sourceBounds = new Rectangle( nameBounds.Left, nameBounds.Bottom, textWidth, e.Bounds.Bottom - nameBounds.Bottom); graphics.DrawString(currentItem.Source, e.Font, foreBrush, sourceBounds, drawFormat); } finally { graphics.Restore(oldState); } // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); } } }
/// <summary> /// Dcs the specified e. /// </summary> /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param> /// <param name="foreColor">Color of the fore.</param> /// <param name="x">The x.</param> /// <param name="text">The text.</param> /// <param name="img">The img.</param> /// <param name="clicked">if set to <c>true</c> [clicked].</param> /// <param name="hovered">if set to <c>true</c> [hovered].</param> /// <param name="chk">if set to <c>true</c> [CHK].</param> /// <param name="chkbox">if set to <c>true</c> [chkbox].</param> /// <param name="width">The width.</param> /// <param name="font">The font.</param> /// <param name="tai">if set to <c>true</c> [tai].</param> /// <param name="ta">The ta.</param> /// <param name="ia">The ia.</param> /// <param name="pt">if set to <c>true</c> [pt].</param> /// <param name="ch">if set to <c>true</c> [ch].</param> /// <returns>System.Single.</returns> public static float dc(PaintEventArgs e, Color foreColor, float x = 0f, string text = "", Image img = null, bool clicked = false, bool hovered = false, bool chk = false, bool chkbox = false, float width = 0f, Font font = null, bool tai = true, ContentAlignment ta = ContentAlignment.MiddleCenter, ContentAlignment ia = ContentAlignment.MiddleLeft, bool pt = false, bool ch = true) { if (font == null) { font = SystemFonts.MessageBoxFont; } width = Math.Max((ch ? 15 : 3) + (chkbox ? 24 : 0) + (img != null ? img.Width : 0) + (!string.IsNullOrEmpty(text) ? TextRenderer.MeasureText(text, font).Width : 0) + (pt ? 13 : 0), width); if (clicked) { e.Graphics.DrawImage(ZeroitBreadCrumb.Clicked_Left_Edge, x, 0); for (int i = (int)x + ZeroitBreadCrumb.Clicked_Left_Edge.Width; i <= x + width - (ch ? ZeroitBreadCrumb.Clicked_Right_Triangle : ZeroitBreadCrumb.Clicked_Right_Edge).Width; i++) { e.Graphics.DrawImage(ZeroitBreadCrumb.Clicked_Body, i, 0); } e.Graphics.DrawImage(ch ? ZeroitBreadCrumb.Clicked_Right_Triangle : ZeroitBreadCrumb.Clicked_Right_Edge, x + width - (ch ? ZeroitBreadCrumb.Clicked_Right_Triangle : ZeroitBreadCrumb.Clicked_Right_Edge).Width, 0); } else if (hovered) { e.Graphics.DrawImage(ZeroitBreadCrumb.Hovered_Left_Edge, x, 0); for (int i = (int)x + ZeroitBreadCrumb.Hovered_Left_Edge.Width; i <= x + width - (ch ? ZeroitBreadCrumb.Hovered_Right_Triangle : ZeroitBreadCrumb.Hovered_Right_Edge).Width; i++) { e.Graphics.DrawImage(ZeroitBreadCrumb.Hovered_Body, i, 0); } e.Graphics.DrawImage((ch ? ZeroitBreadCrumb.Hovered_Right_Triangle : ZeroitBreadCrumb.Hovered_Right_Edge), x + width - (ch ? ZeroitBreadCrumb.Hovered_Right_Triangle : ZeroitBreadCrumb.Hovered_Right_Edge).Width, 0); } else if (chk && !chkbox) { e.Graphics.DrawImage(ZeroitBreadCrumb.Selected_Left_Edge, x, 0); for (int i = (int)x + ZeroitBreadCrumb.Selected_Left_Edge.Width; i <= x + width - (ch ? ZeroitBreadCrumb.Selected_Right_Triangle : ZeroitBreadCrumb.Selected_Right_Edge).Width; i++) { e.Graphics.DrawImage(ZeroitBreadCrumb.Selected_Body, i, 0); } e.Graphics.DrawImage((ch ? ZeroitBreadCrumb.Selected_Right_Triangle : ZeroitBreadCrumb.Selected_Right_Edge), x + width - (ch ? ZeroitBreadCrumb.Selected_Right_Triangle : ZeroitBreadCrumb.Selected_Right_Edge).Width, 0); } else { e.Graphics.DrawImage(ZeroitBreadCrumb.Left_Edge, x, 0); for (int i = (int)x + ZeroitBreadCrumb.Left_Edge.Width; i <= x + width - (ch ? ZeroitBreadCrumb.Right_Triangle : ZeroitBreadCrumb.Right_Edge).Width; i++) { e.Graphics.DrawImage(ZeroitBreadCrumb.Body, i, 0); } e.Graphics.DrawImage((ch ? ZeroitBreadCrumb.Right_Triangle : ZeroitBreadCrumb.Right_Edge), x + width - (ch ? ZeroitBreadCrumb.Right_Triangle : ZeroitBreadCrumb.Right_Edge).Width, 0); } if (chkbox) { var st = chk ? (clicked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedPressed : System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal) : (clicked ? System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedPressed : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); var sz = CheckBoxRenderer.GetGlyphSize(e.Graphics, st); CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point((int)(x + (pt ? 13 : 0) + (24 - sz.Height) / 2), (24 - sz.Height) / 2), st); } if (tai) { dit(e, foreColor, x + (pt ? 13 : 0), ta, text, font, chkbox, width, ia, img); } else { di(e, x, img, ia, chkbox, width); dt(e, foreColor, x + (pt ? 13 : 0), ta, text, font, chkbox, width); } return(width); }
protected override void OnDrawItem(DrawItemEventArgs e) { Brush backColorBrush = Brushes.White; Brush foreColorBrush = Brushes.Black; switch ((Elements)e.Index) { case Elements.Fire: backColorBrush = Brushes.Red; foreColorBrush = Brushes.White; break; case Elements.Lightning: backColorBrush = Brushes.Purple; // TODO: find a better color foreColorBrush = Brushes.White; break; case Elements.Ice: backColorBrush = Brushes.LightCyan; foreColorBrush = Brushes.Black; break; case Elements.Wind: backColorBrush = Brushes.Yellow; foreColorBrush = Brushes.Black; break; case Elements.Earth: backColorBrush = Brushes.Green; foreColorBrush = Brushes.White; break; case Elements.Water: backColorBrush = Brushes.LightBlue; foreColorBrush = Brushes.Black; break; case Elements.Holy: backColorBrush = Brushes.White; foreColorBrush = Brushes.Black; break; case Elements.Dark: backColorBrush = Brushes.Black; foreColorBrush = Brushes.White; break; default: // empty break; } e.Graphics.FillRectangle(backColorBrush, e.Bounds); CheckBoxState state = this.GetItemChecked(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; Size checkBoxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state); Point loc = new Point(1, (e.Bounds.Height - (checkBoxSize.Height + 1)) / 2 + 1); CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(loc.X + e.Bounds.X, loc.Y + e.Bounds.Y), state); e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, foreColorBrush, new PointF(loc.X + checkBoxSize.Width + 1 + e.Bounds.X, loc.Y + e.Bounds.Y)); if ((Defaults != null) && (Defaults.Length > e.Index) && (Defaults[e.Index] != GetItemChecked(e.Index))) { using (Pen p = new Pen(Color.Blue, 1)) { e.Graphics.DrawRectangle(p, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1)); } } if (!Enabled) { using (SolidBrush disabledRect = new SolidBrush(Color.FromArgb(100, Color.Gray))) { e.Graphics.FillRectangle(disabledRect, e.Bounds); } } }
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { //base.OnDrawSubItem(e); if (e.ItemIndex == -1) { return; } if (base.View == View.Details) { Rectangle bounds = e.Bounds; ListViewItemStates itemState = e.ItemState; Graphics g = e.Graphics; ListViewItem item = e.Item; bool selected = (itemState & ListViewItemStates.Selected) != 0; bool drawImage = false; bool fistItem = false; int imageIndex = -1; if (e.ColumnIndex == 0) { fistItem = true; if (item.ImageList != null) { if (item.ImageIndex != -1) { imageIndex = item.ImageIndex; } else if (!string.IsNullOrEmpty(item.ImageKey)) { imageIndex = item.ImageList.Images.IndexOfKey(item.ImageKey); } if (imageIndex != -1) { drawImage = true; } } } Rectangle backRect = bounds; Rectangle imageRect = Rectangle.Empty; if (drawImage) { imageRect = item.GetBounds(ItemBoundsPortion.Icon); backRect = item.GetBounds(ItemBoundsPortion.Label); backRect.X += 2; backRect.Width -= 2; } Color textColor = Color.Black; if (selected && (base.FullRowSelect || (!base.FullRowSelect && fistItem))) { backRect.Height--; Color innerBorderColor = Color.FromArgb(150, 255, 255, 255); //绘制选中区域 Image image = this.ItemBackgroundImage; g.DrawImage(image, backRect, 0, 0, 5, image.Height, GraphicsUnit.Pixel); g.DrawImage(image, backRect, 5, 0, image.Width - 10, image.Height, GraphicsUnit.Pixel); g.DrawImage(image, backRect, image.Width - 5, 0, 5, image.Height, GraphicsUnit.Pixel); textColor = Color.White; } else { using (SolidBrush brush = new SolidBrush(this._itemBackgroundColor)) { g.FillRectangle(brush, backRect); } } TextFormatFlags flags = this.GetFormatFlags(e.Header.TextAlign); if (drawImage)//如果绘制图像 { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; if (selected) { IntPtr hIcon = WinAPIDllImport.ImageList_GetIcon(item.ImageList.Handle, imageIndex, (int)ImageListDrawFlags.ILD_SELECTED); g.DrawIcon(Icon.FromHandle(hIcon), imageRect.X, imageRect.Y + 2); WinAPIDllImport.DestroyIcon(hIcon); } else { Image image = item.ImageList.Images[imageIndex]; g.DrawImage(image, imageRect.X, imageRect.Y + 2); } Rectangle textRect = new Rectangle(imageRect.Right + 3, bounds.Y, bounds.Width - imageRect.Right - 3, bounds.Height); TextRenderer.DrawText(g, item.Text, item.Font, textRect, textColor, flags); } else { bounds.X += 3; TextRenderer.DrawText(g, e.SubItem.Text, e.SubItem.Font, bounds, textColor, flags); } if (base.CheckBoxes) { if (e.Item.SubItems[0] == e.SubItem) { Size sizeCheckBox = CheckBoxRenderer.GetGlyphSize(e.Graphics, e.Item.Checked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); Point checkBoxPoint = new Point(e.Bounds.X, e.Bounds.Top + 10); CheckBoxRenderer.DrawCheckBox(e.Graphics, checkBoxPoint, e.Item.Checked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); } } } }
// Let the item paint itself, and then paint the RadioButton // where the check mark is normally displayed. protected override void OnPaint(PaintEventArgs e) { if (Image != null) { // If the client sets the Image property, the selection behavior // remains unchanged, but the RadioButton is not displayed and the // selection is indicated only by the selection rectangle. base.OnPaint(e); return; } else { // If the Image property is not set, call the base OnPaint method // with the CheckState property temporarily cleared to prevent // the check mark from being painted. CheckState currentState = this.CheckState; this.CheckState = CheckState.Unchecked; base.OnPaint(e); this.CheckState = currentState; } // Determine the correct state of the RadioButton. CheckBoxState buttonState = CheckBoxState.UncheckedNormal; if (Enabled) { if (mouseDownState) { if (Checked) { buttonState = CheckBoxState.CheckedPressed; } else { buttonState = CheckBoxState.UncheckedPressed; } } else if (mouseHoverState) { if (Checked) { buttonState = CheckBoxState.CheckedHot; } else { buttonState = CheckBoxState.UncheckedHot; } } else { if (Checked) { buttonState = CheckBoxState.CheckedNormal; } } } else { if (Checked) { buttonState = CheckBoxState.CheckedDisabled; } else { buttonState = CheckBoxState.UncheckedDisabled; } } // Calculate the position at which to display the RadioButton. Int32 offset = (ContentRectangle.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, buttonState).Height) / 2; Point imageLocation = new Point(ContentRectangle.Location.X + 4, ContentRectangle.Location.Y + offset); //// Paint the RadioButton. //RadioButtonRenderer.DrawRadioButton( // e.Graphics, imageLocation, buttonState); CheckBoxRenderer.DrawCheckBox(e.Graphics, imageLocation, Rectangle.Empty, String.Empty, this.Font, TextFormatFlags.HorizontalCenter, true, buttonState); }
protected override void OnDrawItem(DrawItemEventArgs e) { /* * Brush backColorBrush = Brushes.White; * Brush foreColorBrush = Brushes.Black; * * switch( (Element)e.Index ) * { * case Element.Fire: * backColorBrush = Brushes.Red; * foreColorBrush = Brushes.White; * break; * case Element.Lightning: * backColorBrush = Brushes.Purple; // TODO: find a better color * foreColorBrush = Brushes.White; * break; * case Element.Ice: * backColorBrush = Brushes.LightCyan; * foreColorBrush = Brushes.Black; * break; * case Element.Wind: * backColorBrush = Brushes.Yellow; * foreColorBrush = Brushes.Black; * break; * case Element.Earth: * backColorBrush = Brushes.Green; * foreColorBrush = Brushes.White; * break; * case Element.Water: * backColorBrush = Brushes.LightBlue; * foreColorBrush = Brushes.Black; * break; * case Element.Holy: * backColorBrush = Brushes.White; * foreColorBrush = Brushes.Black; * break; * case Element.Dark: * backColorBrush = Brushes.Black; * foreColorBrush = Brushes.White; * break; * default: * // empty * break; * } */ Brush backColorBrush = (e.Index < elementBGBrushes.Length ? elementBGBrushes[e.Index] : Brushes.White); Brush foreColorBrush = (e.Index < elementFGBrushes.Length ? elementFGBrushes[e.Index] : Brushes.Black); e.Graphics.FillRectangle(backColorBrush, e.Bounds); CheckBoxState state = this.GetItemChecked(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; Size checkBoxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state); Point loc = new Point(1, (e.Bounds.Height - (checkBoxSize.Height + 1)) / 2 + 1); CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(loc.X + e.Bounds.X, loc.Y + e.Bounds.Y), state); e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, foreColorBrush, new PointF(loc.X + checkBoxSize.Width + 1 + e.Bounds.X, loc.Y + e.Bounds.Y)); if ((Defaults != null) && (Defaults.Length > e.Index) && (Defaults[e.Index] != GetItemChecked(e.Index))) { using (Pen p = new Pen(Settings.ModifiedColor.BackgroundColor, 1)) { e.Graphics.DrawRectangle(p, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1)); } } if (!Enabled) { using (SolidBrush disabledRect = new SolidBrush(Color.FromArgb(100, Color.Gray))) { e.Graphics.FillRectangle(disabledRect, e.Bounds); } } }
/* Override the OnPaint method to support replacing the default checkmark with other options. */ protected override void OnPaint(PaintEventArgs e) { // If check mark is set to something besides default, temporarily clear the CheckState property // before calling base OnPaint to prevent the default check mark from being painted. if (this.CheckOnClick && this.CheckRenderMode != CheckRender.Default) { CheckState currentState = this.CheckState; Image currentImage = this.Image; if (this.CheckRenderMode == CheckRender.Custom && this.Checked) { if (this.CustomCheckedImage == null) { throw new NullReferenceException("CheckRenderMode is Custom but CustomCheckedImage is null."); } else // temporarily hijack the Image property to render custom check mark image { this.Image = CustomCheckedImage; } } else if (this.CheckRenderMode == CheckRender.CheckBox) { this.CheckState = CheckState.Unchecked; // CheckState property temporarily cleared to prevent painting of default mark } // Do standard painting base.OnPaint(e); // restore property values this.CheckState = currentState; this.Image = currentImage; } else // all normal; nothing to see here { base.OnPaint(e); return; } // Continue with any non-default check mark behavior // paint CheckBox control graphics in menu margin if (this.CheckRenderMode == CheckRender.CheckBox) { // Determine the correct state of the CheckBox. CheckBoxState boxState = CheckBoxState.UncheckedNormal; // initial value if (this.Enabled) { if (this.Pressed) { if (this.Checked) { boxState = CheckBoxState.CheckedPressed; } else { boxState = CheckBoxState.UncheckedPressed; } } else if (this.Selected) { if (this.Checked) { boxState = CheckBoxState.CheckedHot; } else { boxState = CheckBoxState.UncheckedHot; } } else if (this.Checked) { boxState = CheckBoxState.CheckedNormal; } } else { if (this.Checked) { boxState = CheckBoxState.CheckedDisabled; } else { boxState = CheckBoxState.UncheckedDisabled; } } // Calculate the position at which to display the CheckBox. int offsetY = (this.ContentRectangle.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, boxState).Height) / 2; Point imageLocation = new Point(this.ContentRectangle.Location.X + 4, this.ContentRectangle.Location.Y + offsetY); // Paint the CheckBox. CheckBoxRenderer.DrawCheckBox(e.Graphics, imageLocation, boxState); } }
/// <summary> /// Paints the drop-down, including all items within the scrolled region /// and, if appropriate, the scrollbar. /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (_scrollBarVisible) { Rectangle upper = new Rectangle(_scrollBar.DisplayRectangle.Left, _scrollBar.DisplayRectangle.Top, _scrollBar.DisplayRectangle.Width, _scrollBar.Thumb.Top - _scrollBar.DisplayRectangle.Top); Rectangle lower = new Rectangle(_scrollBar.DisplayRectangle.Left, _scrollBar.Thumb.Bottom, _scrollBar.DisplayRectangle.Width, _scrollBar.DisplayRectangle.Bottom - _scrollBar.Thumb.Bottom); if (_sourceControl.DrawWithVisualStyles && ScrollBarRenderer.IsSupported) { ScrollBarRenderer.DrawUpperVerticalTrack(e.Graphics, upper, GetScrollBarState(upper)); ScrollBarRenderer.DrawLowerVerticalTrack(e.Graphics, lower, GetScrollBarState(lower)); ScrollBarRenderer.DrawArrowButton(e.Graphics, _scrollBar.UpArrow, GetScrollBarStateUp()); ScrollBarRenderer.DrawArrowButton(e.Graphics, _scrollBar.DownArrow, GetScrollBarStateDown()); ScrollBarRenderer.DrawVerticalThumb(e.Graphics, _scrollBar.Thumb, GetScrollBarThumbState()); ScrollBarRenderer.DrawVerticalThumbGrip(e.Graphics, _scrollBar.Thumb, GetScrollBarThumbState()); } else { Rectangle bounds = _scrollBar.DisplayRectangle; bounds.Offset(1, 0); Rectangle up = _scrollBar.UpArrow; up.Offset(1, 0); Rectangle down = _scrollBar.DownArrow; down.Offset(1, 0); Rectangle thumb = _scrollBar.Thumb; thumb.Offset(1, 0); using (HatchBrush brush = new HatchBrush(HatchStyle.Percent50, SystemColors.ControlLightLight, SystemColors.Control)) { e.Graphics.FillRectangle(brush, bounds); } ControlPaint.DrawScrollButton(e.Graphics, up, ScrollButton.Up, GetButtonState(_scrollBar.UpArrow)); ControlPaint.DrawScrollButton(e.Graphics, down, ScrollButton.Down, GetButtonState(_scrollBar.DownArrow)); ControlPaint.DrawButton(e.Graphics, thumb, ButtonState.Normal); } } for (int i = _scrollOffset; i < (_scrollOffset + _numItemsDisplayed); i++) { bool highlighted = ((_highlightedItemIndex == i) && !_sourceControl.ShowCheckBoxes); NodeInfo item = _visibleItems[i]; // background if (highlighted) { e.Graphics.FillRectangle(SystemBrushes.Highlight, item.DisplayRectangle); } // image and glyphs if (item.Image != null) { Rectangle imgBounds = new Rectangle(item.DisplayRectangle.Location, item.Image.Size); e.Graphics.DrawImage(item.Image, imgBounds); if (_sourceControl.ShowCheckBoxes) { CheckBoxState state = GetCheckBoxState(item.Node.CheckState, item.CheckRectangle); Size chkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state); CheckBoxRenderer.DrawCheckBox(e.Graphics, Point.Add(item.CheckRectangle.Location, new Size((item.CheckRectangle.Width - chkSize.Width) / 2, (item.CheckRectangle.Height - chkSize.Height) / 2)), state); } } Rectangle textBounds = new Rectangle(item.DisplayRectangle.X + item.Image.Width + 2, item.DisplayRectangle.Y, item.DisplayRectangle.Width - item.Image.Width - 4, _itemHeight); using (Font font = new Font(Font, _visibleItems[i].Node.FontStyle)) { TextRenderer.DrawText(e.Graphics, item.Node.Text, font, textBounds, highlighted ? SystemColors.HighlightText : ForeColor, TEXT_FORMAT_FLAGS); } if (highlighted && _sourceControl.Focused && _sourceControl.ShowsFocusCues) { ControlPaint.DrawFocusRectangle(e.Graphics, item.DisplayRectangle); } } }
/// <summary> /// 重绘 /// </summary> protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, @"", @"", errorText, cellStyle, advancedBorderStyle, paintParts); this.m_chkboxRegion = RectangleCommon.GetSmallRectOfRectangle(cellBounds, CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.UncheckedNormal), out m_absChkboxRegion); this.RenderCheckBox(graphics); }
/// <summary> /// Gets the size of the image used to display the button. /// </summary> /// <param name="g">Current <see cref="Graphics"/> context.</param> /// <returns>The size of the image.</returns> protected override Size GetButtonSize(Graphics g) => CheckBoxRenderer.GetGlyphSize(g, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal);
private void CleanWorkspaceWindow_Load(object sender, EventArgs e) { IntPtr FolderIconPtr; ExtractIconEx("imageres.dll", 3, IntPtr.Zero, out FolderIconPtr, 1); IntPtr FileIconPtr; ExtractIconEx("imageres.dll", 2, IntPtr.Zero, out FileIconPtr, 1); Icon[] Icons = new Icon[] { Icon.FromHandle(FolderIconPtr), Icon.FromHandle(FileIconPtr) }; Size LargestIconSize = Size.Empty; foreach (Icon Icon in Icons) { LargestIconSize = new Size(Math.Max(LargestIconSize.Width, Icon.Width), Math.Max(LargestIconSize.Height, Icon.Height)); } Size LargestCheckBoxSize = Size.Empty; using (Graphics Graphics = Graphics.FromHwnd(IntPtr.Zero)) { foreach (CheckBoxState State in CheckBoxStates) { Size CheckBoxSize = CheckBoxRenderer.GetGlyphSize(Graphics, State); LargestCheckBoxSize = new Size(Math.Max(LargestCheckBoxSize.Width, CheckBoxSize.Width), Math.Max(LargestCheckBoxSize.Height, CheckBoxSize.Height)); } } Size ImageSize = new Size(LargestCheckBoxSize.Width + LargestIconSize.Width, Math.Max(LargestIconSize.Height, LargestCheckBoxSize.Height)); Bitmap TypeImageListBitmap = new Bitmap(Icons.Length * 3 * ImageSize.Width, ImageSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics Graphics = Graphics.FromImage(TypeImageListBitmap)) { int MinX = 0; for (int IconIdx = 0; IconIdx < Icons.Length; IconIdx++) { for (int StateIdx = 0; StateIdx < 3; StateIdx++) { Size CheckBoxSize = CheckBoxRenderer.GetGlyphSize(Graphics, CheckBoxStates[StateIdx]); CheckBoxRenderer.DrawCheckBox(Graphics, new Point(MinX + (LargestCheckBoxSize.Width - CheckBoxSize.Width) / 2, (LargestCheckBoxSize.Height - CheckBoxSize.Height) / 2), CheckBoxStates[StateIdx]); Size IconSize = Icons[IconIdx].Size; Graphics.DrawIcon(Icons[IconIdx], MinX + LargestCheckBoxSize.Width + (LargestIconSize.Width - IconSize.Width) / 2, (LargestIconSize.Height - IconSize.Height) / 2); MinX += ImageSize.Width; } } } ImageList TypeImageList = new ImageList(); TypeImageList.ImageSize = ImageSize; TypeImageList.ColorDepth = ColorDepth.Depth32Bit; TypeImageList.Images.AddStrip(TypeImageListBitmap); TreeView.ImageList = TypeImageList; TreeNode Node = BuildTreeViewStructure(RootFolderToClean, "/", false, 0); Node.Text = RootFolderToClean.Directory.FullName; TreeView.Nodes.Add(Node); }
/// <summary> /// Raises the <see cref="ListView.DrawSubItem"/> event. /// </summary> /// <remarks> /// This is where the owner draws the specific sub item. We handle this. /// </remarks> /// <param name="e">A <see cref="DrawListViewSubItemEventArgs"/> describing the event arguments.</param> protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { //if the item is not in a list view, then don't bother trying to draw it, // as it isn't visible anyway if (e.Item.ListView == null) { return; } base.OnDrawSubItem(e); e.DrawBackground(); Int32 intBoundsX = e.Bounds.X; Int32 intBoundsY = e.Bounds.Y; Int32 intBoundsWidth = e.Bounds.Width; Int32 intFontX = e.Bounds.X + 3; Int32 intFontWidth = e.Bounds.Width - 3; if (e.Item.SubItems[0] == e.SubItem) { intBoundsX += 4; intBoundsWidth -= 4; if (CheckBoxes) { CheckBoxState cbsState = e.Item.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal; Size szeCheckboxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, cbsState); int intBoxY = intBoundsY + (e.Bounds.Height - szeCheckboxSize.Height) / 2; int intBoxX = intBoundsX; intBoundsX += 3 + szeCheckboxSize.Width; intBoundsWidth -= 3 + szeCheckboxSize.Width; intFontX += 3 + szeCheckboxSize.Width; intFontWidth -= 3 + szeCheckboxSize.Width; CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(intBoxX, intBoxY), cbsState); } m_intFocusBoundsX = intBoundsX; } Color clrForeColor = e.SubItem.ForeColor; if (e.Item.Selected) { clrForeColor = e.Item.ListView.Focused ? SystemColors.HighlightText : clrForeColor; Color clrBackColor = e.Item.ListView.Focused ? SystemColors.Highlight : SystemColors.Control; e.Graphics.FillRectangle(new SolidBrush(clrBackColor), new Rectangle(intBoundsX, intBoundsY, intBoundsWidth, e.Bounds.Height)); } if (Messages.ContainsKey(e.SubItem)) { Image imgIcon = Messages[e.SubItem].Value; Rectangle rctIconBounds = GetMessageIconBounds(e.Bounds, imgIcon, String.IsNullOrEmpty(e.SubItem.Text) ? true : false); Rectangle rctPaint = new Rectangle(new Point(rctIconBounds.X, intBoundsY + rctIconBounds.Y), rctIconBounds.Size); e.Graphics.DrawImage(imgIcon, rctPaint); intFontWidth -= rctIconBounds.Width; } Rectangle rctTextBounds = new Rectangle(intFontX, intBoundsY + 2, intFontWidth, e.Bounds.Height - 4); TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, rctTextBounds, clrForeColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter); if (e.Item.Focused) { Pen penFocusRectangle = new Pen(Brushes.Black); penFocusRectangle.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; e.Graphics.DrawRectangle(penFocusRectangle, new Rectangle(m_intFocusBoundsX, intBoundsY, e.Item.Bounds.Width - m_intFocusBoundsX - 1, e.Item.Bounds.Height - 1)); } }
protected override void OnDrawItem(DrawListViewItemEventArgs e) { if (e.Item == null) { return; } // Draw the background var item = (e.Item.Tag as CloudTaskItem); var textColor = item.GetTextColor(e.Item.Selected, m_TaskColorIsBkgnd); var backColor = item.GetBackColor(e.Item.Selected, m_TaskColorIsBkgnd); Brush textBrush = new SolidBrush(textColor); if (e.Item.Selected) { m_SelectionRect.Draw(Handle, e.Graphics, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height); } else if (m_TaskColorIsBkgnd && !backColor.IsEmpty) { using (Brush backBrush = new SolidBrush(backColor)) e.Graphics.FillRectangle(backBrush, e.Bounds); } else { e.DrawBackground(); } // Draw subitems StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Near; stringFormat.LineAlignment = StringAlignment.Center; stringFormat.FormatFlags = StringFormatFlags.NoWrap; Rectangle itemRect = new Rectangle(e.Bounds.Location, e.Bounds.Size); for (int colIndex = 0; colIndex < e.Item.SubItems.Count; colIndex++) { itemRect.X += 2; itemRect.Width = (Columns[colIndex].Width - 2); if (colIndex == 0) { if (m_ShowCompletionCheckboxes) { if (m_CheckBoxSize.IsEmpty) { m_CheckBoxSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal); } var checkRect = CalcCheckboxRect(itemRect); CheckBoxRenderer.DrawCheckBox(e.Graphics, checkRect.Location, GetItemCheckboxState(item)); itemRect.X += CheckboxOffset; itemRect.Width -= CheckboxOffset; } if (m_TaskMatchesHaveIcons) { if ((e.Item.ImageIndex != -1) && m_TaskIcons.Get(item.Id)) { int imageSize = ImageSize; Rectangle iconRect = new Rectangle(itemRect.Location, new Size(imageSize, imageSize)); iconRect.Y += ((itemRect.Height - imageSize) / 2); m_TaskIcons.Draw(e.Graphics, iconRect.Left, iconRect.Top); } itemRect.X += TextIconOffset; itemRect.Width -= TextIconOffset; } } itemRect.Y++; itemRect.Height--; DrawText(e.Graphics, e.Item.SubItems[colIndex].Text, itemRect, textBrush, StringAlignment.Near, (colIndex == 0)); // next subitem itemRect.X += itemRect.Width; } }
/// <summary> /// Paint single flags item. /// </summary> /// <param name="graphics">The Graphics used to paint the DataGridViewCell.</param> /// <param name="checkBoxBounds">Rectangel that contains the bounds for checkbox</param> /// <param name="cellStyle">A DataGridViewCellStyle that contains formatting and style information about the cell.</param> /// <param name="text">Text of item to paint.</param> /// <param name="state">Check state of item to paint.</param> /// <param name="paint">True if check box should be painted.</param> /// <returns>Returns the end (right coordinate) of the drawed checkbox.</returns> private int PaintItem(Graphics graphics, Rectangle checkBoxBounds, DataGridViewCellStyle cellStyle, string text, bool state, bool paint) { //Calculate check box location. Point glyphLocation = new Point(checkBoxBounds.Left + CheckBoxMargin, checkBoxBounds.Top); // Initial flags to use for text in checkbox. TextFormatFlags flags = TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.NoPrefix; // Determine if should paint as hot flag bool isHot = DataInterpreter.CompareInvariant(text, hotItem); //Trace.WriteLine("Item " + text + " " + isHot.ToString()); //Trace.WriteLine("Hot item " + hotItem); CheckBoxState cbState = state ? (isHot ? CheckBoxState.CheckedHot : CheckBoxState.CheckedNormal) : (isHot ? CheckBoxState.UncheckedHot : CheckBoxState.UncheckedNormal); // Calculate check box glyph size. Size checkBoxSize = CheckBoxRenderer.GetGlyphSize(graphics, cbState); // Apply alingment switch (cellStyle.Alignment) { case DataGridViewContentAlignment.BottomCenter: glyphLocation.Y = checkBoxBounds.Bottom; flags |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; break; case DataGridViewContentAlignment.BottomLeft: glyphLocation.Y = checkBoxBounds.Bottom; flags |= TextFormatFlags.Bottom | TextFormatFlags.Left; break; case DataGridViewContentAlignment.BottomRight: glyphLocation.Y = checkBoxBounds.Bottom; flags |= TextFormatFlags.Bottom | TextFormatFlags.Right; break; case DataGridViewContentAlignment.MiddleCenter: glyphLocation.Y = checkBoxBounds.Top + (checkBoxBounds.Height - checkBoxSize.Height) / 2; flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter; break; case DataGridViewContentAlignment.MiddleLeft: glyphLocation.Y = checkBoxBounds.Top + (checkBoxBounds.Height - checkBoxSize.Height) / 2; flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left; break; case DataGridViewContentAlignment.MiddleRight: glyphLocation.Y = checkBoxBounds.Top + (checkBoxBounds.Height - checkBoxSize.Height) / 2; flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right; break; case DataGridViewContentAlignment.TopCenter: glyphLocation.Y = checkBoxBounds.Top; flags |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; break; case DataGridViewContentAlignment.TopLeft: glyphLocation.Y = checkBoxBounds.Top; flags |= TextFormatFlags.Top | TextFormatFlags.Left; break; case DataGridViewContentAlignment.TopRight: glyphLocation.Y = checkBoxBounds.Top; flags |= TextFormatFlags.Top | TextFormatFlags.Right; break; default: glyphLocation.Y = checkBoxBounds.Top; flags |= TextFormatFlags.Top | TextFormatFlags.Left; break; } // Add check boxe to the list checkboxes.Add(text, new Rectangle(glyphLocation, checkBoxSize)); // Calculate bounds for text Rectangle textBounds = new Rectangle( // Draw text after glyph plus small margin. checkBoxBounds.Left + 2 * CheckBoxMargin + checkBoxSize.Width, checkBoxBounds.Top, checkBoxBounds.Width - (2 * CheckBoxMargin + checkBoxSize.Width), checkBoxBounds.Height); // Darw checkbox only if we should to draw it and have enought space for glyph // and text if (paint && checkBoxBounds.Height >= checkBoxSize.Height && checkBoxBounds.Height >= cellStyle.Font.Height) { using (Region clipRegion = graphics.Clip) { // Draw checkbox CheckBoxRenderer.DrawCheckBox(graphics, glyphLocation, textBounds, text, cellStyle.Font, flags, false, cbState); graphics.Clip = clipRegion; } } // Calculate and return right coordinate for drawed checkbox with margin return(checkBoxSize.Width + TextRenderer.MeasureText(text, cellStyle.Font).Width + CheckBoxMargin); }
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 checkBox cell is disabled, so paint the border, // background, and disabled checkBox for the cell. if (!this.enabledValue) { // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) { Brush cellBackground = new SolidBrush(this.Selected ? cellStyle.SelectionBackColor : 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); } CheckState checkState = CheckState.Unchecked; if (formattedValue != null) { if (formattedValue is CheckState) { checkState = (CheckState)formattedValue; } else if (formattedValue is bool) { if ((bool)formattedValue) { checkState = CheckState.Checked; } } } CheckBoxState state = checkState == CheckState.Checked ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled; // Calculate the area in which to draw the checkBox. // force to unchecked!! Size size = CheckBoxRenderer.GetGlyphSize(graphics, state); Point center = new Point(cellBounds.X, cellBounds.Y); center.X += (cellBounds.Width - size.Width) / 2; center.Y += (cellBounds.Height - size.Height) / 2; // Draw the disabled checkBox. // We prevent painting of the checkbox if the Width, // plus a little padding, is too small. if (size.Width + 4 < cellBounds.Width) { CheckBoxRenderer.DrawCheckBox(graphics, center, state); } } else { // The checkBox cell is enabled, so let the base class // handle the painting. base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } }