本文介绍了在C#中实现类似MFC中CRectTracker类的方法。
本文用到的组件源码及测试程序下载地址
本例的开发环境: Visual Studio .NET 2003 Windows 2000
测试环境:Windows 2000
更新记录:2004.4.7 第一次更新
使用许可:代码是否免费使用
功能介绍:
在MFC中CRectTracker是一个很有用的类,可最近学习C#时,发现没有,于是就从MFC转换了一个供大家享用。
使用该类比较简单,几乎和 MFC中 的 CRectTracker 一样,在MDI中:
public class CReportDoc
{ ...
public CRectTracker m_tracker=null;
public CReportDoc(MainWindow mainWin)
{ ...
m_tracker=new CRectTracker();
m_tracker.m_rect.X = 10;
m_tracker.m_rect.Y = 10;
m_tracker.m_rect.Width = 91;
m_tracker.m_rect.Height = 91;
...
}
...
}
相应的在VIEW类里面
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;
}
...
}
本文用到的组件源码及测试程序下载地址
经本站测试,在XP+VS2005开发环境中,能运行,但在操作某部分功能时会发生错误。
新闻热点
疑难解答