C# Panel 画矩形图并可进行移动
我在Panel里面画几个矩形图画好后并且可以进行拖动和更改它的大小。关键是怎么拖动已经画好和它的大小。没有好多分了,感谢大家。...
我在Panel里面画几个矩形图 画好后并且可以进行拖动和更改它的大小。关键是怎么拖动已经画好和它的大小。没有好多分了,感谢大家。
展开
1个回答
展开全部
定义图像的基类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | abstract class DrawBase { internal Color m_BackColor; internal Color m_ForeColor; internal static int m_HalfGrab; public static int HalfGrab { get { return DrawBase.m_HalfGrab; } set { DrawBase.m_HalfGrab = value; } } public Color BackColor { get { return m_BackColor; } set { m_BackColor = value; } } public Color ForeColor { get { return m_ForeColor; } set { m_ForeColor = value; } } public abstract Rectangle GetBound(); public abstract void Draw(Graphics g); public abstract bool Near( int x, int y); public abstract void SetBound(Rectangle bound); } |
定义矩形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | class RectangleObj:DrawBase { private Point m_Start; private Point m_End; private bool m_Solid; public RectangleObj(Point start, Point end) { this .m_Start = start; this .m_End = end; } public bool Solid { get { return m_Solid; } set { m_Solid = value; } } public override System.Drawing.Rectangle GetBound() { int x = this .m_Start.X < this .m_End.X ? this .m_Start.X : this .m_End.X; int y = this .m_Start.Y < this .m_End.Y ? this .m_Start.Y : this .m_End.Y; int r = this .m_Start.X < this .m_End.X ? this .m_End.X : this .m_Start.X; int b = this .m_Start.Y < this .m_End.Y ? this .m_End.Y : this .m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } public override void Draw(Graphics g) { Rectangle bound = this .GetBound(); if ( this .m_Solid) { using (SolidBrush brush = new SolidBrush( this .m_BackColor)) { g.FillRectangle(brush, bound); } } using (Pen pen = new Pen( this .m_ForeColor)) { g.DrawRectangle(pen, bound); } } public override bool Near( int x, int y) { Rectangle bound = this .GetBound(); Rectangle inner = bound; Rectangle outer = bound; inner.Inflate(-m_HalfGrab, -m_HalfGrab); outer.Inflate(m_HalfGrab, m_HalfGrab); Region reg = new Region(outer); reg.Exclude(inner); return reg.IsVisible(x, y); } public override void SetBound(Rectangle bound) { this .m_Start = new Point(bound.X, bound.Y); this .m_End = new Point(bound.Right, bound.Bottom); } } |
定义图像的集合对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class DrawList:List<DrawBase> { private Control m_Owner; public DrawList(Control owner) { this .m_Owner = owner; } internal DrawBase GetNear( int x, int y) { foreach (DrawBase draw in this ) { if (draw.Near(x, y)) { return draw; } } return null ; } internal void Draw(Graphics graphics) { foreach (DrawBase draw in this ) { draw.Draw(graphics); } } } |
构建一个Window窗体用来展示图像操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DrawLines.DrawObjs; namespace DrawLines { public partial class Form1 : Form { private DrawList m_drawList; private DrawBase m_curDraw; private Rectangle m_curBound; private Rectangle m_lastBound; private Point m_StartPoint; public Form1() { InitializeComponent(); this .SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true ); this .InitObjs(); } /// <summary> /// 构造对象 /// </summary> private void InitObjs() { this .m_drawList = new DrawList( this ); DrawBase.HalfGrab = 5; LineObj line0 = new LineObj( new Point(10, 10), new Point(100, 100)); line0.ForeColor = Color.Red; LineObj line1 = new LineObj( new Point(16, 67), new Point(100, 180)); line1.ForeColor = Color.Blue; RectangleObj rect = new RectangleObj( new Point(120, 70), new Point(220, 200)); rect.ForeColor = Color.Green; ButtonObj button = new ButtonObj( new Point(20, 20), new Point(90, 45)); button.ForeColor = Color.Black; button.Text = "button1" ; m_drawList.Add(line0); m_drawList.Add(line1); m_drawList.Add(rect); m_drawList.Add(button); } /// <summary> /// 绘制对象 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { this .m_drawList.Draw(e.Graphics); base .OnPaint(e); } /// <summary> /// 重写以移动对象或设置鼠标 /// </summary> /// <param name="e"></param> protected override void OnMouseMove(MouseEventArgs e) { base .OnMouseMove(e); if ( this .Capture) { if ( this .m_curDraw != null ) { this .moveObj(e); } return ; } if ( this .m_drawList.GetNear(e.X, e.Y) != null ) { this .Cursor = Cursors.SizeAll; } else { this .Cursor = Cursors.Default; } } /// <summary> /// 重写以获取鼠标下的对象 /// </summary> /// <param name="e"></param> protected override void OnMouseDown(MouseEventArgs e) { base .OnMouseDown(e); this .getObj(e); } /// <summary> /// 重写以按Delete键删除当前对象 /// </summary> /// <param name="keyData"></param> /// <returns></returns> protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Delete) { if ( this .m_curDraw != null ) { this .deleteObj(); } } return base .ProcessDialogKey(keyData); } private void btnDelete_Click( object sender, EventArgs e) { if ( this .m_curDraw != null ) { this .deleteObj(); } else { MessageBox.Show( this , "没有选中的对象,请使用鼠标点选择一个!" , "提示" ); } } private void moveObj(MouseEventArgs e) { Rectangle bound = this .m_curBound; bound.Offset(e.X - this .m_StartPoint.X, e.Y - this .m_StartPoint.Y); this .m_curDraw.SetBound(bound); Rectangle _last = this .m_lastBound; Rectangle _bound = bound; _last.Inflate(2, 2); _bound.Inflate(2, 2); using (Region invReg = new Region(_last)) { invReg.Union(_bound); this .Invalidate(invReg); } this .m_lastBound = bound; } private void getObj(MouseEventArgs e) { this .m_curDraw = this .m_drawList.GetNear(e.X, e.Y); if ( this .m_curDraw != null ) { this .m_curBound = this .m_curDraw.GetBound(); this .m_StartPoint = e.Location; } } private void deleteObj() { Rectangle bound = this .m_curDraw.GetBound(); this .m_drawList.Remove( this .m_curDraw); this .m_curDraw = null ; bound.Inflate(2, 2); this .Invalidate(bound); } } } |
运行起来使用鼠标可以画图并移动图像。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询