WPF ry plot (2) Draw a rectangle

Source: Internet
Author: User

 

Use the rectanglegeometry class to draw a rectangle. The code example is as follows:

Rectanglegeometry myrectanglegeometry = new rectanglegeometry (); <br/> myrectanglegeometry. rect = new rect (50, 50, 25, 25); </P> <p> path mypath = New Path (); <br/> mypath. fill = brushes. lemonchiffon; <br/> mypath. stroke = brushes. black; <br/> mypath. strokethickness = 1; <br/> mypath. data = myrectanglegeometry; <br/>

The relative position and size of the rectangle are defined by the rect structure. The relative position is 50, 50, and the height and width are both 25. This creates a square. Inside the rectangle is drawn using the lemonchiffon brush, and its contour is drawn using a black stroke with a thickness of 1.

The effect is as follows:

Although this example uses the path element for renderingRectanglegeometryBut you can also use many other methods.RectanglegeometryObject. For example,RectanglegeometryIt can be used to specify the clip of the uielement or the geometry of the geometrydrawing.

 

Of course, if you want to draw many rectangles, such as rectangular charts, as shown in, pathgeometry is still required.

 

The above example shows that pathgeometry is used. The code used is as follows:

/// <Summary> <br/> // interaction logic for drawrect. XAML <br/> /// </Summary> </P> <p> Public partial class drawrect: system. windows. window <br/> {<br/> private system. windows. threading. dispatchertimer timerclock; <br/> Public drawrect () <br/>{< br/> initializecomponent (); <br/> drawingrect (); <br/> // initialize timer <br/> timerclock = new system. windows. threading. dispatchertimer (); <br/> // creates a timerclock and enables it </P> <p> timerclock. isenabled = false; <br/> timerclock. tick + = new eventhandler (callback); <br/> checkbox1.click + = new routedeventhandler (checkbox_checked); <br/> checkbox2.click + = new routedeventhandler (checkbox_checked ); <br/>}</P> <p> private void checkbox_checked (Object sender, eventargs e) <br/>{< br/> // If (checkbox) sender ). name = "checkbox1") <br/> // {<br/> timerclock. interval = new timespan (0, 0, 1); <br/> If (checkbox1.ischecked = true) <br/> timerclock. isenabled = true; <br/> else timerclock. isenabled = false; <br/>/}< br/> // else if (checkbox) sender ). name = "checkbox2") <br/> // {<br/> // timerclock. interval = new timespan (0, 0, 1); <br/> // If (checkbox2.ischecked = true) <br/> // timerclock. isenabled = true; <br/> // else timerclock. isenabled = false; <br/>/}< br/>}</P> <p> private void timerclock_tick (Object sender, eventargs E) <br/>{< br/> canvas1.children. clear (); </P> <p> Double Y = 150; </P> <p> pathgeometry AA = new pathgeometry (); </P> <p> for (INT I = 0; I <360; I ++ = 5) <br/> {<br/> double value = I + datetime. now. second; <br/> double H = math. sin (value * Math. PI/180) * 100; </P> <p> bool isminus = false; <br/> If (H <0) <br/>{< br/> isminus = true; <br/> H =-H; <br/> AA. addgeometry (New rectanglegeometry (New rect (50 + I, Y, 5, H); <br/>}< br/> else <br/> AA. addgeometry (New rectanglegeometry (New rect (50 + I, Y-h, 5, H ))); <br/>}</P> <p> path mypath = New Path (); <br/> mypath. fill = brushes. lemonchiffon; <br/> mypath. stroke = brushes. black; <br/> mypath. strokethickness = 1; <br/> // mypath. data = myrectanglegeometry; <br/> mypath. data = AA; </P> <p> This. canvas1.children. add (mypath); <br/>}</P> <p> private void drawingrect () <br/>{< br/> Double Y = 150; </P> <p> pathgeometry AA = new pathgeometry (); </P> <p> for (INT I = 0; I <360; I ++ = 5) <br/>{< br/> double H = math. sin (I * Math. PI/180) * 100; </P> <p> bool isminus = false; <br/> If (H <0) <br/>{< br/> isminus = true; <br/> H =-H; <br/> AA. addgeometry (New rectanglegeometry (New rect (50 + I, Y, 5, H); <br/>}< br/> else <br/> AA. addgeometry (New rectanglegeometry (New rect (50 + I, Y-h, 5, H ))); <br/>}</P> <p> path mypath = New Path (); <br/> mypath. fill = brushes. lemonchiffon; <br/> mypath. stroke = brushes. black; <br/> mypath. strokethickness = 1; <br/> // mypath. data = myrectanglegeometry; <br/> mypath. data = AA; </P> <p> This. canvas1.children. add (mypath); <br/>}< br/>}

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.