using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication10
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private bool bMove=false;
private System.Windows.Forms.Button button1;
private const int R2_NOTXORPEN = 10;
private const int PS_DASHDOT = 10;
private Graphics m_g;
private IntPtr m_hDC;
private long InvertPen;
private long OldPen;
private int ordx=0;
private int ordy=0;
private int ordx1=0;
private int ordy1=0;
[DllImport("GDI32.DLL")]
public static extern int SetROP2(IntPtr hdc,int nDrawMode);
[DllImport("GDI32.DLL")]
public static extern long SelectObject(IntPtr hdc,long hgdiobj );
[DllImport("GDI32.DLL")]
public static extern long CreatePen(int fnPenStyle,int nWidth,int crColor);
[DllImport("GDI32.DLL")]
public static extern bool Ellipse(IntPtr hdc,int nLeftRect,int nTopRect,
int nRightRect,int nBottomRect);
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=true;
m_g=this.CreateGraphics();
m_hDC=m_g.GetHdc();
ordx=e.X;
ordy=e.Y;
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
pt1.X=e.X;
pt1.Y=e.Y;
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=false;
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (bMove)
{
DrawSelectRect(ordx,ordy,ordx1,ordy1);
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
}
}
private void DrawSelectRect(int x,int y,int x1,int y1)
{
InvertPen=CreatePen(PS_DASHDOT,1,Color.Red.ToArgb());
OldPen=SelectObject(m_hDC,InvertPen);
int nOldDrawMode=SetROP2(m_hDC,R2_NOTXORPEN);
Ellipse(m_hDC,x,y,x1,y1);
SetROP2(m_hDC,nOldDrawMode);
}
}
}