When using WPF as a snake game, a problem was found:
The snake's movement, I was realized by constantly refreshing the rectangle (the body of the snake is made up of a group of rectangle), so the coordinates of the rectangle need to be constantly adjusted, but there is no location-related setting in WPF
Found on the Internet can be implemented with thickness
Newnew Thickness (1234);
But it's always a tune.
Can actually be implemented through canvas
About Canvas
To implement canvas with code:
//Create The application ' s main windowMainWindow =NewWindow (); Mainwindow.title="Canvas Sample";//Create the CanvasMyparentcanvas =NewCanvas (); Myparentcanvas.width= -; Myparentcanvas.height= -;//Define child Canvas elementsMYCANVAS1 =NewCanvas (); Mycanvas1.background=Brushes.red;mycanvas1.height= -; Mycanvas1.width= -; Canvas.settop (MYCANVAS1,0); Canvas.setleft (MYCANVAS1,0); MyCanvas2=NewCanvas (); Mycanvas2.background=Brushes.green;mycanvas2.height= -; Mycanvas2.width= -; Canvas.settop (MYCANVAS2, -); Canvas.setleft (MYCANVAS2, -); MYCANVAS3=NewCanvas (); Mycanvas3.background=Brushes.blue;mycanvas3.height= -; Mycanvas3.width= -; Canvas.settop (MYCANVAS3, -); Canvas.setleft (MYCANVAS3, -);//ADD child elements to the Canvas ' children collectionMyParentCanvas.Children.Add (MYCANVAS1); MyParentCanvas.Children.Add (MYCANVAS2); myParentCanvas.Children.Add (MYCANVAS3);//ADD The parent Canvas as the Content of the Window ObjectMainwindow.content =myparentcanvas;mainwindow.show ();
You can also do this by adding a canvas control, after adding
New Rectangle (); Canvas.setleft (Rec, PosX); Canvas.settop (Rec, PosY); The canvas name you added. Children.add (REC);
To set the coordinates of a control in WPF by code