【发布时间】:2014-06-22 08:29:20
【问题描述】:
我覆盖了边界控制并在我覆盖的 OnRender 中执行:
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
this.SnapsToDevicePixels = true;
this.VisualEdgeMode = EdgeMode.Aliased;
var myPen = new Pen(new SolidColorBrush(Colors.LightGray), 1);
dc.DrawLine(myPen, new Point(1, 1), new Point(1, RenderSize.Height - 1));
return;
这给了我这个结果:
问题:
有谁能告诉我为什么我的代码画了一条从 (0,1) 开始的线,而它假设从 (1, 1) 开始,就像用代码写的那样?
我的 DPI 是 96、96。
对于 ref,这是我的 xaml:
<Window xmlns:MyControls="clr-namespace:MyControls;assembly=MyControls" x:Class="TestControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<MyControls:Border3D BorderThickness="3" BorderBrush="Aqua">
<Rectangle Width="74" Height="3" HorizontalAlignment="Left">
<Rectangle.Fill>
<SolidColorBrush Color="#40FF0000">
</SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</MyControls:Border3D>
<Rectangle Grid.Row="1" Grid.Column="0" Width="80" Grid.ColumnSpan="2" HorizontalAlignment="Left">
<Rectangle.Fill>
<SolidColorBrush Color="LightGray"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
【问题讨论】:
-
如果将宽度更改为 526,还会出现这种情况吗?
-
看来-我猜-您没有计算边界线..所以当您尝试绘制 (1,1) 时,您会在屏幕上看到 (0,1).. 试试这个: new Point (1+border.TopLine.Height, 1+border.LeftWall.width) 注意:给定的代码是 PSEUDO,所以在 .NET 中可能找不到同名的方法/字段
-
谢谢。克莱门斯似乎对我的回答有确切的答案,我会说一点很好的额外解释:-)!
标签: c# wpf drawing line render