Implementation of mouse box selection in WinForm c#__c#

Source: Internet
Author: User

You need to implement a mouse-box-selected feature in your project, now share your code:

Train of thought: realize three mouse event mousedown,mousemove,mouseup, as to what function needs to implement after box is selected, can call in MouseUp

The complete code is as follows:

Create a new WinForm form program that modifies the behindcode of the form.


Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Windows.Forms;  namespace Winfrmmouseframeselect {public partial class Mframselctdemo:form {private bool Mouseisdown =
        False

        Private Rectangle mouserect = Rectangle.empty;
        Public Mframselctdemo () {InitializeComponent (); } private void Mframselctdemo_mousedown (object sender, MouseEventArgs e) {Mouseisdown = True
            ;
        Drawstart (e.location);
                } private void Mframselctdemo_mousemove (object sender, MouseEventArgs e) {if (Mouseisdown)
        Resizetorectangle (e.location);
            } private void Mframselctdemo_mouseup (object sender, MouseEventArgs e) {Capture = false;
         Cursor.clip = Rectangle.empty;   Mouseisdown = false;
            DrawRectangle ();
        Mouserect = Rectangle.empty; ///<summary>///initialization selection box///</summary>///<param name= "StartPoint" >& lt;/param> private void Drawstart (point startpoint) {this.
            Capture = true;
            Specifies that the workspace is the entire control Cursor.clip = Rectangletoscreen (new Rectangle (0, 0, clientsize.width, clientsize.height));
        Mouserect = new Rectangle (startpoint.x, startpoint.y, 0, 0); ///<summary>///Changes the size of the selection box when the mouse moves///</summary>///<param name= "P" > Mice
            The marked position </param> private void Resizetorectangle (point P) {DrawRectangle ();
            Mouserect.width = P.x-mouserect.left;
            Mouserect.height = P.y-mouserect.top;
        DrawRectangle (); ///<summary>///Draw selection box///</summary> private void DrawRectangle () {Rectangle rect = Rectangletoscreen (mouserect);
        Controlpaint.drawreversibleframe (Rect, Color.White, framestyle.dashed);
 }
    }
}


Related Article

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.