GetContrastingColor ( GraphDrawingContext oGraphDrawingContext, Byte btAlpha, Boolean bUseMaximumContrast ) { Debug.Assert(oGraphDrawingContext != null); AssertValid(); return(WpfGraphicsUtil.SetWpfColorAlpha( WpfGraphicsUtil.GetContrastingColor( oGraphDrawingContext.BackColor, bUseMaximumContrast), btAlpha)); }
DrawLabelBackground ( DrawingContext oDrawingContext, GraphDrawingContext oGraphDrawingContext, FormattedText oFormattedText, Color oFormattedTextColor, Byte btBackgroundAlpha, Point oTextOrigin ) { Debug.Assert(oDrawingContext != null); Debug.Assert(oGraphDrawingContext != null); Debug.Assert(oFormattedText != null); AssertValid(); if (oFormattedText.Width == 0 || oFormattedText.Height == 0) { return; } // Note: Don't make the background any more opaque than the text, which // might be translucent itself. Color oBackgroundColor = WpfGraphicsUtil.SetWpfColorAlpha( oGraphDrawingContext.BackColor, Math.Min(btBackgroundAlpha, oFormattedTextColor.A)); SolidColorBrush oBackgroundBrush = CreateFrozenSolidColorBrush( oBackgroundColor); Rect oBackgroundRectangle = WpfGraphicsUtil.GetFormattedTextBounds( oFormattedText, oTextOrigin); // Draw a rounded rectangle with a small amount of padding. const Int32 Padding = 1; const Int32 Radius = 2; oBackgroundRectangle.Inflate(Padding, Padding); oDrawingContext.DrawRoundedRectangle(oBackgroundBrush, null, oBackgroundRectangle, Radius, Radius); }
DrawLabel ( DrawingContext oDrawingContext, GraphDrawingContext oGraphDrawingContext, Point oEdgeEndpoint1, Point oEdgeEndpoint2, String sLabel, Color oColor ) { Debug.Assert(oDrawingContext != null); Debug.Assert(oGraphDrawingContext != null); Debug.Assert(sLabel != null); AssertValid(); if (sLabel.Length == 0) { return; } sLabel = TruncateLabel(sLabel); if (oEdgeEndpoint2.X < oEdgeEndpoint1.X) { // Don't let text be drawn upside-down. Point oTemp = oEdgeEndpoint2; oEdgeEndpoint2 = oEdgeEndpoint1; oEdgeEndpoint1 = oTemp; } Double dEdgeAngleDegrees = MathUtil.RadiansToDegrees( WpfGraphicsUtil.GetAngleBetweenPoints( oEdgeEndpoint1, oEdgeEndpoint2)); Double dEdgeLength = WpfGraphicsUtil.GetDistanceBetweenPoints( oEdgeEndpoint1, oEdgeEndpoint2); // To avoid trigonometric calculations, use a RotateTransform to make // the edge look as if it is horizontal, with oEdgeEndpoint2 to the // right of oEdgeEndpoint1. RotateTransform oRotateTransform = new RotateTransform( dEdgeAngleDegrees, oEdgeEndpoint1.X, oEdgeEndpoint1.Y); oEdgeEndpoint2 = oRotateTransform.Transform(oEdgeEndpoint2); oRotateTransform.Angle = -dEdgeAngleDegrees; oDrawingContext.PushTransform(oRotateTransform); FormattedText oFormattedText = CreateFormattedText(sLabel, oColor); oFormattedText.Trimming = TextTrimming.CharacterEllipsis; if (sLabel.IndexOf('\n') == -1) { // Unless the label includes line breaks, don't allow the // FormattedText class to break the text into multiple lines. oFormattedText.MaxLineCount = 1; } Double dTextWidth = oFormattedText.Width; // The ends of the label text are between one and two "buffer units" // from the ends of the edge. The buffer unit is the width of an // arbitrary character. Double dBufferUnit = CreateFormattedText("i", oColor).Width; Double dEdgeLengthMinusBuffers = dEdgeLength - 2 * dBufferUnit; if (dEdgeLengthMinusBuffers <= 0) { return; } // Determine where to draw the label text, and where to draw a // translucent rectangle behind the text. The translucent rectangle // serves to obscure, but not completely hide, the underlying edge. Point oLabelOrigin = oEdgeEndpoint1; Rect oTranslucentRectangle; if (dTextWidth > dEdgeLengthMinusBuffers) { // The label text should start one buffer unit from the first // endpoint, and terminate with ellipses approximately one buffer // length from the second endpoint. oLabelOrigin.Offset(dBufferUnit, 0); oFormattedText.MaxTextWidth = dEdgeLengthMinusBuffers; // The translucent rectangle should be the same width as the text. oTranslucentRectangle = new Rect(oLabelOrigin, new Size(dEdgeLengthMinusBuffers, oFormattedText.Height)); } else { // The label should be centered along the edge's length. oLabelOrigin.Offset(dEdgeLength / 2.0, 0); oFormattedText.TextAlignment = TextAlignment.Center; // The translucent rectangle should extend between zero and one // buffer units beyond the ends of the text. This provides a // margin between the text and the unobscured edge, if there is // enough space. oTranslucentRectangle = new Rect(oLabelOrigin, new Size( Math.Min(dTextWidth + 2 * dBufferUnit, dEdgeLengthMinusBuffers), oFormattedText.Height) ); oTranslucentRectangle.Offset( -oTranslucentRectangle.Width / 2.0, 0); } // The text and rectangle should be vertically centered on the edge. oDrawingContext.PushTransform( new TranslateTransform(0, -oFormattedText.Height / 2.0)); // Draw the translucent rectangle, then the text. // // Note: Don't make the rectangle any more opaque than the edge, which // might be translucent itself. oDrawingContext.DrawRectangle(GetBrush( WpfGraphicsUtil.SetWpfColorAlpha(oGraphDrawingContext.BackColor, Math.Min(LabelBackgroundAlpha, oColor.A)) ), null, oTranslucentRectangle); oDrawingContext.DrawText(oFormattedText, oLabelOrigin); oDrawingContext.Pop(); oDrawingContext.Pop(); }