public System.Drawing.Size Measure(System.Drawing.Graphics g, System.Drawing.Font font) { System.Drawing.Size size1 = Image == null ? System.Drawing.Size.Empty : Image.Size + (new System.Drawing.Size(8, 4)); System.Drawing.Size size2 = System.Drawing.Size.Round(g.MeasureString(Name, font)); System.Drawing.Size size3 = System.Drawing.Size.Round(g.MeasureString(Description, font, 2147483647, System.Drawing.StringFormat.GenericTypographic)); return new System.Drawing.Size(size1.Width + System.Math.Max(size2.Width, size3.Width), System.Math.Max(size2.Height + size3.Height + 5, size1.Height)); }
protected override void OnDraw(System.Drawing.Graphics graphics, System.Drawing.Color backgroundColor) { System.Drawing.SizeF size = graphics.MeasureString(lastFlow.ToString(),labelFont); graphics.FillRectangle(new System.Drawing.SolidBrush(backgroundColor), this.Location.X, this.Location.Y, size.Width, size.Height); lastFlow = Flow; base.OnDraw(graphics, backgroundColor); size = graphics.MeasureString(lastFlow.ToString(), labelFont); graphics.FillRectangle(new System.Drawing.SolidBrush(backgroundColor), this.Location.X, this.Location.Y, size.Width, size.Height); graphics.DrawRectangle(System.Drawing.Pens.Black, this.Location.X, this.Location.Y, size.Width, size.Height); graphics.DrawString(this.Flow.ToString(), labelFont, System.Drawing.Brushes.Black, this.Location); }
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { try { int progressVal = (int)value; if(progressVal < 0) progressVal = 0; if(progressVal > 100) progressVal = 100; float percentage = ((float)progressVal / 100.0f); Brush backColorBrush = new SolidBrush(cellStyle.BackColor); Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor); Brush barColorBrush = new SolidBrush(GetColorBetween(cellStyle.BackColor, cellStyle.ForeColor, progressVal == 100 ? 0.2f : 0.3f)); base.Paint(g, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground)); var s = progressVal.ToString() + "%"; var sz = g.MeasureString(s, cellStyle.Font); int dy = (cellBounds.Height - this.DataGridView.RowTemplate.Height) / 2; g.FillRectangle(barColorBrush, cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4); g.DrawString(s, cellStyle.Font, foreColorBrush, cellBounds.X + (cellBounds.Width / 2) - sz.Width / 2 - 5, cellBounds.Y + 2 + dy); } catch (Exception e) { } }
/// <summary> /// Renders a label to the map. /// </summary> /// <param name="g">Graphics reference</param> /// <param name="LabelPoint">Label placement</param> /// <param name="Offset">Offset of label in screen coordinates</param> /// <param name="font">Font used for rendering</param> /// <param name="forecolor">Font forecolor</param> /// <param name="backcolor">Background color</param> /// <param name="halo">Color of halo</param> /// <param name="rotation">Text rotation in degrees</param> /// <param name="text">Text to render</param> /// <param name="map">Map reference</param> public static void DrawLabel(System.Drawing.Graphics g, System.Drawing.PointF LabelPoint, System.Drawing.PointF Offset, System.Drawing.Font font, System.Drawing.Color forecolor, System.Drawing.Brush backcolor, System.Drawing.Pen halo, float rotation, string text, SharpMap.Map map) { System.Drawing.SizeF fontSize = g.MeasureString(text, font); //Calculate the size of the text LabelPoint.X += Offset.X; LabelPoint.Y += Offset.Y; //add label offset if (rotation != 0 && rotation != float.NaN) { g.TranslateTransform(LabelPoint.X, LabelPoint.Y); g.RotateTransform(rotation); g.TranslateTransform(-fontSize.Width / 2, -fontSize.Height / 2); if (backcolor != null && backcolor != System.Drawing.Brushes.Transparent) g.FillRectangle(backcolor, 0, 0, fontSize.Width * 0.74f + 1f, fontSize.Height * 0.74f); System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); path.AddString(text, font.FontFamily, (int)font.Style, font.Size, new System.Drawing.Point(0, 0), null); if (halo != null) g.DrawPath(halo, path); g.FillPath(new System.Drawing.SolidBrush(forecolor), path); //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0); g.Transform = map.MapTransform; } else { if (backcolor != null && backcolor != System.Drawing.Brushes.Transparent) g.FillRectangle(backcolor, LabelPoint.X, LabelPoint.Y, fontSize.Width * 0.74f + 1, fontSize.Height * 0.74f); System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); path.AddString(text, font.FontFamily, (int)font.Style, font.Size, LabelPoint, null); if (halo != null) g.DrawPath(halo, path); g.FillPath(new System.Drawing.SolidBrush(forecolor), path); //g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y); } }
public override void Draw(System.Drawing.Graphics g) { if (Bounds.IsEmpty) return; GPoint p1 = map.FromLatLngToLocal(Bounds.LocationTopLeft); GPoint p2 = map.FromLatLngToLocal(Bounds.LocationRightBottom); Font font = new Font(FontFamily.GenericSansSerif, 30, FontStyle.Bold); SizeF fs = g.MeasureString(Title, font); int x1 = p1.X; int y1 = p1.Y; int x2 = p2.X; int y2 = p2.Y; Rectangle r1 = new Rectangle(x1, y1 - (int)fs.Height, x2 - x1, y2 - y1 + (int)fs.Height); if (sbTable != null) { tableHeight = (int)(map.Font.GetHeight(g) * (sbTable.Rows.Count + 1)); tableWidth = 60 * 3; tableRect = new Rectangle(r1.Right, r1.Top + (int)fs.Height, tableWidth, tableHeight); r1.Width += tableWidth; r1.Height = (int)Math.Max(r1.Height, 2 * fs.Height + tableHeight); tableRect.Y = r1.Bottom - tableRect.Height - 1; } Rectangle r2 = r1; r2.Inflate(5, 5); Point p3 = new Point(r1.Left + (r1.Width - (int)fs.Width) / 2, r1.Top + 5); g.DrawString(Title, font, Brushes.Black, p3); g.DrawRectangle(penin, r1); g.DrawRectangle(penout, r2); //drawsbtjInfo(g, tableRect.Location); }
public void paint(System.Drawing.Graphics g, System.Windows.Forms.Control c) { //UPGRADE_TODO: The equivalent in .NET for method 'java.awt.Graphics.getFont' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.Drawing.Font font = SupportClass.GraphicsManager.manager.GetFont(g); System.Drawing.Font metrics = SupportClass.GraphicsManager.manager.GetFont(g); //UPGRADE_TODO: Class 'java.awt.font.FontRenderContext' was converted to 'System.Windows.Forms.Control' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'" //UPGRADE_ISSUE: Constructor 'java.awt.font.FontRenderContext.FontRenderContext' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtfontFontRenderContextFontRenderContext_javaawtgeomAffineTransform_boolean_boolean'" System.Drawing.Text.TextRenderingHint frc = TextRenderingHint.AntiAlias; // new FontRenderContext(null, false, false); System.Drawing.Size size = c.Size; SupportClass.GraphicsManager.manager.SetColor(g, c.BackColor); g.FillRectangle(SupportClass.GraphicsManager.manager.GetPaint(g), 0, 0, size.Width, size.Height); SupportClass.GraphicsManager.manager.SetColor(g, c.ForeColor); g.DrawRectangle(SupportClass.GraphicsManager.manager.GetPen(g), 0, 0, size.Width - 1, size.Height - 1); if (strs != null) { int y = 0; for (int i = 0; i < strs.Length; i++) { // TODO: use render hint? frc y += (int) g.MeasureString(strs[i], font).Height + 2; //UPGRADE_TODO: Method 'java.awt.Graphics.drawString' was converted to 'System.Drawing.Graphics.DrawString' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaawtGraphicsdrawString_javalangString_int_int'" g.DrawString(strs[i], SupportClass.GraphicsManager.manager.GetFont(g), SupportClass.GraphicsManager.manager.GetBrush(g), 3, y - SupportClass.GraphicsManager.manager.GetFont(g).GetHeight()); //g.drawString(strs[i],3,(metrics.getHeight())*(i+1)); } } }
public void Render(System.Drawing.Graphics g, System.Drawing.Rectangle bounds) { try { g.Clip = new Region(bounds); Rectangle textBounds; textBounds = new Rectangle(bounds.X + ClientSettings.Margin, bounds.Y, bounds.Width - (ClientSettings.Margin * 2), bounds.Height); DisplayItemDrawingHelper.DrawItemBackground(g, bounds, Selected); string itemText = PockeTwit.Localization.XmlBasedResourceManager.GetString("more"); SizeF textSize = g.MeasureString(itemText, ClientSettings.MenuFont); Point startPoint = new Point((int)(bounds.Left + (bounds.Width - textSize.Width) / 2),(int)(bounds.Top + (bounds.Height - textSize.Height) / 2)); Color drawColor = ClientSettings.MenuTextColor; using (Brush drawBrush = new SolidBrush(drawColor)) { g.DrawString(itemText, ClientSettings.MenuFont, drawBrush, startPoint.X, startPoint.Y); } } catch (ObjectDisposedException) { } }
public static System.Drawing.SizeF MeasureDisplayString(System.Drawing.Graphics graphics, string text, System.Drawing.Font font) { const int width = 32; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap (width, 1, graphics); System.Drawing.SizeF size = graphics.MeasureString (text, font); System.Drawing.Graphics anagra = System.Drawing.Graphics.FromImage (bitmap); int measured_width = (int) size.Width; if (anagra != null) { anagra.Clear (System.Drawing.Color.White); anagra.DrawString (text+"|", font, System.Drawing.Brushes.Black, width - measured_width, -font.Height / 2); for (int i = width-1; i >= 0; i--) { measured_width--; if (bitmap.GetPixel (i, 0).R == 0) { break; } } } return new System.Drawing.SizeF (measured_width, size.Height); }
public static string GetCuttedString(string s,sys.Drawing.Font font, sys.Drawing.Graphics gfx, int width) { int stringWidth = (int)gfx.MeasureString(s,font,width).Width; if(stringWidth < width) return s; int charWidth = (int)gfx.MeasureString("#",font,width).Width; int finalCut = stringWidth - width; finalCut = finalCut / charWidth; string temp = s.Substring(0,s.Length - finalCut - 1); return temp; }
/// <summary> /// Gets the text size width and height from a given GDI+ drawing surface and a given font /// </summary> /// <param name="graphics">Drawing surface to use</param> /// <param name="graphicsFont">Font type to measure</param> /// <param name="text">String of text to measure</param> /// <param name="width">Maximum width of the string</param> /// <param name="format">StringFormat object that represents formatting information, such as line spacing, for the string</param> /// <returns>A point structure with both size dimentions; x for width and y for height</returns> public static System.Drawing.Point GetTextSize(System.Drawing.Graphics graphics, System.Drawing.Font graphicsFont, System.String text, System.Int32 width, System.Drawing.StringFormat format) { System.Drawing.Point textSize; System.Drawing.SizeF tempSizeF; tempSizeF = graphics.MeasureString(text, graphicsFont, width, format); textSize = new System.Drawing.Point(); textSize.X = (int)tempSizeF.Width; textSize.Y = (int)tempSizeF.Height; return textSize; }
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { if (value == null) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); return; } PossibleMatch match = (PossibleMatch)value; // draw the native combo first, we are just painting on top of it, not redrawing from scratch base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, match.Movie.Title, errorText, cellStyle, advancedBorderStyle, paintParts); string yearText = "(" + match.Movie.Year + ")"; string altTitleText = "as " + match.Result.AlternateTitle; string providerName = match.Movie.PrimarySource == null ? string.Empty : match.Movie.PrimarySource.Provider.Name; // figure basic positioning SizeF providerSize = graphics.MeasureString(providerName, DataGridView.Font); SizeF titleSize = graphics.MeasureString(match.Movie.Title, DataGridView.Font); SizeF yearSize = graphics.MeasureString(yearText, DataGridView.Font); int providerPosition = (int)(cellBounds.X + cellBounds.Width - providerSize.Width - 25); int yearPosition = (int)(cellBounds.X + titleSize.Width); int altTitlePosition = (int)(cellBounds.X + titleSize.Width + yearSize.Width); // draw year RectangleF yearRect = new RectangleF(cellBounds.X + titleSize.Width, cellBounds.Y + 4, cellBounds.Width - providerSize.Width - 25, cellBounds.Height); graphics.DrawString(yearText, DataGridView.Font, SystemBrushes.ControlDark, yearRect); // draw alternate title if needed if (match.Result.AlternateTitleUsed()) { graphics.DrawString(altTitleText, DataGridView.Font, SystemBrushes.ControlDark, altTitlePosition, cellBounds.Y + 4); } // draw data provider if needed if (!string.IsNullOrEmpty(providerName)) { bool uncertainMatch = match.Result.TitleScore > 0 || match.Result.YearScore > 0; if (MovingPicturesCore.Settings.AlwaysDisplayProviderTags || !match.Result.FromTopSource || (uncertainMatch && match.HasMultipleSources)) { graphics.FillRectangle(SystemBrushes.ControlLight, providerPosition + 1, cellBounds.Y + 4, providerSize.Width + 1, cellBounds.Height - 10); graphics.DrawString(providerName, DataGridView.Font, SystemBrushes.ControlDark, providerPosition + 1, cellBounds.Y + 4); } } }
public override System.Drawing.Size Draw(System.Drawing.Graphics graphics, int x, int y, int width, int height, System.Drawing.Brush brush, System.Drawing.Pen pen, System.Drawing.Font font, bool setRect = true) { SizeF size = graphics.MeasureString(ToString(), font); graphics.DrawString(ToString(), font, brush, x, y); if (setRect) SetRect(x, y, width, (int)size.Height + 6); if (selected) graphics.DrawLine(pen, x, y, x, y + size.Height); return new Size(width, (int)size.Height + 6); }
public override System.Drawing.Size Draw(System.Drawing.Graphics graphics, int x, int y, int width, int height, Brush brush, Pen pen, Font font, Font mini_font, bool setRect = true) { SizeF size = graphics.MeasureString(ToString(), font); if (invert) graphics.DrawString("x", font, Brushes.Red, x, y); graphics.DrawString(ToString(), font, brush, x + ((invert)?15:0), y); if (setRect) SetRect(x, y, width, (int)size.Height + 6); if (selected) graphics.DrawLine(pen, x, y, x, y + size.Height); return new Size(width, (int)size.Height + 6); }
/// <summary> /// /// </summary> /// <param name="GraphicRef"></param> /// <param name="GraphicString"></param> /// <param name="clipRectangle"></param> /// <param name="fontResourceName">Either Font Family Name or the full path to the Font Resourse (RESX)</param> /// <param name="MaxFontSize"></param> /// <param name="MinFontSize"></param> /// <param name="SmallestOnFail"></param> /// <returns></returns> public static Font GetAdjustedFont(System.Drawing.Graphics GraphicRef, string GraphicString, System.Drawing.Rectangle clipRectangle, string fontResourceName, int MaxFontSize=100, int MinFontSize=10, bool SmallestOnFail=true) { bool privateFont = fontResourceName.ToLower().EndsWith("ttf"); Font OriginalFont=null; try { if (privateFont) OriginalFont = GetFontFromResx(fontResourceName); else OriginalFont= new Font(fontResourceName, 100); // We utilize MeasureString which we get via a control instance for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--) { Font TestFont; if (privateFont) TestFont = new Font(private_fonts.Families[0], AdjustedSize); else TestFont= new Font(fontResourceName, AdjustedSize); // Test the string with the new size SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont); if (clipRectangle.Width-4 > Convert.ToInt32(AdjustedSizeNew.Width) && clipRectangle.Height-4> Convert.ToInt32(AdjustedSizeNew.Height)) { // Good font, return it return TestFont; } else TestFont.Dispose(); } // If you get here there was no fontsize that worked // return MinimumSize or Original? if (SmallestOnFail) { return new Font(OriginalFont.Name, MinFontSize, OriginalFont.Style); } else { return OriginalFont; } } finally { if (OriginalFont != null) OriginalFont.Dispose(); } }
protected override void EscreverValoresEixoX(int width, int height, System.Collections.IDictionary props, System.Drawing.Graphics g, float puloX, double pontoX, int gapEsquerdo, int gapInferior, int gapDireito, float iniEixoX) { if (valoresX) { float ini; float pAnterior = 0; ini = gapEsquerdo + puloX; ini -= puloX / 2; for (float i = ini; i <= width - gapDireito; i += puloX) { string s; s = valorX(Math.Floor((i - gapEsquerdo) * pontoX + minX)); if (s.Length < 1) continue; SizeF tam = g.MeasureString(s, (Font)props["valorFont"]); if (!forçarValoresX && i + tam.Width / 2 >= iniEixoX) break; if (!forçarValoresX && i - tam.Width / 2 <= pAnterior) continue; g.DrawString(s, (Font)props["valorFont"], (Brush)props["valorBrush"], i - tam.Width / 2, height - gapInferior + 2, StringFormat.GenericDefault); pAnterior = i + tam.Width / 2 + 1; } } }
protected override void DrawText(int id, string str, System.Drawing.Font font, System.Drawing.Brush fc, System.Drawing.Rectangle bounds, System.Drawing.StringFormat sf, System.Drawing.Graphics g, bool selected) { float fix = g.MeasureString("傻", font).Width * 2 - g.MeasureString("傻逼", font).Width; int indentw = (int)(g.MeasureString(strCommand, font).Width - fix); int indent = GetIndent(id); int x, height, y; x = bounds.X; y = bounds.Y; height = bounds.Height; EventCommand cmd = GetCodeCommand(id); x += indent * indentw + indentw; if (cmd != null && cmd.IsGenerated) { int iw = (int)(g.MeasureString(strIndent, font).Width - fix); g.DrawString(strIndent, font, fc, new System.Drawing.Rectangle(x - iw, y, bounds.Right - x + iw, height)); } else { g.DrawString(strCommand, font, fc, new System.Drawing.Rectangle(x - indentw, y, bounds.Right - x + indentw, height)); } if (GetCode(id) == "0") return; if (cmd != null && !selected) fc = new System.Drawing.SolidBrush(cmd.Group.ForeColor); string drawing; if (cmd == null) drawing = strUnknown; else { drawing = cmd.FormatParams(this.Items[id] as RubyObject); } string draw; while (drawing.Length > 0) { int pos = drawing.IndexOf("{hide}"); if (pos > 0) { draw = drawing.Substring(0, pos); drawing = drawing.Substring(pos); g.DrawString(draw, font, fc, new System.Drawing.Rectangle(x, y, bounds.Right - x, height), sf); x += (int)(g.MeasureString(draw, font).Width - fix); } else if (pos == 0) { pos = drawing.IndexOf("{/hide}"); draw = drawing.Substring(6, pos - 6); drawing = drawing.Substring(pos + 7); x += (int)(g.MeasureString(draw, font).Width - fix); } else { g.DrawString(drawing, font, fc, new System.Drawing.Rectangle(x, y, bounds.Right - x, height), sf); drawing = ""; } } }
public void Render(System.Drawing.Graphics g, System.Drawing.Rectangle bounds) { try { g.Clip = new Region(bounds); //_currentOffset = bounds; var foreBrush = new SolidBrush(ClientSettings.ForeColor); Rectangle textBounds = new Rectangle(bounds.X + ClientSettings.Margin, bounds.Y, bounds.Width - (ClientSettings.Margin * 2), bounds.Height); var innerBounds = new Rectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height); innerBounds.Offset(1, 1); innerBounds.Width--; innerBounds.Height--; DisplayItemDrawingHelper.DrawItemBackground(g, innerBounds, Selected); textBounds.Offset(ClientSettings.Margin, 1); textBounds.Height--; //BreakUpTheText(g, textBounds); //int lineOffset = 0; SizeF textSize = g.MeasureString(TrendingTopic.Name, ClientSettings.MenuFont); Point startPoint = new Point((int)(bounds.Left + (bounds.Width - textSize.Width) / 2), (int)(bounds.Top + (bounds.Height - textSize.Height) / 2)); textBounds.Location = new Point(textBounds.X, textBounds.Y + startPoint.Y); textBounds.Height = 20; Color drawColor = ClientSettings.MenuTextColor; using (Brush drawBrush = new SolidBrush(drawColor)) { g.DrawString(TrendingTopic.Name, ClientSettings.MenuFont, drawBrush, startPoint.X, startPoint.Y - 20); g.DrawString(TrendingTopic.Description, ClientSettings.MenuFont, drawBrush, new RectangleF(textBounds.Left, textBounds.Top, textBounds.Width, textBounds.Height)); } //if (!ClientSettings.UseClickables) //{ // g.DrawString(Tweet.DisplayText, ClientSettings.TextFont, foreBrush, new RectangleF(textBounds.Left, textBounds.Top, textBounds.Width, textBounds.Height)); // //g.DrawString(Tweet.DisplayText, TextFont, ForeBrush, textBounds.Left, textBounds.Top, _mStringFormat); //} //else //{ // for (int i = 0; i < Tweet.SplitLines.Count; i++) // { // if (i >= ClientSettings.LinesOfText) // { // break; // } // float position = ((lineOffset * (ClientSettings.TextSize)) + textBounds.Top); // g.DrawString(Tweet.SplitLines[i], ClientSettings.TextFont, foreBrush, textBounds.Left, position, _mStringFormat); // lineOffset++; // } // MakeClickable(g, textBounds); // foreBrush.Dispose(); //} //g.Clip = new Region(); //Tweet.SplitLines = null; } catch (ObjectDisposedException) { } }
public void DrawEan13Barcode(System.Drawing.Graphics g, System.Drawing.Point pt) { float width = this.Width * this.Scale; float height = this.Height * this.Scale; // EAN13 Barcode should be a total of 113 modules wide. float lineWidth = width / 113f; // Save the GraphicsState. System.Drawing.Drawing2D.GraphicsState gs = g.Save(); // Set the PageUnit to Inch because all of our measurements are in inches. g.PageUnit = System.Drawing.GraphicsUnit.Millimeter; // Set the PageScale to 1, so a millimeter will represent a true millimeter. g.PageScale = 1; System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); ///调整条码左边距 float xPosition = 3;//0 System.Text.StringBuilder strbEAN13 = new System.Text.StringBuilder(); System.Text.StringBuilder sbTemp = new System.Text.StringBuilder(); float xStart = pt.X; float yStart = pt.Y; float xEnd = 0; System.Drawing.Font font = new System.Drawing.Font("Arial", this._fFontSize * this.Scale); // Calculate the Check Digit. //20121112注释,根据客户需求,不需要验证13码,直接打印即可 //this.CalculateChecksumDigit(); sbTemp.AppendFormat("{0}{1}{2}{3}", this.CountryCode, this.ManufacturerCode, this.ProductCode, this.ChecksumDigit); string sTemp = sbTemp.ToString(); string sLeftPattern = ""; // Convert the left hand numbers. sLeftPattern = ConvertLeftPattern(sTemp.Substring(0, 7)); // Build the UPC Code. strbEAN13.AppendFormat("{0}{1}{2}{3}{4}{1}{0}", this._sQuiteZone, this._sLeadTail, sLeftPattern, this._sSeparator, ConvertToDigitPatterns(sTemp.Substring(7), this._aRight)); string sTempUPC = strbEAN13.ToString(); float fTextHeight = g.MeasureString(sTempUPC, font).Height; // Draw the barcode lines. for (int i = 0; i < strbEAN13.Length; i++) { if (sTempUPC.Substring(i, 1) == "1") { if (xStart == pt.X) xStart = xPosition; // Save room for the UPC number below the bar code. if ((i > 12 && i < 55) || (i > 57 && i < 101)) // Draw space for the number g.FillRectangle(brush, xPosition, yStart, lineWidth, height - fTextHeight); else // Draw a full line. g.FillRectangle(brush, xPosition, yStart, lineWidth, height - fTextHeight / 2);//20121127修改,减短长线 } xPosition += lineWidth; xEnd = xPosition; } /**/ // Draw the upc numbers below the line. xPosition = xStart - g.MeasureString(this.CountryCode.Substring(0, 1), font).Width; float yPosition = yStart + (height - fTextHeight); // Draw 1st digit of the country code. g.DrawString(sTemp.Substring(0, 1), font, brush, new System.Drawing.PointF(xPosition, yPosition)); xPosition += (g.MeasureString(sTemp.Substring(0, 1), font).Width + 43 * lineWidth) - (g.MeasureString(sTemp.Substring(1, 6), font).Width); // Draw MFG Number. g.DrawString(sTemp.Substring(1, 6), font, brush, new System.Drawing.PointF(xPosition, yPosition)); xPosition += g.MeasureString(sTemp.Substring(1, 6), font).Width + (11 * lineWidth); // Draw Product ID. g.DrawString(sTemp.Substring(7), font, brush, new System.Drawing.PointF(xPosition, yPosition)); // Restore the GraphicsState. g.Restore(gs); }
Size GetFontSize( System.Drawing.Graphics tempGraphics, Font font, string testString ) { SizeF imageSize; imageSize = tempGraphics.MeasureString ( testString, font ); return new Size ( ( int ) imageSize.Width, ( int ) imageSize.Height ); }
public void DrawImage( System.Drawing.Graphics graphics) { float width = graphics.VisibleClipBounds.Width; float height = graphics.VisibleClipBounds.Height; Pen penGrid = new Pen(ColorGrid, 1); graphics.FillRectangle(BrushBackground, 0, 0, width, height); #region Horizontal Grid for (int i = 0; i < 5; i++) { float y = (height - MarginTop - MarginBottom) / 4 * i + MarginTop; graphics.DrawLine(penGrid, MarginLeft, y, width - MarginRight, y); string t = ""; switch(4-i) { case 0: t = "0"; break; case 1: t = "¼"; break; case 2: t = "½"; break; case 3: t = "¾"; break; case 4: t = "1"; break; } graphics.DrawString(t, FontGrid, new SolidBrush(ColorGrid), MarginLeft / 2, y-4); } graphics.DrawString("µ", FontGrid, new SolidBrush(ColorGrid), MarginLeft / 8, MarginTop - 4); #endregion #region Vertical grid if (_variableDimension is IContinuousDimension) { IContinuousDimension dim = (IContinuousDimension)_variableDimension; if (dim.Unit != "") { SizeF size = graphics.MeasureString(dim.Unit, FontGrid); graphics.DrawString(dim.Unit, FontGrid, new SolidBrush(ColorGrid), width - MarginRight - size.Width, height - MarginBottom + (float)(size.Height * 1.25)); } } decimal minValue; decimal maxValue; List<decimal> significantValues = _variableDimension.SignificantValues.ToList<decimal>(); if (this.SupportOnly) { if (_variableDimension is IContinuousDimension) { IContinuousDimension dim = (IContinuousDimension)_variableDimension; decimal ls = _relation.GetLowerSupportBound(inputsWithoutVariableInput); decimal us = _relation.GetUpperSupportBound(inputsWithoutVariableInput); decimal distance = us - ls; ls = ls - (distance * (decimal)(SupportSurroundings / 100)); us = us + (distance * (decimal)(SupportSurroundings / 100)); if (ls < dim.MinValue) ls = dim.MinValue; if (us > dim.MaxValue) us = dim.MaxValue; //If there is too little left, though, use at least two. while (significantValues.Count > 2) { if (significantValues[1] < ls) significantValues.RemoveAt(0); else if (significantValues[significantValues.Count - 2] > us) significantValues.RemoveAt(significantValues.Count - 1); else break; } } else { int valuesLeft = significantValues.Count; for (int i = significantValues.Count - 1; i >= 0; i--) { if (isMember(significantValues[i]) == 0) { significantValues.Remove(significantValues[i]); valuesLeft--; if (valuesLeft <= 3) break; } } } } if (_variableDimension is IContinuousDimension) { minValue = significantValues[0]; maxValue = significantValues[significantValues.Count - 1]; } else { minValue = 0; maxValue = significantValues.Count - 1; } bool toggledMode = MaxValueCountInSingleRow < significantValues.Count; bool oddCount = ((significantValues.Count % 2) == 1); uint c=0; foreach (decimal gridValue in significantValues) { decimal value; c++; bool sub = (((c % 2) == 0) && oddCount) || (((c % 2) == 1) && !oddCount); string label; if (_variableDimension is IContinuousDimension) { label = gridValue.ToString(); value = gridValue; } else { IDiscreteDimension dim = (IDiscreteDimension)_variableDimension; if (dim.DefaultSet!=null) label = dim.DefaultSet.GetMember(gridValue).Caption; else { label = "#" + gridValue.ToString("F0"); } value = c - 1; } float x = ((width-MarginLeft-MarginRight) / ((float)(maxValue-minValue))) * ((float)(value-minValue))+MarginLeft; graphics.DrawLine(penGrid, x, MarginTop, x, height - MarginBottom); SizeF size = graphics.MeasureString(label, FontGrid); graphics.DrawString(label, FontGrid, new SolidBrush(ColorGrid), x - size.Width / 2, height - MarginBottom + size.Height / 4 + (sub ? size.Height : 0)); #region Memberships for discrete set if (_variableDimension is IDiscreteDimension) { graphics.DrawLine(PenLine, x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1-isMember(gridValue)), x, height - MarginBottom); if (FullySpecified && gridValue == SpecifiedValue.Value) { graphics.DrawLine(PenValue, x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - isMember(gridValue)), x, height - MarginBottom); graphics.DrawLine(PenValue, MarginLeft, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - isMember(gridValue)), x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - isMember(gridValue))); } } #endregion } #endregion #region Line for continuous dimension if (_variableDimension is IContinuousDimension) { IntervalSet intervals = _relation.GetFunction(inputsWithoutVariableInput); foreach (Interval interval in intervals.Intervals) { if ( //(interval.LowerBound <= minValue && interval.UpperBound >= maxValue) || (interval.LowerBound >= minValue && interval.UpperBound <= maxValue) || (interval.LowerBound <= minValue && interval.UpperBound >= minValue) || (interval.LowerBound <= maxValue && interval.UpperBound >= maxValue) ) { decimal intervalMinValue = (interval.LowerBound < minValue ? minValue : interval.LowerBound); decimal intervalMaxValue = (interval.UpperBound > maxValue ? maxValue : interval.UpperBound); float intervalMinX = MarginLeft + ((width - MarginLeft - MarginRight) / (float)(maxValue - minValue)) * ((float)(intervalMinValue - minValue)); float intervalMaxX = MarginLeft + ((width - MarginLeft - MarginRight) / (float)(maxValue - minValue)) * ((float)(intervalMaxValue - minValue)); for (float x = intervalMinX; x <= intervalMaxX; x++) { decimal percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (decimal)((x - intervalMinX) / (intervalMaxX - intervalMinX)); decimal value = ((intervalMaxValue - intervalMinValue) * percentage) + intervalMinValue; double membership = isMember(value); //note that if y1 == y2 && x1 == x2, no point is plotted. graphics.DrawLine(PenLine, x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - membership), x, height - MarginBottom); } } } if (FullySpecified) { decimal percentage; if (SpecifiedValue.Value- minValue == 0) percentage = 0; else percentage = (SpecifiedValue.Value-minValue)/(maxValue-minValue); float x = (width-MarginLeft-MarginRight) * (float)percentage + MarginLeft; float y = MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - SpecifiedMembership.Value); graphics.DrawLine(PenValue, x, y, x, height - MarginBottom); graphics.DrawLine(PenValue, MarginLeft, y, x, y); //graphics.DrawLine(PenValue, 0, 0, 100, 100); } } #endregion }
private SharpMap.Rendering.Label CreateLabel(SharpMap.Geometries.Geometry feature,string text, float rotation, SharpMap.Styles.LabelStyle style, Map map, System.Drawing.Graphics g) { System.Drawing.SizeF size = g.MeasureString(text, style.Font); System.Drawing.PointF position = map.WorldToImage(feature.GetBoundingBox().GetCentroid()); position.X = position.X - size.Width * (short)style.HorizontalAlignment * 0.5f; position.Y = position.Y - size.Height * (short)style.VerticalAlignment * 0.5f; if (position.X-size.Width > map.Size.Width || position.X+size.Width < 0 || position.Y-size.Height > map.Size.Height || position.Y+size.Height < 0) return null; else { SharpMap.Rendering.Label lbl; if (!style.CollisionDetection) lbl = new SharpMap.Rendering.Label(text, position, rotation, this.Priority, null, style); else { //Collision detection is enabled so we need to measure the size of the string lbl = new SharpMap.Rendering.Label(text, position, rotation, this.Priority, new SharpMap.Rendering.LabelBox(position.X - size.Width * 0.5f - style.CollisionBuffer.Width, position.Y + size.Height * 0.5f + style.CollisionBuffer.Height, size.Width + 2f * style.CollisionBuffer.Width, size.Height + style.CollisionBuffer.Height * 2f), style); } if (feature.GetType() == typeof(SharpMap.Geometries.LineString)) { SharpMap.Geometries.LineString line = feature as SharpMap.Geometries.LineString; if (line.Length / map.PixelSize > size.Width) //Only label feature if it is long enough CalculateLabelOnLinestring(line, ref lbl, map); else return null; } return lbl; } }
/// <summary> /// Measures the dimensions of a string, as required by BuildFontImage() /// </summary> private static Size MeasureString( System.Drawing.Graphics graphics, string str, Font font ) { Size stringSize = new Size( ); for ( int charIndex = 0; charIndex < str.Length; ++charIndex ) { string subString = str.Substring( charIndex, 1 ); SizeF charSize = graphics.MeasureString( subString, font ); stringSize.Width += ( int )( charSize.Width ); stringSize.Height = Utils.Max( stringSize.Height, ( int )charSize.Height ); } return stringSize; }
internal java.awt.geom.Rectangle2D GetStringBounds(String aString, System.Drawing.Graphics g) { // TODO (KR) Could replace with System.Windows.Forms.TextRenderer#MeasureText (to skip creating Graphics) // // From .NET Framework Class Library documentation for Graphics#MeasureString: // // To obtain metrics suitable for adjacent strings in layout (for // example, when implementing formatted text), use the // MeasureCharacterRanges method or one of the MeasureString // methods that takes a StringFormat, and pass GenericTypographic. // Also, ensure the TextRenderingHint for the Graphics is // AntiAlias. // // TODO (KR) Consider implementing with one of the Graphics#MeasureString methods that takes a StringFormat. // TODO (KR) Consider implementing with Graphics#MeasureCharacterRanges(). if (aString.Length == 0) { SizeF size = g.MeasureString(aString, GetNetFont(), Int32.MaxValue, StringFormat.GenericTypographic); return new java.awt.geom.Rectangle2D.Float(0, 0, size.Width, size.Height); } else { StringFormat format = new StringFormat(StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap); format.Trimming = StringTrimming.None; format.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, aString.Length) }); Region[] regions = g.MeasureCharacterRanges(aString, GetNetFont(), new RectangleF(0, 0, int.MaxValue, int.MaxValue), format); SizeF size = regions[0].GetBounds(g).Size; regions[0].Dispose(); return new java.awt.geom.Rectangle2D.Float(0, -getAscent(), size.Width, size.Height); } }
//used to fire an event to retrieve formatting info //and then draw the cell with this formatting info protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) { DataGridFormatCellEventArgs e = null; bool callBaseClass = true; //fire the formatting event if (SetCellFormat != null) { int col = this.DataGridTableStyle.GridColumnStyles.IndexOf(this); e = new DataGridFormatCellEventArgs(rowNum, col, this.GetColumnValueAtRow(source, rowNum)); SetCellFormat(this, e); if (e.BackBrush != null) backBrush = e.BackBrush; //if these properties set, then must call drawstring if (e.ForeBrush != null || e.TextFont != null) { if (e.ForeBrush == null) e.ForeBrush = foreBrush; if (e.TextFont == null) e.TextFont = this.DataGridTableStyle.DataGrid.Font; g.FillRectangle(backBrush, bounds); Region saveRegion = g.Clip; Rectangle rect = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); using (Region newRegion = new Region(rect)) { g.Clip = newRegion; int charWidth = (int)Math.Ceiling(g.MeasureString("c", e.TextFont, 20, StringFormat.GenericTypographic).Width); string s = this.GetColumnValueAtRow(source, rowNum).ToString(); int maxChars = Math.Min(s.Length, (bounds.Width / charWidth)); try { g.DrawString(s.Substring(0, maxChars), e.TextFont, e.ForeBrush, bounds.X, bounds.Y + 2); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } //empty catch finally { g.Clip = saveRegion; } } callBaseClass = false; } if (!e.UseBaseClassDrawing) { callBaseClass = false; } } if (callBaseClass) base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); //clean up if (e != null) { if (e.BackBrushDispose) e.BackBrush.SafeDispose(); if (e.ForeBrushDispose) e.ForeBrush.SafeDispose(); if (e.TextFontDispose) e.TextFont.SafeDispose(); } }
/// <summary>This method will paint the group title.</summary> /// <param name="g">The paint event graphics object.</param> private void PaintGroupText(System.Drawing.Graphics g) { //Check if string has something------------- if(this.GroupTitle==string.Empty){return;} //------------------------------------------ //Set Graphics smoothing mode to Anit-Alias-- g.SmoothingMode = SmoothingMode.AntiAlias; //------------------------------------------- //Declare Variables------------------ SizeF StringSize = g.MeasureString(this.GroupTitle, this.Font); Size StringSize2 = StringSize.ToSize(); if(this.GroupImage!=null){StringSize2.Width+=18;} int ArcWidth = this.RoundCorners; int ArcHeight = this.RoundCorners; int ArcX1 = 20; int ArcX2 = (StringSize2.Width+34) - (ArcWidth + 1); int ArcY1 = 0; int ArcY2 = 24 - (ArcHeight + 1); System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor); System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness); System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null; System.Drawing.Brush BackgroundBrush = (this.PaintGroupBox) ? new SolidBrush(this.CustomGroupBoxColor) : new SolidBrush(this.BackgroundColor); System.Drawing.SolidBrush TextColorBrush = new SolidBrush(this.ForeColor); System.Drawing.SolidBrush ShadowBrush = null; System.Drawing.Drawing2D.GraphicsPath ShadowPath = null; //----------------------------------- //Check if shadow is needed---------- if(this.ShadowControl) { ShadowBrush = new SolidBrush(this.ShadowColor); ShadowPath = new System.Drawing.Drawing2D.GraphicsPath(); ShadowPath.AddArc(ArcX1+(this.ShadowThickness-1), ArcY1+(this.ShadowThickness-1), ArcWidth, ArcHeight, 180, Tools.GroupBoxConstants.SweepAngle); // Top Left ShadowPath.AddArc(ArcX2 + (this.ShadowThickness - 1), ArcY1 + (this.ShadowThickness - 1), ArcWidth, ArcHeight, 270, Tools.GroupBoxConstants.SweepAngle); //Top Right ShadowPath.AddArc(ArcX2 + (this.ShadowThickness - 1), ArcY2 + (this.ShadowThickness - 1), ArcWidth, ArcHeight, 360, Tools.GroupBoxConstants.SweepAngle); //Bottom Right ShadowPath.AddArc(ArcX1 + (this.ShadowThickness - 1), ArcY2 + (this.ShadowThickness - 1), ArcWidth, ArcHeight, 90, Tools.GroupBoxConstants.SweepAngle); //Bottom Left ShadowPath.CloseAllFigures(); //Paint Rounded Rectangle------------ g.FillPath(ShadowBrush, ShadowPath); //----------------------------------- } //----------------------------------- //Create Rounded Rectangle Path------ path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, Tools.GroupBoxConstants.SweepAngle); // Top Left path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, Tools.GroupBoxConstants.SweepAngle); //Top Right path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, Tools.GroupBoxConstants.SweepAngle); //Bottom Right path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, Tools.GroupBoxConstants.SweepAngle); //Bottom Left path.CloseAllFigures(); //----------------------------------- //Check if Gradient Mode is enabled-- if(this.PaintGroupBox) { //Paint Rounded Rectangle------------ g.FillPath(BackgroundBrush, path); //----------------------------------- } else { if(this.BackgroundGradientMode==GroupBoxGradientMode.None) { //Paint Rounded Rectangle------------ g.FillPath(BackgroundBrush, path); //----------------------------------- } else { BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode); //Paint Rounded Rectangle------------ g.FillPath(BackgroundGradientBrush, path); //----------------------------------- } } //----------------------------------- //Paint Borded----------------------- g.DrawPath(BorderPen, path); //----------------------------------- //Paint Text------------------------- int CustomStringWidth = (this.GroupImage!=null) ? 44 : 28; g.DrawString(this.GroupTitle, this.Font, TextColorBrush, CustomStringWidth, 5); //----------------------------------- //Draw GroupImage if there is one---- if(this.GroupImage!=null) { g.DrawImage(this.GroupImage, 28,4, 16, 16); } //----------------------------------- //Destroy Graphic Objects------------ if(path!=null){path.Dispose();} if(BorderBrush!=null){BorderBrush.Dispose();} if(BorderPen!=null){BorderPen.Dispose();} if(BackgroundGradientBrush!=null){BackgroundGradientBrush.Dispose();} if(BackgroundBrush!=null){BackgroundBrush.Dispose();} if(TextColorBrush!=null){TextColorBrush .Dispose();} if(ShadowBrush!=null){ShadowBrush.Dispose();} if(ShadowPath!=null){ShadowPath.Dispose();} //----------------------------------- }
public void DrawImage(System.Drawing.Graphics graphics) { float width = graphics.VisibleClipBounds.Width; float height = graphics.VisibleClipBounds.Height; Pen penGrid = new Pen(ColorGrid, 1); graphics.FillRectangle(BrushBackground, 0, 0, width, height); double minValue; double maxValue; List<double> significantValues = _variable.SignificantValues; minValue = (double)_variable.Min; //significantValues[0]; maxValue = (double)_variable.Max; //significantValues[significantValues.Count - 1]; #region Lines for terms // draw footprints int penIndex = 0; foreach (var term in _variable.Terms) { float intervalMinX = MarginLeft; float intervalMaxX = width - MarginRight; double step = (maxValue - minValue) / 10; for (double x = intervalMinX; x <= intervalMaxX; x += step) { double percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); double value = ((maxValue - minValue) * percentage) + minValue; double upperMembership = term.UpperMembershipFunction.GetValue(value); double lowerMembership = term.LowerMembershipFunction.GetValue(value); graphics.DrawLine(PenFootprint, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - upperMembership), (float)x, height - MarginBottom); graphics.DrawLine(PenFootprint, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - lowerMembership), (float)x, height - MarginBottom); } penIndex ++; } // draw lines on top penIndex = 0; foreach (var term in _variable.Terms) { float intervalMinX = MarginLeft; float intervalMaxX = width - MarginRight; double step = (maxValue - minValue) / 10; float? prevX = null; float? prevYUpper = null; float? prevYLower = null; for (double x = intervalMinX; x <= intervalMaxX; x += step) { double percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); double value = ((maxValue - minValue) * percentage) + minValue; double upperMembership = term.UpperMembershipFunction.GetValue(value); double lowerMembership = term.LowerMembershipFunction.GetValue(value); if (prevX != null && prevYUpper != null && prevYLower != null) { graphics.DrawLine(PensLine[penIndex], prevX.Value, prevYUpper.Value, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - upperMembership)); graphics.DrawLine(PensLine[penIndex], prevX.Value, prevYLower.Value, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - lowerMembership)); } prevX = (float)x; prevYUpper = MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - upperMembership); prevYLower = MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - lowerMembership); } penIndex++; } // draw type-2 footprints penIndex = 0; foreach (var term in _variable.Terms) { float intervalMinX = MarginLeft; float intervalMaxX = width - MarginRight; // double step = (maxValue - minValue) / 100 < 0.1 ? 0.1 : (maxValue - minValue) / 100; double step = 0.1; for (double x = intervalMinX; x <= intervalMaxX; x += step) { double percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); double value = ((maxValue - minValue) * percentage) + minValue; double upperMembership = term.UpperMembershipFunction.GetValue(value); double lowerMembership = term.LowerMembershipFunction.GetValue(value); graphics.DrawLine(PensFootprintType2[penIndex], (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - upperMembership), (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - lowerMembership)); } penIndex++; } //// the top chunk of the type-2 footprint - for uncertain means //penIndex = 0; //foreach (var term in _variable.Terms) //{ // float intervalMinX = MarginLeft; // float intervalMaxX = width - MarginRight; // // double step = (maxValue - minValue) / 100 < 0.1 ? 0.1 : (maxValue - minValue) / 100; // double step = 0.1; // bool isDraw = false; // for (double x = intervalMinX; x <= intervalMaxX; x += step) // { // double percentage; // if ((intervalMaxX - intervalMinX) == 0) // percentage = 0; // else // percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); // double value = ((maxValue - minValue) * percentage) + minValue; // double upperMembership = term.UpperMembershipFunction.GetValue(value); // double lowerMembership = term.LowerMembershipFunction.GetValue(value); // if (lowerMembership >= 1 || upperMembership >= 1) // { // if (!isDraw) // { // // start // isDraw = true; // } // else // { // // stop // isDraw = false; // } // } // if (isDraw) // { // graphics.DrawLine(PensFootprintType2[penIndex], (float)x, MarginTop, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - lowerMembership)); // } // } // penIndex++; //} #endregion #region Horizontal Grid for (int i = 0; i < 5; i++) { float y = (height - MarginTop - MarginBottom) / 4 * i + MarginTop; graphics.DrawLine(penGrid, MarginLeft, y, width - MarginRight, y); string t = ""; switch (4 - i) { case 0: t = "0"; break; case 1: t = "¼"; break; case 2: t = "½"; break; case 3: t = "¾"; break; case 4: t = "1"; break; } graphics.DrawString(t, FontGrid, new SolidBrush(ColorGrid), MarginLeft / 2, y - 4); } graphics.DrawString("µ", FontGrid, new SolidBrush(ColorGrid), MarginLeft / 8, MarginTop - 4); #endregion #region Vertical grid if (_variable.Unit != "") { SizeF size = graphics.MeasureString(_variable.Unit, FontGrid); graphics.DrawString(_variable.Unit, FontGrid, new SolidBrush(ColorGrid), width - MarginRight - size.Width, height - MarginBottom + (float)(size.Height * 1.25)); } //bool toggledMode = MaxValueCountInSingleRow < significantValues.Count; bool oddCount = ((significantValues.Count % 2) == 1); uint c = 0; foreach (double gridValue in significantValues) { double value; c++; bool sub = (((c % 2) == 0) && oddCount) || (((c % 2) == 1) && !oddCount); string label; label = gridValue.ToString(); value = gridValue; float x = ((width - MarginLeft - MarginRight) / ((float)(maxValue - minValue))) * ((float)(value - minValue)) + MarginLeft; graphics.DrawLine(penGrid, x, MarginTop, x, height - MarginBottom); SizeF size = graphics.MeasureString(label, FontGrid); graphics.DrawString(label, FontGrid, new SolidBrush(ColorGrid), x - size.Width / 2, height - MarginBottom + size.Height / 4 + (sub ? size.Height : 0)); } graphics.DrawString(_variable.Name, FontGrid, new SolidBrush(ColorGrid), width/2 - MarginRight, height - MarginBottom/3); #endregion }
public static ABC GetCharWidthABC(char ch, Font font, System.Drawing.Graphics gr) { var sf = StringFormat.GenericTypographic; sf.Trimming = StringTrimming.None; sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; return new ABC { abcA = 0, abcB = (uint)gr.MeasureString(ch.ToString(), font, new PointF(0, 0), sf).Width, abcC = 0 }; }
protected override Rectangle GetBodyTextRect(System.Drawing.Graphics dc, string text, Rectangle rect) { SizeF size = dc.MeasureString(text, font, rect.Size.Width, new StringFormat()); return new Rectangle(new Point(0, 0), Size.Ceiling(size)); }
public void DrawImage(System.Drawing.Graphics graphics) { float width = graphics.VisibleClipBounds.Width; float height = graphics.VisibleClipBounds.Height; Pen penGrid = new Pen(ColorGrid, 1); graphics.FillRectangle(BrushBackground, 0, 0, width, height); double minValue; double maxValue; List<double> significantValues = _variable.SignificantValues; minValue = (double)_variable.Min; //significantValues[0]; maxValue = (double)_variable.Max; //significantValues[significantValues.Count - 1]; #region Lines for terms // draw footprints foreach (var term in _variable.Terms) { var function = term.MembershipFunction; float intervalMinX = MarginLeft; float intervalMaxX = width - MarginRight; double step = (maxValue - minValue) / 10; for (double x = intervalMinX; x <= intervalMaxX; x += step) { double percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); double value = ((maxValue - minValue) * percentage) + minValue; double membership = function.GetValue(value); //note that if y1 == y2 && x1 == x2, no point is plotted. graphics.DrawLine(PenFootprint, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - membership), (float)x, height - MarginBottom); } } // draw lines on top int penIndex = 0; foreach (var term in _variable.Terms) { var function = term.MembershipFunction; float intervalMinX = MarginLeft; float intervalMaxX = width - MarginRight; double step = (maxValue - minValue) / 10; float? prevX = null; float? prevY = null; for (double x = intervalMinX; x <= intervalMaxX; x += step) { double percentage; if ((intervalMaxX - intervalMinX) == 0) percentage = 0; else percentage = (double)((x - intervalMinX) / (intervalMaxX - intervalMinX)); double value = ((maxValue - minValue) * percentage) + minValue; double membership = function.GetValue(value); if (prevX != null && prevY != null) { graphics.DrawLine(PensLine[penIndex], prevX.Value, prevY.Value, (float)x, MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - membership)); } prevX = (float)x; prevY = MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - membership); } penIndex++; } //if (FullySpecified) //{ // double percentage; // if (SpecifiedValue.Value - minValue == 0) // percentage = 0; // else // percentage = (SpecifiedValue.Value - minValue) / (maxValue - minValue); // float x = (width - MarginLeft - MarginRight) * (float)percentage + MarginLeft; // float y = MarginTop + (height - MarginBottom - MarginTop) * (float)(1 - SpecifiedMembership.Value); // graphics.DrawLine(PenValue, x, y, x, height - MarginBottom); // graphics.DrawLine(PenValue, MarginLeft, y, x, y); // //graphics.DrawLine(PenValue, 0, 0, 100, 100); //} #endregion #region Horizontal Grid for (int i = 0; i < 5; i++) { float y = (height - MarginTop - MarginBottom) / 4 * i + MarginTop; graphics.DrawLine(penGrid, MarginLeft, y, width - MarginRight, y); string t = ""; switch (4 - i) { case 0: t = "0"; break; case 1: t = "¼"; break; case 2: t = "½"; break; case 3: t = "¾"; break; case 4: t = "1"; break; } graphics.DrawString(t, FontGrid, new SolidBrush(ColorGrid), MarginLeft / 2, y - 4); } graphics.DrawString("µ", FontGrid, new SolidBrush(ColorGrid), MarginLeft / 8, MarginTop - 4); #endregion #region Vertical grid if (_variable.Unit != "") { SizeF size = graphics.MeasureString(_variable.Unit, FontGrid); graphics.DrawString(_variable.Unit, FontGrid, new SolidBrush(ColorGrid), width - MarginRight - size.Width, height - MarginBottom + (float)(size.Height * 1.25)); } //bool toggledMode = MaxValueCountInSingleRow < significantValues.Count; bool oddCount = ((significantValues.Count % 2) == 1); uint c = 0; foreach (double gridValue in significantValues) { double value; c++; bool sub = (((c % 2) == 0) && oddCount) || (((c % 2) == 1) && !oddCount); string label; label = gridValue.ToString(); value = gridValue; float x = ((width - MarginLeft - MarginRight) / ((float)(maxValue - minValue))) * ((float)(value - minValue)) + MarginLeft; graphics.DrawLine(penGrid, x, MarginTop, x, height - MarginBottom); SizeF size = graphics.MeasureString(label, FontGrid); graphics.DrawString(label, FontGrid, new SolidBrush(ColorGrid), x - size.Width / 2, height - MarginBottom + size.Height / 4 + (sub ? size.Height : 0)); } graphics.DrawString(_variable.Name, FontGrid, new SolidBrush(ColorGrid), width/2 - MarginRight, height - MarginBottom/3); #endregion }
protected override Rectangle GetTitleTextRect(System.Drawing.Graphics dc, string text, Rectangle rect) { StringFormat format = new StringFormat(StringFormatFlags.NoWrap); format.Trimming = StringTrimming.EllipsisCharacter; SizeF size = dc.MeasureString(text, titleFont, rect.Size.Width, format); return new Rectangle(new Point(0, 0), Size.Ceiling(size)); }