Popular Posts

Mar 17, 2011

Drawing (C#.NET)


Long time ago I shared this with my google group… just for a fun.
To make this take a windows application form.There will be a windows form named Form1.cs.
and  Form1.Designer.cs with Form1.cs.
Open Form1.Designer.cs  and expand Windows Form Designer generated code with (+) sign.
After expanding it looks like this:

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(379, 312);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

            //Add this three lines

            this.MouseUp += new System.Windows.Forms.MouseEventHandler
(this.form1_MouseUp);
            this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseDown);
            this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);

            //End

        }

        #endregion


Add this three lines to InitializeComponent.
            this.MouseUp += new System.Windows.Forms.MouseEventHandler
(this.form1_MouseUp);
            this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseDown);
            this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);

Then open Form1.cs file and Type these lines.
        private bool shouldPoint = false;
        public Form1()
        {
            InitializeComponent();

            this.MouseUp += new System.Windows.Forms.MouseEventHandler
(this.form1_MouseUp);
            this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseDown);
            this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.form1_MouseMove);
        }

        private void form1_MouseDown(object sender, MouseEventArgs e)
        {
            shouldPoint = true;
        }

        private void form1_MouseUp(object sender, MouseEventArgs e)
        {
            shouldPoint = false;
        }

        private void form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (shouldPoint)
            {
                Graphics graphics = CreateGraphics();
                graphics.FillEllipse(new SolidBrush(Color.Red),
e.X, e.Y, 6, 6);
                graphics.Dispose();

            }
        }

Now run the application.Move the cursor and press the left buttom….YOU CAN DRAW!! 


No comments:

Post a Comment