void DrawLink(CustomDrawCellEventArgs e, GraphicsCache cache, string text) { e.DrawDefault(); e.Handled = true; var brush = cache.GetSolidBrush(Color.FromArgb(5, 111, 206)); var font = cache.GetFont(e.Font, FontStyle.Underline); SizeF size = cache.CalcTextSize(text, font, StringFormat.GenericDefault, 0); float height = (float)e.Bounds.Height - size.Height; RectangleF textBounds = new RectangleF(e.Bounds.Left + 8, e.Bounds.Top + height / 2, size.Width + 4, size.Height); cache.DrawString(text, font, brush, Rectangle.Round(textBounds), StringFormat.GenericDefault); }
// Handle the CustomDrawCell event to mark data entry fields with an asterisk. void spreadsheetControl1_CustomDrawCell(object sender, CustomDrawCellEventArgs e) { string cellReference = e.Cell.GetReferenceA1(); if (e.Cell.Worksheet.Name != "Invoice" || (cellReference != "A5" && cellReference != "A10" && cellReference != "F12")) { return; } e.Handled = true; e.DrawDefault(); using (Font font = new Font(e.Font.Name, 14f, FontStyle.Bold)) { string text = "*"; SizeF size = e.Graphics.MeasureString(text, font, Int32.MaxValue, StringFormat.GenericDefault); RectangleF textBounds = new RectangleF(e.Bounds.Right - size.Width - 2, e.Bounds.Bottom - size.Height * 0.7f, size.Width + 2, size.Height); e.Graphics.DrawString(text, font, e.Cache.GetSolidBrush(Color.Red), textBounds, StringFormat.GenericDefault); } }
private void spreadsheetControl1_CustomDrawCell(object sender, CustomDrawCellEventArgs e) { // Check whether a cell belongs to the "Category" column. if (e.Cell.ColumnIndex == 1) { // Obtain the category name. String categoryName = e.Cell.Value.ToString(); // Return the image that corresponds to the category name. Image categoryImage = ChooseImage(categoryName); // Specify the image location (the upper-right corner of the cell). Point point = new Point(e.Bounds.Right - 50, e.Bounds.Top); // Draw the category image over the cell. if (categoryImage != null) { e.Graphics.DrawImage(categoryImage, point); } } // If a cell belongs to the "Units In Stock" column and the cell value is "0", // draw a callout to the right of this cell. if (e.Cell.ColumnIndex == 4 && e.Cell.Value.IsNumeric && e.Cell.Value.NumericValue == 0) { // Specify callout text. string text = "OUT OF STOCK"; using (Font font = new Font(e.Font.Name, 9f, FontStyle.Bold)) { using (StringFormat format = new StringFormat()) { // Align callout text. format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Center; // Calculate the callout bounds. SizeF size = e.Graphics.MeasureString(text, font, int.MaxValue, format); Rectangle textBounds = new Rectangle( e.Bounds.Right + 15, (int)Math.Round(e.Bounds.Top + (e.Bounds.Height - size.Height) / 2), (int)(size.Width + 8), (int)Math.Round(size.Height)); int middle = (int)Math.Round(textBounds.Height / 2.0); Point[] points = new Point[] { new Point(textBounds.Left, textBounds.Top), new Point(textBounds.Left - middle, textBounds.Top + middle), new Point(textBounds.Left, textBounds.Bottom) }; // Draw the callout. Brush brush = e.Cache.GetSolidBrush(Color.FromArgb(200, 44, 74)); e.Graphics.FillPolygon(brush, points); e.Cache.FillRectangle(brush, textBounds); e.Graphics.DrawString(text, font, e.Cache.GetSolidBrush(Color.White), textBounds, format); } } } }
void DrawFormulaTip(object sender, CustomDrawCellEventArgs e) { if(String.IsNullOrEmpty(e.Cell.Formula)) return; }
void DrawErrorTooltip(CustomDrawCellEventArgs e, bool isError) { e.Handled = true; e.DrawDefault(); const int alpha = 255; Color foreColor = GetSemitransparentColor(SystemColors.InfoText, alpha); Color backColor = GetSemitransparentColor(SystemColors.Info, alpha); System.Drawing.Font font = tipFont; Rectangle bounds = e.Bounds; string text = isError ? IncorectData : e.Cell.Formula; SizeF size = e.Graphics.MeasureString(text, font, Int32.MaxValue, StringFormat.GenericDefault); Size tooltipSize = new Size((int)Math.Round(size.Width), (int)Math.Round(size.Height)); Rectangle textBounds = new Rectangle(bounds.X + 1, bounds.Y - tooltipSize.Height - 2, tooltipSize.Width+3, tooltipSize.Height); Rectangle markerBounds = textBounds; markerBounds.Inflate(2, 1); Point[] points = new Point[] { new Point(markerBounds.Left, markerBounds.Top), new Point(markerBounds.Right, markerBounds.Top), new Point(markerBounds.Right, markerBounds.Bottom), new Point(markerBounds.Left + Math.Min(e.FillBounds.Height / 4, markerBounds.Height / 2), markerBounds.Bottom), new Point(markerBounds.Left, markerBounds.Bottom + e.FillBounds.Height / 2), new Point(markerBounds.Left, markerBounds.Top) }; e.Graphics.FillPolygon(e.Cache.GetSolidBrush(backColor), points); e.Graphics.DrawPolygon(e.Cache.GetPen(foreColor), points); e.Graphics.DrawString(text, font, e.Cache.GetSolidBrush(foreColor), textBounds, StringFormat.GenericDefault); }