public class CReportVIEw : System.Windows.Forms.Form
{
...
private void InitializeComponent()
{
...
this.MouseDown +=
new System.Windows.Forms.MouseEventHandler(this.CReportVIEw_MouseDown);
this.Paint +=
new System.Windows.Forms.PaintEventHandler(this.PaintHandler);
// 因為C#中不支持OnSetCursor所以只好用MouseMove替代了,
// 各位兄台還有更好的方法,請告之
this.MouseMove+=new MouseEventHandler(CReportVIEw_MouseMove);
}
private void PaintHandler(Object sender, PaintEventArgs e)
{
Graphics gs=e.Graphics;
//其余的畫圖代碼
myDoc.m_tracker.OnDraw(gs);
}
private void CReportVIEw_MouseDown(object sender,
MouseEventArgs e)
{
if(e.Button==MouseButtons .Left)
{
Rectangle rectSave=new Rectangle();
Point pt=new Point(e.X,e.Y);
myDoc.m_tracker.GetTrueRect(ref rectSave);
if (myDoc.m_tracker.HitTest(pt) < 0)
{ // just to demonstrate CRectTracker::TrackRubberBand
CRectTracker tracker=new CRectTracker();
if (tracker.TrackRubberBand(this,
pt,
myDoc.m_bAllowInvert))
{ // see if rubber band intersects with the doc''s tracker
tracker.NormalizeRect(ref tracker.m_rect);
// so intersect rect works
if (tracker.m_rect.IntersectsWith(myDoc.m_tracker.m_rect))
{ // if so, put resize handles on it (IE. select it)
if ((myDoc.m_tracker.m_nStyle &
CRectTracker.StyleFlags.resizeInside)!=0)
{ // swap from resize inside to
// resize outside for effect
myDoc.m_tracker.m_nStyle &=
~CRectTracker.StyleFlags.resizeInside;
myDoc.m_tracker.m_nStyle |=
CRectTracker.StyleFlags.resizeOutside;
}
else
{// just use inside resize handles on first time
myDoc.m_tracker.m_nStyle &=
~CRectTracker.StyleFlags.resizeOutside;
myDoc.m_tracker.m_nStyle |=
CRectTracker.StyleFlags.resizeInside;
}
}
}
myDoc.UpdateAllVIEws(null);
}
}
else
{
if (myDoc.m_tracker.Track(this,
pt,
myDoc.m_bAllowInvert,null))
{
// normal tracking action, when tracker is "hit"
myDoc.UpdateAllVIEws(null);
}
}
}
private void CReportVIEw_MouseMove(object sender, MouseEventArgs e)
{
Point mousept=new Point(e.X,e.Y);
if(!myDoc.m_tracker.SetCursor(this,0,mousept))
this.Cursor=Cursors.Arrow;
}
...
}
結束。