To set the coordinates of a control in WPF by code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.