Scribble with Mouse : Graphics « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » GraphicsScreenshots 
Scribble with Mouse
Scribble with Mouse
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class frmScribble : System.Windows.Forms.Form {
    private System.ComponentModel.Container components = null;
    private bool mouseDown = false;
    private Point lastPoint = Point.Empty;
    private string color = "black";
    private Graphics g;
    private Pen p;

    public frmScribble() {
        g = CreateGraphics();
        p = new Pen(Color.FromName(color));

    }
    protected override void OnMouseDown(MouseEventArgs e) {
        mouseDown = true;
        if (e.Button == MouseButtons.Right) {
            ContextMenu m = new ContextMenu();
            m.MenuItems.Add(0new MenuItem("black"new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(1new MenuItem("white"new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(2new MenuItem("red"new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(3new MenuItem("green"new EventHandler(RightMouseButton_Click)));
            m.MenuItems.Add(4new MenuItem("blue"new EventHandler(RightMouseButton_Click)));
            m.Show(this, new Point(e.X, e.Y));
        }
    }

    protected void RightMouseButton_Click(object sender, EventArgs e) {
        color = ((MenuItem)sender).Text;
        p = new Pen(Color.FromName(color));
    }

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

    protected override void OnMouseMove(MouseEventArgs e) {
        if (lastPoint.Equals(Point.Empty)) lastPoint = new Point(e.X, e.Y);
        if (mouseDown) {
            Point pMousePos = new Point(e.X, e.Y);
            g.DrawLine(p, pMousePos, lastPoint);
        }
        lastPoint = new Point(e.X, e.Y);
    }

    [STAThread]
    static void Main() {
        Application.Run(new frmScribble());
    }
}

 
Related examples in the same category
1.Graphics: TranslateClip
2.Graphics: SetClip
3.FillPolygon: Alternate and Winding Fill Modes
4.One-Inch Ellipse
5.Graphics: FillRectangles
6.Graphics: FillRectangle
7.Graphics: FillEllipse
8.Wide Polyline: DrawLines
9.Line and Arc Combo
10.Graphics: FillClosedCurve
11.Bezier Art: DrawBeziers
12.Graphics: MeasureString
13.Clear a Graphics
14.Graphics: DrawRectangles
15.Graphics: DrawRectangle
16.Draw Ellipse
17.Graphics: DrawArc
18.Graphics: DrawPie
19.Draw a Rectangle
20.Draw an Image
21.FillEllipse: Red Traffic Light
22.Graphics: Draw an Icon
23.Graphics: DrawString
24.Use different Font object to draw a line of text
25.Graphics: ScaleTransform
26.Graphics: Transform
27.Graphics: TranslateTransform
28.Graphics: Pen Alignment
29.Graphics: PageScale
30.Graphics: PageUnit
31.DpiY and DpiX
32.PixelOffsetMode
33.Paint along points in a list pointsPaint along points in a list points
34.Picture Cube: DrawImage
35.Clipping: ResetClip
36.Rotate the text 45 degrees clockwise then Translate the text 150 pixels horizontally
37.Transform and RotateTransform in a loop
38.Apply the scaling transformation(scale subsequent operations by 2x horizontally and 3x vertically)
39.Translate the text 150 pixels horizontally and 75 vertically
40.Reset the transformation Translate by 30 pixels in vertical direction
41.Rotate the text through 45 degrees
42.Fill the area 'bounded' by the path
43.DrawImage with destination points
44.DrawImage with size
45.Graphics.SmoothingMode
46.DrawPie with offset
47.TransformPoints
48.Fill a Rectangle with TextureBrush
49.Fill a Rectangle with LinearGradientBrush
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.