2014-08-31 56 views
-3

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

谢谢!

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); 
     } 
    } 
} 
+0

所以你的问题是????? – Bas 2014-08-31 18:05:42

回答

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); 
    } 
+0

非常感谢! – 2014-08-31 18:18:21

+0

不客气,不要忘记将答案标记为解决问题的正确答案。 – ntl 2014-08-31 18:19:53