首先,大家来做个选择题:
首先新建一个窗体,把FormBorderStyle设为None
我需要先画一条宽度为10,长度为100的竖线,x坐标为0,y坐标为0。
画完竖线,我先填充一个宽度为10长度为100的方块,要紧挨着刚才那条竖线。代码如下:        private void test_Paint(object sender, PaintEventArgs e)
        {
            Graphics gp = this.CreateGraphics();            //画线
            Pen p = new Pen(Color.Black, 10);
            gp.DrawLine(p, 0, 0, 0, 100);
            //画方框
            SolidBrush sb = new SolidBrush(Color.Red);
            //int x = 10;
            int x = 0;
            gp.FillRectangle(sb, x, 0, 10, 100);
        }
请选择,上面的画方框的x坐标应该是:
A:x=10
B:x=0我的答案:上面两个都不对!运行结果图片:
各位,请解释一下吧

解决方案 »

  1.   

    这个不是bug
    10是线宽,而线起始点是从中心算的,
    你从(0,0)点画一条宽度是10的线,
    等于告诉它(0,0)点是起始的中心点,然后往左往右各延伸10/2=5个像素
    ,所以你画的线其实只显示了一半的宽度
    x=10/2就紧挨着了
                Graphics gp = this.CreateGraphics();            //画线
                Pen p = new Pen(Color.Black, 10);
                gp.DrawLine(p, 0, 0, 0, 100);
                //画方框
                SolidBrush sb = new SolidBrush(Color.Red);
                int x = 10/2;
                gp.FillRectangle(sb, x, 0, 10, 100);
      

  2.   

    我宁愿相信是我代码的问题,我也不相信是Graphics的BUG。
    别一出问题就说别人的有BUG。