(改進)UserControl的單擊事件對滑鼠左右鍵都有效,怎樣使之像按鈕控制項那樣只對滑鼠左鍵敏感?

來源:互聯網
上載者:User

UserControl的單擊事件對滑鼠左右鍵都有效,怎樣使之像按鈕控制項那樣只對滑鼠左鍵敏感?(改進)

在類的外面聲明這個委託:

public delegate void MouseClickEvent(object sender, MouseEventArgs e);

在類裡面重寫一些方法:

/// <summary>

/// 標記滑鼠是否經曆了按下移動抬起的過程。

/// </summary>

private bool _isMouseLeftButtonDownMoveUp = false;

protected override void OnMouseDown(MouseEventArgs e)

{

this._isMouseLeftButtonDownMoveUp = false;

base.OnMouseDown (e);

}

protected override void OnMouseMove(MouseEventArgs e)

{

if(e.Button == MouseButtons.Left)

{

this._isMouseLeftButtonDownMoveUp = true;

}

base.OnMouseMove (e);

}

protected override void OnMouseUp(MouseEventArgs e)

{

base.OnMouseUp (e);

if(!(this.AllowDragMove & this._isMouseLeftButtonDownMoveUp))

{

//如果被單擊,而且位置還在控制項範圍之內,而且單擊的是左鍵

if(e.Clicks == 1 & this.InShell(e.X, e.Y) & e.Button == MouseButtons.Left)

{

//觸發MouseClick事件

this.MouseClick(this, e);

}

}

this._isMouseLeftButtonDownMoveUp = false;

}

private bool InBounds(int x, int y)

{

Point p = this.PointToScreen(new Point(x, y));

Point p1 = this.PointToScreen(new Point(0, 0));

Point p2 = this.PointToScreen(new Point(this.Width, this.Height));

if(p.X < p1.X)

{

return false;

}

if(p.X > p2.X)

{

return false;

}

if(p.Y < p1.Y)

{

return false;

}

if(p.Y > p2.Y)

{

return false;

}

return true;

}

public event MouseClickEvent MouseClick;

private void Shell_MouseClick(object sender, MouseEventArgs e)

{

//做一些需要的事情

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.