CSharp: State Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /// <summary> ///empty base class containing State methods to override /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class State { //keeps state of each button protected Mediator med; public State(Mediator md) { med = md; //save reference to mediator } public virtual void mouseDown( int x, int y) { } public virtual void mouseUp( int x, int y) { } public virtual void mouseDrag( int x, int y) { } public virtual void selectOne(Drawing d) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /// <summary> /// Summary description for RectState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class RectState : State { public RectState(Mediator md) : base (md) { } //----- public override void mouseDown( int x, int y) { VisRectangle vr = new VisRectangle(x, y); med.addDrawing(vr); } } |
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 | /// <summary> /// Summary description for StateManager. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class StateManager { private State currentState; private RectState rState; private ArrowState aState; private CircleState cState; private FillState fState; /// <summary> /// /// </summary> /// <param name="med"></param> public StateManager(Mediator med) { //create an instance of each state rState = new RectState(med); cState = new CircleState(med); aState = new ArrowState(med); fState = new FillState(med); //and initialize them //set default state currentState = aState; } /// <summary> /// These methods are called when the toolbuttons are clicked /// </summary> public void setRect() { currentState = rState; } /// <summary> /// /// </summary> public void setCircle() { currentState = cState; } /// <summary> /// /// </summary> public void setFill() { currentState = fState; } /// <summary> /// /// </summary> public void setArrow() { currentState = aState; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDown( int x, int y) { currentState.mouseDown(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseUp( int x, int y) { currentState.mouseUp(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDrag( int x, int y) { currentState.mouseDrag(x, y); } /// <summary> /// /// </summary> /// <param name="d"></param> public void selectOne(Drawing d) { currentState.selectOne(d); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /// <summary> /// interface defining Drawing object /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Drawing { void setSelected( bool b); void draw(Graphics g); void move( int xpt, int ypt); bool contains( int x, int y); void setFill( bool b); Rectangle getRects(); void setRects(Rectangle rect); } |
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 | /// <summary> /// Summary description for Rectangle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Rectangle { /// <summary> /// /// </summary> private int xp, yp, wr, hr; /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle( int x, int y, int w, int h) { xp = x; yp = y; wr = w; hr = h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle( float x, float y, float w, float h) { xp = ( int )x; yp = ( int )y; wr = ( int )w; hr = ( int )h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains( int x, int y) { bool cn = xp <= x && x <= xp + wr; cn = cn && yp <= y && y <= yp + hr; return cn; } /// <summary> /// /// </summary> public int x { get { return xp; } set { xp = value; } } /// <summary> /// /// </summary> public int y { get { return yp; } set { yp = value; } } /// <summary> /// /// </summary> public int w { get { return wr; } set { wr = value; } } /// <summary> /// /// </summary> public int h { get { return hr; } set { hr = value; } } } |
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 | /// <summary> /// Summary description for VisCircle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VisCircle : VisRectangle { private int r; /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public VisCircle( int x, int y) : base (x, y) { r = 15; w = 30; h = 30; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="g"></param> public override void draw(Graphics g) { if (filled) { g.FillEllipse(rBrush, x, y, w, h); } g.DrawEllipse(bPen, x, y, w, h); if (selected) { drawHandles(g); } } } |
1 2 3 4 5 6 7 8 9 10 | /// <summary> /// Summary description for Memento. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Memento { void restore(); } |
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | /// <summary> /// Summary description for Mediator.\ /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Mediator { private bool startRect; private int selectedIndex; private RectButton rectb; private bool dSelected; private ArrayList drawings; private ArrayList undoList; private RectButton rButton; private FillButton filButton; private CircleButton circButton; private PickButton arrowButton; private PictureBox canvas; private int selectedDrawing; private StateManager stMgr; /// <summary> /// /// </summary> /// <param name="pic"></param> public Mediator(PictureBox pic) { startRect = false ; dSelected = false ; drawings = new ArrayList(); undoList = new ArrayList(); stMgr = new StateManager( this ); canvas = pic; selectedDrawing = -1; } /// <summary> /// /// </summary> public void startRectangle() { stMgr.setRect(); arrowButton.setSelected( false ); circButton.setSelected( false ); filButton.setSelected( false ); } /// <summary> /// /// </summary> public void startCircle() { stMgr.setCircle(); rectb.setSelected( false ); arrowButton.setSelected( false ); filButton.setSelected( false ); } /// <summary> /// /// </summary> public void startFill() { stMgr.setFill(); rectb.setSelected( false ); circButton.setSelected( false ); arrowButton.setSelected( false ); if (selectedDrawing >= 0) { stMgr.selectOne(getDrawing(selectedDrawing)); FillMemento m = new FillMemento(selectedDrawing, this ); undoList.Add(m); } repaint(); } /// <summary> /// /// </summary> public void startArrow() { stMgr.setArrow(); rectb.setSelected( false ); circButton.setSelected( false ); filButton.setSelected( false ); } /// <summary> /// /// </summary> /// <returns></returns> public Drawing getSelected() { return (Drawing)drawings[selectedDrawing]; } /// <summary> /// /// </summary> /// <param name="index"></param> public void setSelected( int index) { dSelected = true ; selectedDrawing = index; Drawing d = getDrawing(index); d.setSelected( true ); repaint(); } /// <summary> /// /// </summary> public void fillSelected() { if (dSelected) { Drawing d = (Drawing)drawings[selectedDrawing]; d.setFill( true ); FillMemento m = new FillMemento(selectedDrawing, this ); undoList.Add(m); } } /// <summary> /// /// </summary> public void undo() { rectb.setSelected( false ); circButton.setSelected( false ); arrowButton.setSelected( false ); if (undoList.Count > 0) { //get last element in undo list object obj = undoList[undoList.Count - 1]; undoList.RemoveAt(undoList.Count - 1); //get the Memento Memento m = (Memento)obj; m.restore(); //and restore the old position repaint(); } } /// <summary> /// /// </summary> /// <returns></returns> public ArrayList getDrawings() { return drawings; } /// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public Drawing getDrawing( int a) { if (a < drawings.Count) return (Drawing)drawings[a]; else return null ; } /// <summary> /// /// </summary> /// <param name="d"></param> public void addDrawing(Drawing d) { drawings.Add(d); DrawInstance intc = new DrawInstance(drawings.Count - 1, this ); undoList.Add(intc); } /// <summary> /// /// </summary> public void clear() { drawings = new ArrayList(); undoList = new ArrayList(); dSelected = false ; selectedDrawing = 0; repaint(); } /// <summary> /// /// </summary> public void unpick() { dSelected = false ; if (selectedDrawing >= 0) { Drawing d = getDrawing(selectedDrawing); if (d != null ) { d.setSelected( false ); } selectedDrawing = -1; repaint(); } } /// <summary> /// /// </summary> private void repaint() { canvas.Refresh(); } /// <summary> /// /// </summary> /// <param name="rb"></param> public void registerRectButton(RectButton rb) { rectb = rb; } /// <summary> /// /// </summary> /// <param name="fb"></param> public void registerFillButton(FillButton fb) { filButton = fb; } /// <summary> /// /// </summary> /// <param name="ab"></param> public void registerArrowButton(PickButton ab) { arrowButton = ab; } /// <summary> /// /// </summary> /// <param name="cb"></param> public void registerCircleButton(CircleButton cb) { circButton = cb; } /// <summary> /// /// </summary> /// <param name="a"></param> public void removeDrawing( int a) { drawings.RemoveAt(a); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public int findDrawing( int x, int y) { int i = 0; bool found = false ; int index = -1; while (i < drawings.Count && !found) { Drawing d = getDrawing(i); if (d.contains(x, y)) { index = i; found = true ; } i++; } return index; } /// <summary> /// /// </summary> public void rememberPosition() { if (dSelected) { Drawing d = (Drawing)drawings[selectedDrawing]; Memento m = new DrawMemento(d); undoList.Add(m); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseUp( int x, int y) { stMgr.mouseUp(x, y); } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDown( int x, int y) { stMgr.mouseDown(x, y); repaint(); } /// <summary> /// / /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void mouseDrag( int x, int y) { stMgr.mouseDrag(x, y); repaint(); } /// <summary> /// /// </summary> /// <param name="g"></param> public void reDraw(Graphics g) { foreach ( object obj in drawings) { Drawing d = (Drawing)obj; d.draw(g); } } } |
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 | /// <summary> /// Summary description for FillState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillState : State { public FillState(Mediator md) : base (md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown( int x, int y) { //Fill drawing if you click inside one int i = med.findDrawing(x, y); if (i >= 0) { Drawing d = med.getDrawing(i); d.setFill( true ); //fill drawing } } /// <summary> /// /// </summary> /// <param name="d"></param> public override void selectOne(Drawing d) { //fill drawing if selected d.setFill( true ); } } |
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 | /// <summary> /// Summary description for FillMemento. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillMemento : Memento { private int index; private Mediator med; /// <summary> /// /// </summary> /// <param name="dindex"></param> /// <param name="md"></param> public FillMemento( int dindex, Mediator md) { index = dindex; med = md; } /// <summary> /// /// </summary> public void restore() { Drawing d = med.getDrawing(index); d.setFill( false ); } } |
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 | /// <summary> /// save the state of a visual rectangle /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class DrawMemento : Memento { private int x, y, w, h; private Rectangle rect; private Drawing visDraw; /// <summary> /// /// </summary> /// <param name="d"></param> public DrawMemento(Drawing d) { visDraw = d; rect = visDraw.getRects(); x = rect.x; y = rect.y; w = rect.w; h = rect.h; } /// <summary> /// /// </summary> public void restore() { //restore the state of a drawing object rect.x = x; rect.y = y; rect.h = h; rect.w = w; visDraw.setRects(rect); } } |
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 | /// <summary> /// Summary description for DrawInstance. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class DrawInstance : Memento { private int intg; private Mediator med; /// <summary> /// /// </summary> /// <param name="intg"></param> /// <param name="md"></param> public DrawInstance( int intg, Mediator md) { this .intg = intg; med = md; } /// <summary> /// /// </summary> public int integ { get { return intg; } } /// <summary> /// /// </summary> public void restore() { med.removeDrawing(intg); } } |
1 2 3 4 5 6 7 8 9 10 | /// <summary> /// Summary description for Command. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Command { void Execute(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /// <summary> /// Summary description for CircleState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class CircleState : State { public CircleState(Mediator md) : base (md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown( int x, int y) { VisCircle c = new VisCircle(x, y); med.addDrawing(c); } } } |
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 | /// <summary> /// Summary description for ArrowState. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ArrowState : State { /// <summary> /// /// </summary> /// <param name="md"></param> public ArrowState(Mediator md) : base (md) { } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDown( int x, int y) { med.unpick(); med.startArrow(); int i = med.findDrawing(x, y); if (i >= 0) { med.setSelected(i); med.rememberPosition(); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override void mouseDrag( int x, int y) { int i = med.findDrawing(x, y); if (i >= 0) { Drawing d = med.getDrawing(i); d.move(x, y); } } } |
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 | /// <summary> /// Summary description for CircleButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class CircleButton : ComdToolBarButton { /// <summary> /// /// </summary> /// <param name="md"></param> public CircleButton(Mediator md) : base ( "Circle" , md) { this .DisplayStyle = ToolStripItemDisplayStyle.Text; // // this.Style=ToolBarButtonStyle.ToggleButton ; med.registerCircleButton( this ); } /// <summary> /// / /// </summary> public override void Execute() { med.startCircle(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /// <summary> /// Summary description for ClearButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ClearButton : ComdToolBarButton { public ClearButton(Mediator md) : base ( "Clear" , md) { } public override void Execute() { med.clear(); } } |
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 | /// <summary> /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ComdToolBarButton : ToolStripButton, Command { protected Mediator med; protected bool selected; /// <summary> /// /// </summary> /// <param name="caption"></param> /// <param name="md"></param> public ComdToolBarButton( string caption, Mediator md) { InitializeComponent(); med = md; this .Text = caption; } /// <summary> /// /// </summary> /// <param name="b"></param> public void setSelected( bool b) { selected = b; if (!selected) this .selected = false ; //this.Pushed = false; } /// <summary> /// /// </summary> public virtual void Execute() { } /// <summary> /// /// </summary> public ComdToolBarButton() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="container"></param> public ComdToolBarButton(IContainer container) { container.Add( this ); InitializeComponent(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /// <summary> /// Summary description for UndoButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class UndoButton : ComdToolBarButton { public UndoButton(Mediator md) : base ( "Undo" , md) { } public override void Execute() { med.undo(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <summary> /// Summary description for RectButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class RectButton : ComdToolBarButton { public RectButton(Mediator md) : base ( "Rectangle" , md) { //this.Style =ToolBarButtonStyle.ToggleButton ; this .DisplayStyle = ToolStripItemDisplayStyle.Text; med.registerRectButton( this ); } public override void Execute() { med.startRectangle(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <summary> /// Summary description for PickButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class PickButton : ComdToolBarButton { public PickButton(Mediator md) : base ( "Select" , md) { //this.Style = ToolBarButtonStyle.ToggleButton ; this .DisplayStyle = ToolStripItemDisplayStyle.Text; med.registerArrowButton( this ); } public override void Execute() { med.startArrow(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <summary> /// Summary description for FillButton. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FillButton : ComdToolBarButton { public FillButton(Mediator md) : base ( "Fill" , md) { this .DisplayStyle = ToolStripItemDisplayStyle.Text; //this.Style = ToolBarButtonStyle.ToggleButton ; med.registerFillButton( this ); } public override void Execute() { med.startFill(); } } |
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 | /// <summary> /// Summary description for VisRectangle. /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VisRectangle : Drawing { protected int x, y, w, h; private const int VSIZE = 30; private const int HSIZE = 40; private Rectangle rect; protected bool selected; protected bool filled; protected Pen bPen; protected SolidBrush bBrush, rBrush; /// <summary> /// /// </summary> /// <param name="xp"></param> /// <param name="yp"></param> public VisRectangle( int xp, int yp) { x = xp; y = yp; w = HSIZE; h = VSIZE; saveAsRect(); bPen = new Pen(Color.Black); bBrush = new SolidBrush(Color.Black); rBrush = new SolidBrush(Color.Red); } /// <summary> /// used by Memento for saving and restoring state /// </summary> /// <returns></returns> public Rectangle getRects() { return rect; } /// <summary> /// /// </summary> /// <param name="value"></param> public void setRects(Rectangle value) { x = value.x; y = value.y; w = value.w; h = value.h; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="b"></param> public void setSelected( bool b) { selected = b; } /// <summary> /// move to new position /// </summary> /// <param name="xp"></param> /// <param name="yp"></param> public void move( int xp, int yp) { x = xp; y = yp; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="g"></param> public virtual void draw(Graphics g) { //draw rectangle g.DrawRectangle(bPen, x, y, w, h); if (filled) g.FillRectangle(rBrush, x, y, w, h); drawHandles(g); } /// <summary> /// /// </summary> /// <param name="g"></param> public void drawHandles(Graphics g) { if (selected) { //draw handles g.FillRectangle(bBrush, x + w / 2, y - 2, 4, 4); g.FillRectangle(bBrush, x - 2, y + h / 2, 4, 4); g.FillRectangle(bBrush, x + (w / 2), y + h - 2, 4, 4); g.FillRectangle(bBrush, x + (w - 2), y + (h / 2), 4, 4); } } /// <summary> /// return whether point is inside rectangle /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains( int x, int y) { return rect.contains(x, y); } /// <summary> /// create Rectangle object from new position /// </summary> protected void saveAsRect() { rect = new Rectangle(x, y, w, h); } /// <summary> /// /// </summary> /// <param name="b"></param> public void setFill( bool b) { filled = b; } } |
调用:
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 | /// <summary> /// State Patterns状态模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class StatePatternForm : Form { private Mediator med; private CircleButton circButton; private RectButton rctButton; private PickButton arowButton; private FillButton flButton; private UndoButton undoB; private ClearButton clrb; /// <summary> /// /// </summary> private void init() { //create a Mediator med = new Mediator(pic); //create the buttons rctButton = new RectButton(med); arowButton = new PickButton(med); circButton = new CircleButton(med); flButton = new FillButton(med); undoB = new UndoButton(med); clrb = new ClearButton(med); //add the buttons into the toolbar tBar.Items.Add(arowButton); tBar.Items.Add(rctButton); tBar.Items.Add(circButton); tBar.Items.Add(flButton); //include a separator //ToolBarButton sep = new ToolBarButton(); //sep.Style = ToolBarButtonStyle.Separator; ComdToolBarButton sep = new ComdToolBarButton(); sep.DisplayStyle = ToolStripItemDisplayStyle.Text; // ToolBarButtonStyle.Separator; sep.Text = "Separator" ; tBar.Items.Add(sep); tBar.Items.Add(undoB); tBar.Items.Add(clrb); } /// <summary> /// /// </summary> public StatePatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StatePatternForm_Load( object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseDown( object sender, MouseEventArgs e) { med.mouseDown(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseMove( object sender, MouseEventArgs e) { if (e.Button != MouseButtons.None) { med.mouseDrag(e.X, e.Y); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseUp( object sender, MouseEventArgs e) { med.mouseUp(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_Paint( object sender, PaintEventArgs e) { Graphics g = e.Graphics; med.reDraw(g); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tBar_ItemClicked( object sender, ToolStripItemClickedEventArgs e) { Command comd = (Command)e.ClickedItem; comd.Execute(); } } |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!