using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication27 { public partial class Form1 : Form { Point StartPoint; public Form1() { InitializeComponent(); this.MouseMove += new MouseEventHandler(Form1_MouseMove); this.MouseUp+=new MouseEventHandler(Form1_MouseUp); } void Form1_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) StartPoint = e.Location; else using (Graphics G = this.CreateGraphics()) { Point Start = StartPoint; Point End = e.Location; int Width = e.X - Start.X; int Height = e.Y - Start.Y; if (Width < 0) { Start.X += Width; End.X -= Width; } if (Height < 0) { Start.Y += Height; End.Y -= Height; } G.Clear(this.BackColor);
ControlPaint.DrawFocusRectangle(G, new Rectangle(Start, new Size(Math.Abs(Width), Math.Abs(Height)))); } } void Form1_MouseUp(object sender, MouseEventArgs e) {
private Point m_CurPoint;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.m_DownPoint = this.PointToScreen(e.Location);
this.m_CurPoint = this.PointToScreen(e.Location);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Capture)
{
Point curPoint = this.PointToScreen(e.Location);
ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, curPoint.X, curPoint.Y), Color.Yellow, FrameStyle.Dashed);
this.m_CurPoint = curPoint;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
}
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication27
{
public partial class Form1 : Form
{
Point StartPoint; public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
this.MouseUp+=new MouseEventHandler(Form1_MouseUp);
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
StartPoint = e.Location;
else using (Graphics G = this.CreateGraphics())
{
Point Start = StartPoint;
Point End = e.Location;
int Width = e.X - Start.X;
int Height = e.Y - Start.Y; if (Width < 0)
{
Start.X += Width;
End.X -= Width;
}
if (Height < 0)
{
Start.Y += Height;
End.Y -= Height;
} G.Clear(this.BackColor);
ControlPaint.DrawFocusRectangle(G, new Rectangle(Start, new Size(Math.Abs(Width), Math.Abs(Height))));
}
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
}
}