2016-07-12 2 views
0

마우스가 사각형을 클릭했는지 어떻게 확인합니까?사각형을 만들었습니다. 마우스가 클릭되었는지 어떻게 확인합니까?

Graphics gfx; 
Rectangle hitbox; 
hitbox = new hitbox(50,50,10,10); 
//TIMER AT THE BOTTOM 
gfx.Draw(System.Drawing.Pens.Black,hitbox); 
+0

'당신은 무엇을 참조 gfx'는 무엇입니까? – Tommy

+0

"gfx"가 양식의 "e.Graphics ..."인 경우 이벤트 MouseDown을 사용하면 e.X와 e.Y가 있습니다. –

+0

'control.CreateGraphics'를 절대로 사용하지 마십시오! 'Graphics' 객체를 캐쉬하지 마라! 'Graphics g = Graphics.FromImage (bmp)'를 사용하여'Bitmap bmp'로 그리거나'e.Graphics' 매개 변수를 사용하여 컨트롤의'Paint' 이벤트에서 그립니다. – TaW

답변

2

그냥 샘플 신속하고 더러운, 귀하의 "GFX는"양식에서 "e.Graphics을 ..."인 경우 :

public partial class Form1 : Form 
    { 
    private readonly Rectangle hitbox = new Rectangle(50, 50, 10, 10); 
    private readonly Pen pen = new Pen(Brushes.Black); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.DrawRectangle(pen, hitbox); 
    } 

    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if ((e.X > hitbox.X) && (e.X < hitbox.X + hitbox.Width) && 
      (e.Y > hitbox.Y) && (e.Y < hitbox.Y + hitbox.Height)) 
     { 
     Text = "HIT"; 
     } 
     else 
     { 
     Text = "NO"; 
     } 
    } 
    } 
+1

thanks! 친구! 너는 많은 도움을 주었다. – Kill4lyfe

0

Rectangle 여러 편리하지만 종종 간과 기능이 있습니다. 당신이 을 결정하는 것이 좋습니다 당신이 개요를 클릭하면 사용자가 쉽게 하나의 히트 수 없기 때문에

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (hitbox.Contains(e.Location)) .. // clicked inside 
} 

가 결정하려면 다음 Rectangle.Contains(Point) 기능을 사용하여이 경우 가장 좋은 솔루션입니다 픽셀. 이를 위해

사용할 수 있습니다 GraphicsPath.IsOutlineVisible(Point) ...

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    GraphicsPath gp = new GraphicsPath(); 
    gp.AddRectanle(hitbox); 
    using (Pen pen = new Pen(Color.Black, 2f)) 
     if (gp.IsOutlineVisible(e.location), pen) .. // clicked on outline 

} 

.. 또는 사각형에 충실 .. :

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    Rectangle inner = hitbox; 
    Rectangle outer = hitbox; 
    inner.Inflate(-1, -1); // a two pixel 
    outer.Inflate(1, 1); // ..outline 

    if (outer.Contains(e.Location) && !innerContains(e.Location)) .. // clicked on outline 
} 
관련 문제