本文整理汇总了C#中System.Windows.Media.DrawingContext.PushGuidelineSet方法的典型用法代码示例。如果您正苦于以下问题:C# DrawingContext.PushGuidelineSet方法的具体用法?C# DrawingContext.PushGuidelineSet怎么用?C# DrawingContext.PushGuidelineSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.DrawingContext
的用法示例。
在下文中一共展示了DrawingContext.PushGuidelineSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
var fe = AdornedElement as FrameworkElement;
if (fe == null)
{
return;
}
var rect = new Rect(1, 1, Math.Max(0, fe.ActualWidth - 2), Math.Max(0, fe.ActualHeight - 2));
var color = Colors.Red;
var brush = new SolidColorBrush(color);
var pen = new Pen(brush, 1);
pen.Freeze();
var dashPen = new Pen(brush, 1) { DashStyle = new DashStyle(new double[] { 1, 6 }, 0) };
dashPen.Freeze();
var guidelineSet = new GuidelineSet();
guidelineSet.GuidelinesX.Add(0.5);
guidelineSet.GuidelinesY.Add(0.5);
//var outlinePen = new Pen(new SolidColorBrush(Color.FromArgb(0x70, 0xFF, 0xFF, 0xFF)), 5);
//outlinePen.Freeze();
drawingContext.PushGuidelineSet(guidelineSet);
//drawingContext.DrawRectangle(null, outlinePen, rect);
drawingContext.DrawRectangle(null, pen, rect);
//var parent = VisualTreeHelper.GetParent(fe) as FrameworkElement;
//if (parent != null)
//{
// var thisLeft = new Point(0, fe.ActualHeight / 2);
// var thisRight = new Point(fe.ActualWidth, fe.ActualHeight / 2);
// var thisTop = new Point(fe.ActualWidth / 2, 0);
// var thisBottom = new Point(fe.ActualWidth / 2, fe.ActualHeight);
// var ancestorLeft = new Point(parent.TranslatePoint(thisLeft, fe).X, thisLeft.Y);
// var ancestorRight = ancestorLeft + new Vector(parent.ActualWidth, 0);
// var ancestorTop = new Point(thisTop.X, parent.TranslatePoint(new Point(), fe).Y);
// var ancestorBottom = new Point(thisBottom.X, parent.TranslatePoint(new Point(), fe).Y + parent.ActualHeight);
// var leftPen = fe.HorizontalAlignment == HorizontalAlignment.Left || fe.HorizontalAlignment == HorizontalAlignment.Stretch ? pen : dashPen;
// var rightPen = fe.HorizontalAlignment == HorizontalAlignment.Right || fe.HorizontalAlignment == HorizontalAlignment.Stretch ? pen : dashPen;
// var topPen = fe.VerticalAlignment == VerticalAlignment.Top || fe.VerticalAlignment == VerticalAlignment.Stretch ? pen : dashPen;
// var bottomPen = fe.VerticalAlignment == VerticalAlignment.Bottom || fe.VerticalAlignment == VerticalAlignment.Stretch ? pen : dashPen;
// drawingContext.DrawLine(leftPen, thisLeft, ancestorLeft);
// drawingContext.DrawLine(rightPen, thisRight, ancestorRight);
// drawingContext.DrawLine(topPen, thisTop, ancestorTop);
// drawingContext.DrawLine(bottomPen, thisBottom, ancestorBottom);
//}
var formattedHeight = new FormattedText(string.Format("{0:0}", fe.ActualHeight), CultureInfo.InvariantCulture, FlowDirection.LeftToRight, TypeFace, 10, brush);
var formattedWidth = new FormattedText(string.Format("{0:0}", fe.ActualWidth), CultureInfo.InvariantCulture, FlowDirection.LeftToRight, TypeFace, 10, brush);
drawingContext.DrawText(formattedHeight, new Point(rect.Width + 5, (rect.Height / 2) - (formattedHeight.Height / 2)));
drawingContext.DrawText(formattedWidth, new Point(rect.Width / 2 - formattedWidth.Width / 2, rect.Height + 5));
drawingContext.Pop();
}
示例2: Draw
protected override void Draw(DrawingContext dc, int RenderZoom)
{
EarthPoint topLeftPoint = OsmIndexes.GetTopLeftPoint(HorizontalIndex, VerticalIndex, RenderZoom);
Point topLeftPointScreenProjection = Projector.Project(topLeftPoint, RenderZoom);
dc.PushGuidelineSet(ScreenGuidelineSet);
var tileRect = new Rect(topLeftPointScreenProjection, new Size(256, 256));
DrawTile(dc, tileRect);
//dc.DrawRectangle(null, new Pen(Brushes.Gray, 2), tileRect);
}
示例3: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
Pen pen = new Pen(LineBrush, 1);
GuidelineSet gs = new GuidelineSet();
gs.GuidelinesX.Clear();
gs.GuidelinesY.Clear();
gs.GuidelinesY.Add(_lastPoint.Y - 0.5);
gs.GuidelinesY.Add(_lastPoint.Y + 0.5);
drawingContext.PushGuidelineSet(gs.Clone());
drawingContext.DrawLine(pen, new Point(0, _lastPoint.Y), new Point(ActualWidth, _lastPoint.Y));
drawingContext.PushGuidelineSet(gs.Clone());
gs.GuidelinesX.Clear();
gs.GuidelinesY.Clear();
gs.GuidelinesX.Add(_lastPoint.X - 0.5);
gs.GuidelinesX.Add(_lastPoint.X + 0.5);
drawingContext.PushGuidelineSet(gs.Clone());
drawingContext.DrawLine(pen, new Point(_lastPoint.X, 0), new Point(_lastPoint.X, ActualHeight));
drawingContext.PushGuidelineSet(gs.Clone());
}
示例4: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
var rect = new Rect(startPoint, currentPoint);
if (SurpassesThreshold(rect) && isDragging)
{
const double thickness = 1.0;
const double thicknessOffset = thickness / 2;
drawingContext.PushGuidelineSet(new GuidelineSet(new[] { startPoint.X - thicknessOffset, currentPoint.X - thicknessOffset }, new[] { startPoint.Y - thicknessOffset, currentPoint.Y - thicknessOffset }));
drawingContext.DrawGeometry(brush, new Pen(SystemColors.HighlightBrush, thickness), new RectangleGeometry(rect));
}
base.OnRender(drawingContext);
}
示例5: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
Pen pen = new Pen(Brushes.Gray, 1);
Pen penDash = new Pen(Brushes.Silver, 1) { DashStyle = DashStyles.Dash };
Rect rect = new Rect(20, 20, 50, 60);
double halfPenWidth = pen.Thickness / 2;
TimeSpan StartTime = TimeSpan.FromHours(6);
TimeSpan EndTime = TimeSpan.FromHours(24);
GuidelineSet guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rect.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rect.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth);
drawingContext.PushGuidelineSet(guidelines);
TimeSpan tmpTime = StartTime;
int drawHours = (int)EndTime.Subtract(StartTime).TotalHours;
double segmentHeight = this.ActualHeight / drawHours;
for (int i = 0; i < drawHours; i++)
{
int y = (int)(segmentHeight * i);
drawingContext.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
int y2 = (int)(y + segmentHeight / 2);
drawingContext.DrawLine(penDash, new Point(0, y2), new Point(this.ActualWidth, y2));
drawingContext.DrawText(new FormattedText(
tmpTime.ToString(@"h\:mm"),
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.RightToLeft,
new Typeface("Segoe UI"),
12,
Brushes.Black,
null,
TextFormattingMode.Display),new Point(this.ActualWidth, y));
tmpTime = tmpTime.Add(TimeSpan.FromHours(1));
}
TimeSpan timeNow = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second).Subtract(StartTime);
double h = segmentHeight * (timeNow.TotalHours);
drawingContext.DrawLine(new Pen(Brushes.Red, 1), new Point(0, h), new Point(this.ActualWidth, h));
drawingContext.Pop();
}
示例6: GuidelineSetBlock
public GuidelineSetBlock(
DrawingContext target,
IEnumerable<double> xGuides = null,
IEnumerable<double> yGuides = null)
{
Debug.Assert(target != null);
_target = target;
var guidelines = new GuidelineSet();
{
xGuides?.ForEach(g => guidelines.GuidelinesX.Add(g));
yGuides?.ForEach(g => guidelines.GuidelinesY.Add(g));
}
target.PushGuidelineSet(guidelines);
}
示例7: DrawColumnLines
//for linescontrol
public static void DrawColumnLines(this FrameworkElement element, DrawingContext drawingContext,
double RowWidth, double xOffset,double offset,Pen pen)
{
double halfPenHeight = pen.Thickness / 2;
Point start = new Point(0, 0);
Point end = new Point(0, element.RenderSize.Height);
GuidelineSet guidelines = new GuidelineSet();
//draw column
var count = Convert.ToInt32(element.RenderSize.Width / RowWidth);
for (int i = 1; i <= count; i++)
{
start.X = xOffset - offset;
end.X = xOffset - offset;
guidelines.GuidelinesX = new DoubleCollection(new[] { xOffset - halfPenHeight, xOffset + halfPenHeight });
drawingContext.PushGuidelineSet(guidelines);
drawingContext.DrawLine(pen, start, end);
drawingContext.Pop();
xOffset += RowWidth;
}
}
示例8: DrawPoint
public static void DrawPoint(DrawingContext dc, Point? point,string fromColor, string toColor, double duration, bool isAutoReverse)
{
SolidColorBrush scbW = new SolidColorBrush(Colors.Black);
ColorAnimation myAnimation = new ColorAnimation((Color)ColorConverter.ConvertFromString(fromColor),
(Color)ColorConverter.ConvertFromString(toColor),
new Duration(TimeSpan.FromSeconds(duration)));
myAnimation.AutoReverse = isAutoReverse;
myAnimation.RepeatBehavior = RepeatBehavior.Forever;
scbW.BeginAnimation(SolidColorBrush.ColorProperty, myAnimation);
Pen wpen = new Pen(scbW, 1);
Rect rectW = new Rect(point.Value.X, point.Value.Y, 0.1, 0.1);
double halfPenWidth = wpen.Thickness / 2;
GuidelineSet guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rectW.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rectW.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rectW.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rectW.Bottom + halfPenWidth);
dc.PushGuidelineSet(guidelines);
dc.DrawRectangle(null, wpen, rectW);
dc.Pop();
}
示例9: DrawImage
private void DrawImage(DrawingContext dc, Rect rect)
{
ImageSource source = Image;
if (source != null)
{
double opacity = ImageOpacity;
if (IsNineGrid)
{
// make sure we don't step out of borders
Thickness margin = Clamp(ImageMargin, new Size(source.Width, source.Height), rect.Size);
double[] xGuidelines = { 0, margin.Left, rect.Width - margin.Right, rect.Width };
double[] yGuidelines = { 0, margin.Top, rect.Height - margin.Bottom, rect.Height };
GuidelineSet guidelineSet = new GuidelineSet(xGuidelines, yGuidelines);
guidelineSet.Freeze();
dc.PushGuidelineSet(guidelineSet);
double[] vx = { 0D, margin.Left / source.Width, (source.Width - margin.Right) / source.Width, 1D };
double[] vy = { 0D, margin.Top / source.Height, (source.Height - margin.Bottom) / source.Height, 1D };
double[] x = { rect.Left, rect.Left + margin.Left, rect.Right - margin.Right, rect.Right };
double[] y = { rect.Top, rect.Top + margin.Top, rect.Bottom - margin.Bottom, rect.Bottom };
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
var brush = new ImageBrush(source)
{
Opacity = opacity,
Viewbox = new Rect(vx[j], vy[i], Math.Max(0D, (vx[j + 1] - vx[j])),
Math.Max(0D, (vy[i + 1] - vy[i])))
};
dc.DrawRectangle(brush, null,
new Rect(x[j], y[i], Math.Max(0D, (x[j + 1] - x[j])), Math.Max(0D, (y[i + 1] - y[i]))));
}
}
dc.Pop();
}
else
{
var brush = new ImageBrush(source) { Opacity = opacity };
dc.DrawRectangle(brush, null, rect);
}
}
}
示例10: OnRender
/// <summary>
/// Render callback.
/// </summary>
protected override void OnRender(DrawingContext drawingContext)
{
CornerRadius cornerRadius = CornerRadius;
Rect shadowBounds = new Rect(new Point(ShadowDepth, ShadowDepth),
new Size(RenderSize.Width, RenderSize.Height));
Color color = Color;
if (shadowBounds.Width > 0 && shadowBounds.Height > 0 && color.A > 0)
{
// The shadow is drawn with a dark center the size of the shadow bounds
// deflated by shadow depth on each side.
double centerWidth = shadowBounds.Right - shadowBounds.Left - 2 * ShadowDepth;
double centerHeight = shadowBounds.Bottom - shadowBounds.Top - 2 * ShadowDepth;
// Clamp corner radii to be less than 1/2 the side of the inner shadow bounds
double maxRadius = Math.Min(centerWidth * 0.5, centerHeight * 0.5);
cornerRadius.TopLeft = Math.Min(cornerRadius.TopLeft, maxRadius);
cornerRadius.TopRight = Math.Min(cornerRadius.TopRight, maxRadius);
cornerRadius.BottomLeft = Math.Min(cornerRadius.BottomLeft, maxRadius);
cornerRadius.BottomRight = Math.Min(cornerRadius.BottomRight, maxRadius);
// Get the brushes for the 9 regions
Brush[] brushes = GetBrushes(color, cornerRadius);
// Snap grid to device pixels
double centerTop = shadowBounds.Top + ShadowDepth;
double centerLeft = shadowBounds.Left + ShadowDepth;
double centerRight = shadowBounds.Right - ShadowDepth;
double centerBottom = shadowBounds.Bottom - ShadowDepth;
// Because of different corner radii there are 6 potential x (or y) lines to snap to
double[] guidelineSetX = new double[] { centerLeft,
centerLeft + cornerRadius.TopLeft,
centerRight - cornerRadius.TopRight,
centerLeft + cornerRadius.BottomLeft,
centerRight - cornerRadius.BottomRight,
centerRight};
double[] guidelineSetY = new double[] { centerTop,
centerTop + cornerRadius.TopLeft,
centerTop + cornerRadius.TopRight,
centerBottom - cornerRadius.BottomLeft,
centerBottom - cornerRadius.BottomRight,
centerBottom};
drawingContext.PushGuidelineSet(new GuidelineSet(guidelineSetX, guidelineSetY));
// The corner rectangles are drawn drawn ShadowDepth pixels bigger to
// account for the blur
cornerRadius.TopLeft = cornerRadius.TopLeft + ShadowDepth;
cornerRadius.TopRight = cornerRadius.TopRight + ShadowDepth;
cornerRadius.BottomLeft = cornerRadius.BottomLeft + ShadowDepth;
cornerRadius.BottomRight = cornerRadius.BottomRight + ShadowDepth;
// Draw Top row
Rect topLeft = new Rect(shadowBounds.Left, shadowBounds.Top, cornerRadius.TopLeft, cornerRadius.TopLeft);
drawingContext.DrawRectangle(brushes[TopLeft], null, topLeft);
double topWidth = guidelineSetX[2] - guidelineSetX[1];
if (topWidth > 0)
{
Rect top = new Rect(guidelineSetX[1], shadowBounds.Top, topWidth, ShadowDepth);
drawingContext.DrawRectangle(brushes[Top], null, top);
}
Rect topRight = new Rect(guidelineSetX[2], shadowBounds.Top, cornerRadius.TopRight, cornerRadius.TopRight);
drawingContext.DrawRectangle(brushes[TopRight], null, topRight);
// Middle row
double leftHeight = guidelineSetY[3] - guidelineSetY[1];
if (leftHeight > 0)
{
Rect left = new Rect(shadowBounds.Left, guidelineSetY[1], ShadowDepth, leftHeight);
drawingContext.DrawRectangle(brushes[Left], null, left);
}
double rightHeight = guidelineSetY[4] - guidelineSetY[2];
if (rightHeight > 0)
{
Rect right = new Rect(guidelineSetX[5], guidelineSetY[2], ShadowDepth, rightHeight);
drawingContext.DrawRectangle(brushes[Right], null, right);
}
// Bottom row
Rect bottomLeft = new Rect(shadowBounds.Left, guidelineSetY[3], cornerRadius.BottomLeft, cornerRadius.BottomLeft);
drawingContext.DrawRectangle(brushes[BottomLeft], null, bottomLeft);
double bottomWidth = guidelineSetX[4] - guidelineSetX[3];
if (bottomWidth > 0)
{
Rect bottom = new Rect(guidelineSetX[3], guidelineSetY[5], bottomWidth, ShadowDepth);
drawingContext.DrawRectangle(brushes[Bottom], null, bottom);
}
Rect bottomRight = new Rect(guidelineSetX[4], guidelineSetY[4], cornerRadius.BottomRight, cornerRadius.BottomRight);
//.........这里部分代码省略.........
示例11: OnRender
protected override void OnRender(DrawingContext drawingContext)
{
Matrix transform = Transform;
dc = drawingContext;
dc.PushGuidelineSet(SymbolControl.Guidelines);
DrawLayout(layout, dc, transform, Pen, ShowText ? FontFamily : null, FontWeight, FontSize);
Rect bounds = new Rect(T(transform, layout.LowerBound), T(transform, layout.UpperBound));
if (Selected)
dc.DrawRectangle(null, SelectedPen, bounds);
else if (Highlighted)
dc.DrawRectangle(null, HighlightPen, bounds);
dc.Pop();
dc = null;
}
示例12: DrawLayout
public static void DrawLayout(
Circuit.SymbolLayout Layout,
DrawingContext Context, Matrix Tx,
Pen Pen, FontFamily FontFamily, FontWeight FontWeight, double FontSize)
{
Context.PushGuidelineSet(Guidelines);
foreach (Circuit.SymbolLayout.Shape i in Layout.Lines)
Context.DrawLine(
Pen != null ? Pen : MapToPen(i.Edge),
T(Tx, i.x1),
T(Tx, i.x2));
foreach (Circuit.SymbolLayout.Shape i in Layout.Rectangles)
Context.DrawRectangle(
(i.Fill && Pen == null) ? MapToBrush(i.Edge) : null,
Pen != null ? Pen : MapToPen(i.Edge),
new Rect(T(Tx, i.x1), T(Tx, i.x2)));
foreach (Circuit.SymbolLayout.Shape i in Layout.Ellipses)
{
Brush brush = (i.Fill && Pen == null) ? MapToBrush(i.Edge) : null;
Pen pen = Pen != null ? Pen : MapToPen(i.Edge);
Point p1 = T(Tx, i.x1);
Point p2 = T(Tx, i.x2);
Context.DrawEllipse(
brush, pen,
new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2), (p2.X - p1.X) / 2, (p2.Y - p1.Y) / 2);
}
foreach (Circuit.SymbolLayout.Curve i in Layout.Curves)
{
IEnumerator<Circuit.Point> e = i.x.AsEnumerable().GetEnumerator();
if (!e.MoveNext())
return;
Pen pen = Pen != null ? Pen : MapToPen(i.Edge);
Point x1 = T(Tx, e.Current);
while (e.MoveNext())
{
Point x2 = T(Tx, e.Current);
Context.DrawLine(pen, x1, x2);
x1 = x2;
}
}
if (FontFamily != null)
{
// Not sure if this matrix has row or column vectors... want the y axis scaling here.
double scale = Math.Sqrt(Tx.M11 * Tx.M11 + Tx.M21 * Tx.M21);
foreach (Circuit.SymbolLayout.Text i in Layout.Texts)
{
double size;
switch (i.Size)
{
case Circuit.Size.Small: size = 0.5; break;
case Circuit.Size.Large: size = 1.5; break;
default: size = 1.0; break;
}
FormattedText text = new FormattedText(
i.String,
CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyles.Normal, FontWeight, FontStretches.Normal), FontSize * scale * size,
Brushes.Black);
Point p = T(Tx, i.x);
Vector p1 = T(Tx, new Circuit.Point(i.x.x - MapAlignment(i.HorizontalAlign), i.x.y + (1 - MapAlignment(i.VerticalAlign)))) - p;
Vector p2 = T(Tx, new Circuit.Point(i.x.x - (1 - MapAlignment(i.HorizontalAlign)), i.x.y + MapAlignment(i.VerticalAlign))) - p;
p1.X *= text.Width; p2.X *= text.Width;
p1.Y *= text.Height; p2.Y *= text.Height;
Rect rc = new Rect(
Math.Min(p.X + p1.X, p.X - p2.X),
Math.Min(p.Y + p1.Y, p.Y - p2.Y),
text.Width,
text.Height);
if (TextOutline != null)
Context.DrawRectangle(null, TextOutline, rc);
Context.DrawText(text, rc.TopLeft);
}
}
foreach (Circuit.Terminal i in Layout.Terminals)
DrawTerminal(Context, T(Tx, Layout.MapTerminal(i)), i.ConnectedTo != null);
Context.Pop();
}
示例13: OnRender
/// <summary>
/// OnRender method.
/// </summary>
/// <param name="drawingContext">Drawing context.</param>
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (TimeAxis.ZoomMode == ZoomMode.FrameLevel)
{
GuidelineSet gs = new GuidelineSet();
Pen pen = new Pen(Brushes.LightGray, 1);
double halfPen = pen.Thickness / 2;
// draw time line for each frame
for (int i = 0; i <= ActualWidth / ViewHelper.TimespanToPixel(TimeAxis.SampleInterval, TimeAxis.ZoomScale); ++i)
{
double x = ViewHelper.TimespanToPixel(TimeAxis.SampleInterval * (double)i, TimeAxis.ZoomScale);
gs.GuidelinesX.Clear();
gs.GuidelinesX.Add(x - halfPen);
gs.GuidelinesX.Add(x + halfPen);
drawingContext.PushGuidelineSet(gs.Clone());
drawingContext.DrawLine(pen, new Point(x, 0), new Point(x, Height));
drawingContext.Pop();
}
}
}
示例14: GuidelineSet
DrawPixelAlignedRectangle
(
DrawingContext drawingContext,
Brush brush,
Pen pen,
Rect rect
)
{
Debug.Assert(drawingContext != null);
Debug.Assert(pen != null);
// This technique was described at "Draw lines excactly on physical
// device pixels," by Christian Mosers, at
// http://wpftutorial.net/DrawOnPhysicalDevicePixels.html.
Double dHalfPenWidth = pen.Thickness / 2.0;
GuidelineSet oGuidelineSet = new GuidelineSet();
DoubleCollection oGuidelinesX = oGuidelineSet.GuidelinesX;
DoubleCollection oGuidelinesY = oGuidelineSet.GuidelinesY;
oGuidelinesX.Add(rect.Left + dHalfPenWidth);
oGuidelinesX.Add(rect.Right + dHalfPenWidth);
oGuidelinesY.Add(rect.Top + dHalfPenWidth);
oGuidelinesY.Add(rect.Bottom + dHalfPenWidth);
drawingContext.PushGuidelineSet(oGuidelineSet);
drawingContext.DrawRectangle(brush, pen, rect);
drawingContext.Pop();
}
示例15: OnRender
protected override void OnRender(DrawingContext ctx)
{
base.OnRender(ctx);
if (ctx != null)
{
// only solid brush is supported now
var brush = (IsContentActive ? ActiveBorderBrush : InactiveBorderBrush) as SolidColorBrush;
if (brush != null)
{
Rect rClient = new Rect(0, 0, this.ActualWidth, this.ActualHeight);
var thick = BorderThickness;
var clientW = rClient.Width - thick.Right - thick.Left;// -1;
var clientH = rClient.Height - thick.Top - thick.Bottom;// -1;
if (clientW > 1 && clientH > 1)
{
rClient.X += thick.Left;
rClient.Y += thick.Top;
rClient.Width = clientW;
rClient.Height = clientH;
var rTop = new Rect(rClient.Left, 0, rClient.Width, thick.Top);
var rTopLeft = new Rect(0, 0, thick.Left, thick.Top);
var rTopRight = new Rect(rClient.Right, 0, thick.Right, thick.Top);
var rBottom = new Rect(rClient.Left, rClient.Bottom, rClient.Width, thick.Bottom);
var rBottomLeft = new Rect(0, rClient.Bottom, thick.Left, thick.Bottom);
var rBottomRight = new Rect(rClient.Right, rClient.Bottom, thick.Right, thick.Bottom);
var rLeft = new Rect(0, rClient.Top, thick.Left, rClient.Height);
var rRight = new Rect(rClient.Right, rClient.Top, thick.Right, rClient.Height);
var brushes = GetShadowBrushes(brush.Color);//, (thick.Top + thick.Bottom + thick.Right + thick.Left) / 4);
ctx.DrawRectangle(brushes[(int)BorderSide.TopLeft], null, rTopLeft);
ctx.DrawRectangle(brushes[(int)BorderSide.TopRight], null, rTopRight);
ctx.DrawRectangle(brushes[(int)BorderSide.Top], null, rTop);
ctx.DrawRectangle(brushes[(int)BorderSide.BottomLeft], null, rBottomLeft);
ctx.DrawRectangle(brushes[(int)BorderSide.BottomRight], null, rBottomRight);
ctx.DrawRectangle(brushes[(int)BorderSide.Bottom], null, rBottom);
ctx.DrawRectangle(brushes[(int)BorderSide.Left], null, rLeft);
ctx.DrawRectangle(brushes[(int)BorderSide.Right], null, rRight);
Pen borderPen = new Pen(brush, 1);
// from http://wpftutorial.net/DrawOnPhysicalDevicePixels.html
double halfPenWidth = borderPen.Thickness / 2;
// Create a guidelines set
GuidelineSet guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rClient.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rClient.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rClient.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rClient.Bottom + halfPenWidth);
ctx.PushGuidelineSet(guidelines);
rClient.X -= 1;
rClient.Y -= 1;
rClient.Width += 1;
rClient.Height += 1;
ctx.DrawRectangle(null, borderPen, rClient);
//ctx.DrawRectangle(null, new Pen(Brushes.Red, 1), rect);
}
}
}
}