C#画出问题

时间:2014-08-31 17:55:07

标签: c# draw

当我尝试在屏幕上绘制矩形或其他内容时出现问题。在这段代码中,我希望在坐标x = 0和y =屏幕的一半上绘制一个矩形,但它不在屏幕的中心。

谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Graphics gr;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            gr = this.CreateGraphics();

            Pen p = new Pen(new SolidBrush(Color.Black),1);

            gr.DrawRectangle(p, 0, this.Height / 2 - 50, 100, 100);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于

  

this.Height

返回标题栏高度的表单高度。要绘制具有下一坐标的矩形:x = 0,y =屏幕的一半,您可以使用下一个代码:

    protected void Form1_Paint(object sender, PaintEventArgs e)
    {
       gr = this.CreateGraphics();
       Pen p = new Pen(new SolidBrush(Color.Black), 1);
       gr.DrawRectangle(p, 0, ClientRectangle.Height / 2 - 50, 100, 100);
    }