2009-08-26 106 views

回答

1

您使用MouseEventArgs.X和MouseEventArgs.Y,看看他們是否是座標平面內。

這個答案是一個點擊遠離我張貼在我的回答對你前面的問題的鏈接。

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs_members.aspx

新增

情景: 我有一個矩形形狀的區域我想處理點擊在

形狀的左上角是在位置28,83(左,上)

大小因此,如果位置X(左邊在28和28 + 225(253)之間,並且位置Y在83和83之間+ 52(135)在邊界內,則爲225,52(寬度,高度)

代碼例如:

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

namespace WindowsFormsApplication4 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.MouseClick += new MouseEventHandler(Form1_MouseClick); 
     } 

     void Form1_MouseClick(object sender, MouseEventArgs e) 
     { 
      if (e.X >= 28 && e.X <= 253 && e.Y >= 83 && e.Y <= 135) 
      { 
       MessageBox.Show("Clicked within the rectangle"); 
      } 
      else 
      { 
       MessageBox.Show("Clicked outside the rectangle"); 
      } 
     } 
    } 
} 
+0

請給一個代碼示例 – 2009-08-26 20:33:35

+0

哎大衛對不起,你能做vb.net嗎? – 2009-08-26 20:56:23

1

用於標記開始座標100100 100像素的正方形區域VB代碼。 (設定你自己的數值。)

Private Sub frm_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick 

     Dim x As Integer = e.Location.X 
     Dim y As Integer = e.Location.Y 

     If x > 100 AndAlso x < 200 AndAlso y > 100 AndAlso y < 200 Then 
     MessageBox.Show("Inside") 
     Else 
     MessageBox.Show("Outside") 
     End If 

    End Sub 

當然,如果它碰到窗體表面,這隻會捕獲鼠標點擊。如果你點擊窗體上的某個控件,你將不得不考慮你想要做什麼。

+0

或者你可以使用Dim x As Integer = e.X(我的intellisense窗口很小,我首先看到了Location屬性!) – Bill 2009-08-27 09:51:36