做:
MouseClick += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseClick);
現在在UserControl1_MouseClick
事件做:
private void UserControl1_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X;
int y = e.Y;
}
現在讓我們來劃分用戶控制到10×10面積:
int xIdx = x/(Width/10);
int yIdx = y/(Height/10);
ClickOnArea(xIdx, yIdx);
在ClickOnArea
方法你只需要決定在每個領域做什麼。也許使用的Action
一個二維數組,作爲邊境做到這一點:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black);
float xIdx = (float)(Width/10.0);
float yIdx = (float)(Height/10.0);
for (int i = 0; i < 10; i++)
{
float currVal = yIdx*i;
g.DrawLine(p, 0, currVal, Width, currVal);
}
g.DrawLine(p, 0, Height - 1, Width, Height - 1);
for (int j = 0; j < 10; j++)
{
float currVal = xIdx * j;
g.DrawLine(p, currVal, 0, currVal, Height);
}
g.DrawLine(p, Width - 1, 0, Width - 1, Height);
}
你必須知道何時停止使用設計,並開始編寫一些代碼。這是這種情況。只需重寫OnMouseUp()方法並計算從e.Location屬性中單擊的「區域」。通過重寫OnPaint()來繪製邊框。 –