C # simulates mouse writing

Source: Internet
Author: User

Often see others take a boxy writing pad with a pen, in C # can actually be achieved, the general idea is to deal with the mouse coordinates and graphics,

The code is as follows:

Declare two global variables first

BOOL Ismousedown = false;        Point point = new Point ();


Re-overriding the MouseDown event:

protected override void OnMouseDown (MouseEventArgs e)        {point            = e.location;//Current coordinates            Ismousedown = true;// Indicates that the mouse has been pressed        }


Because when you write, you need to move around, you also rewrite the move event:

protected override void OnMouseMove (MouseEventArgs e)        {            if (Ismousedown)            {                using (Graphics G = this. CreateGraphics ())                {                    g.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;                    using (Pen pen=new pen (color.black,3f))//Brush                    {                        g.drawline (pen, point, e.location);//start drawing line point                        = e. location;//Update Coordinates}}}}        


As a final step, rewrite the MouseUp event to:

protected override void OnMouseUp (MouseEventArgs e)        {            Ismousedown = false;        }


As follows: initial interface

Interface for writing

C # simulates mouse writing

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.