All pastes #69364 Raw Edit

v

public text v1 · immutable
#69364 ·published 2006-06-22 20:46 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private Rectangle RcDraw;
        private float PenWidth = 5;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Console.WriteLine("Mouse X: {0}", e.X);
            Console.WriteLine("Mouse Y: {0}", e.Y);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // Determine the initial rectangle coordinates...

            RcDraw.X = e.X;
            RcDraw.Y = e.Y;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            // Determine the width and height of the rectangle...

            if (e.X < RcDraw.X)
            {
                RcDraw.Width = RcDraw.X - e.X;
                RcDraw.X = e.X;
            }
            else
            {
                RcDraw.Width = e.X - RcDraw.X;
            }

            if (e.Y < RcDraw.Y)
            {
                RcDraw.Height = RcDraw.Y - e.Y;
                RcDraw.Y = e.Y;
            }
            else
            {
                RcDraw.Height = e.Y - RcDraw.Y;
            }

            // Force a repaint of the region occupied by the rectangle...

            this.Invalidate(RcDraw);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Draw the rectangle...

            e.Graphics.DrawRectangle(new Pen(Color.Blue, PenWidth), RcDraw);
        }
    }
}