HOW TO:判斷 ImageButton Web 伺服器控制項中的座標
更新:2007 年 11 月
當使用者按一下 ImageButton 控制項時,有一參數會傳遞至該控制項之 Click 事件的事件處理常式,此參數包含的座標會指出使用者所按的位置。這可讓您根據使用者所按的位置執行不同的工作。
![]() |
---|
如果要將影像的特定範圍定義為使用者可按的區域,也可以使用 ImageMap 控制項。 |
座標資訊會當做在 ImageButton 控制項中 Click 事件的事件引數物件來傳送。
若要判斷使用者所按位置的座標
為 ImageButton 控制項的 Click 事件建立事件處理常式。此方法的事件引數物件必須是 ImageClickEventArgs 型別。
在 Click 事件處理常式中,取得 ImageClickEventArgs 引數物件的 X 和 Y 屬性。
x 及 y 座標會以像素表示,影像的左上角是 (0,0)。
下列範例將說明如何判斷使用者在 100x100 像素圖案中所按的位置。此程式碼會取得使用者所按位置的 x 及 y 座標。然後將它與預先決定的值比較,檢查使用者是否按在特定的象限上。結果會在 Label 控制項中顯示。
Protected Sub ImageButton1_Click(ByVal sender As System.Object, _ ByVal e As System.Web.UI.ImageClickEventArgs) _ Handles ImageButton1.Click Dim msg as String = "" Dim x As Integer = e.X Dim y As Integer = e.Y ' The button graphic is assumed to be 100x100 pixels. ' This checks coordinates against predetermined values that ' make up quadrants of the picture. If x >= 50 And y >= 50 Then msg = "Southeast" ElseIf x >= 50 And y < 50 Then msg = "Northeast" ElseIf x < 50 And y >= 50 Then msg = "Southwest" ElseIf x < 50 And y < 50 Then msg = "Northwest" End If Label1.Text = msg End Sub
protected void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string msg = ""; int x = e.X; int y = e.Y; // The button graphic is assumed to be 100x100 pixels. // This checks coordinates against predetermined values that // make up quadrants of the picture. if(x >= 50 && y >= 50) { msg = "Southeast"; } else if(x >=50 && y < 50) { msg = "Northeast"; } else if(x < 50 && y >= 50) { msg = "Southwest"; } else if(x < 50 && y < 50) { msg = "Northwest"; } Label1.Text = msg; }