(Improvement) The Click Event of usercontrol is valid for both the left and right mouse keys. How does one make it as sensitive to the left mouse button as the button control?

Source: Internet
Author: User

The click event of usercontrol is valid for both the left and right mouse keys. How does one make it as sensitive as the left mouse button control? (Improvement)

Declare this delegate outside the class:

Public Delegate void mouseclickevent (Object sender, mouseeventargs E );

Rewrite some methods in the class:

/// <Summary>

/// Indicates whether the mouse has been lifted by pressing the mouse.

/// </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 it is clicked and its position is still within the control range, the left button is clicked.

If (E. clicks = 1 & this. Inshell (E. X, E. Y) & E. Button = mousebuttons. Left)

{

// Trigger the mouseclick event

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)

{

// Do something you need

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.