Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

c# - Why my coordinate (1,1) start at (0, 1)?

I overriden Border control and in my overriden OnRender I do:

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;

Which give me this result: enter image description here

Question:

Is anybody can tell me why my code draw a line that start at (0,1) while it is suppose to start at (1, 1) like written in code ?

My DPI are 96, 96.

For ref, this is my 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Keep in mind that (0, 0) is not the center of the upper left pixel. Instead it is the upper left corner of that pixel. In order to draw a line with a stroke thickness of 1 in the middle of the second pixel column (with index 1) you would have to draw from (1.5, 1) to (1.5, RenderSize.Height - 1):

dc.DrawLine(myPen, new Point(1.5, 1), new Point(1.5, RenderSize.Height - 1));

Setting SnapsToDevicePixels = true made your line snap to the left by half a pixel.


If you would use PenLineCap.Square for both the StartLineCap and EndLineCap properties of the line's Pen, you could draw from exactly one pixel center to the other:

var myPen = new Pen(Brushes.LightGray, 1);
myPen.StartLineCap = PenLineCap.Square;
myPen.EndLineCap = PenLineCap.Square;
dc.DrawLine(myPen, new Point(1.5, 1.5), new Point(1.5, RenderSize.Height - 1.5));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...