Silverlight 2 dynamically creates a rectangular object (with complete source code)

Source: Internet
Author: User
Silverlight 2 dynamically create a rectangle object (complete Source code ) [Switch]

Using the canvas of Silverlight 2, I wrote an example of dynamically creating a rectangle. Due to the time, there are some shortcomings,ProgramAll functions work normally. You can drag a rectangle object from any position on the canvas with the mouse, and release the mouse to create a rectangle!

Program running effect:

 

XAMLCode:

  1. <Usercontrol X: Class="Sample. dragrect"
  2. Xmlns=Http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. Xmlns: x=Http://schemas.microsoft.com/winfx/2006/xaml" 
  4. Width="780" Height="400">
  5. <Stackpanel Background="Green" 
  6. Orientation="Horizontal">
  7. <Canvas X: Name="Layoutroot"
  8. Background="Greenyellow"
  9. Width="650" Height="400"
  10. Mousemove="Canvas_mousemove"
  11. Mouseleftbuttondown="Canvas_mouseleftbuttondown" 
  12. Mouseleftbuttonup="Canvas_mouseleftbuttonup"/>
  13. <Stackpanel Background="Gold" Margin="10">
  14. <Textblock Text="Select color :"/>
  15. <Button X: Name="Btnred" 
  16. Width="100" Height="50" 
  17. Fontsize="20" Content="Red" Margin="5" 
  18. Click="Btnred_click"/>
  19. <Button X: Name="Btnblue" 
  20. Width="100" Height="50"
  21. Fontsize="20" Content="Blue" Margin="5" 
  22. Click="Btnblue_click"/>
  23. <Button X: Name="Btngreen" 
  24. Width="100" Height="50"
  25. Fontsize="20" Content="Green" Margin="5"
  26. Click="Btngreen_click"/>
  27. <Button X: Name="Btnclear" 
  28. Width="100" Height="50"
  29. Fontsize="20" Content="Clear" Margin="5" 
  30. Background="Red"
  31. Click="Btnclear_click"/>
  32. </Stackpanel>
  33. </Stackpanel>
  34. </Usercontrol>

C # code:

  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. LINQ;
  4. UsingSystem. net;
  5. UsingSystem. windows;
  6. UsingSystem. Windows. controls;
  7. UsingSystem. Windows. documents;
  8. UsingSystem. Windows. input;
  9. UsingSystem. Windows. Media;
  10. UsingSystem. Windows. Media. animation;
  11. UsingSystem. Windows. shapes;
  12. NamespaceSample
  13. {
  14. PublicPartialClassDragrect: usercontrol
  15. {
  16. PublicDragrect ()
  17. {
  18. Initializecomponent ();
  19. }
  20. BoolMousemoveing =False;
  21. Point mousepoint;
  22. Color rectcolor = colors. Red;
  23. Private VoidCanvas_mousemove (ObjectSender, mouseeventargs E)
  24. {
  25. // Return if the cursor does not drag the rectangle
  26. If(! Mousemoveing)
  27. Return;
  28. // Obtain the current mouse Coordinate
  29. Point curpos = E. getposition (Null);
  30. // Obtain the minimum coordinate value
  31. DoublePosx = mousepoint. X;
  32. DoublePosy = mousepoint. Y;
  33. // Calculate the width and height of the rectangle
  34. DoubleRectwidth = math. Abs (curpos. X-mousepoint. X );
  35. DoubleRectheight = math. Abs (curpos. Y-mousepoint. y );
  36. // Create a rectangle Element
  37. Rectangle rect =NewRectangle ();
  38. // Declare the width and height of the rectangle
  39. Rect. width = rectwidth;
  40. Rect. Height = rectheight;
  41. // Fill color
  42. Rect. Fill =NewSolidcolorbrush (rectcolor );
  43. // Declare the position of the rectangle created in the canvas
  44. Canvas. setleft (rect, posx );
  45. Canvas. settop (rect, Posy );
  46. // Add a rectangle to the canvas.
  47. Layoutroot. Children. Add (rect );
  48. }
  49. Private VoidCanvas_mouseleftbuttondown (ObjectSender, mousebuttoneventargs E)
  50. {
  51. // Obtain the current mouse position
  52. Mousepoint = E. getposition (Null);
  53. // Start creating a rectangle
  54. Mousemoveing =True;
  55. }
  56. Private VoidCanvas_mouseleftbuttonup (ObjectSender, mousebuttoneventargs E)
  57. {
  58. // Rectangle created
  59. Mousemoveing =False;
  60. }
  61. Private VoidBtnred_click (ObjectSender, routedeventargs E)
  62. {
  63. // Declare the rectangle color as red
  64. Rectcolor = colors. Red;
  65. }
  66. Private VoidBtnblue_click (ObjectSender, routedeventargs E)
  67. {
  68. // Declare the rectangle color as blue
  69. Rectcolor = colors. blue;
  70. }
  71. Private VoidBtngreen_click (ObjectSender, routedeventargs E)
  72. {
  73. // Declare the rectangle color as green
  74. Rectcolor = colors. Green;
  75. }
  76. Private VoidBtnclear_click (ObjectSender, routedeventargs E)
  77. {
  78. // Clear all the rectangular elements in the canvas
  79. Layoutroot. Children. Clear ();
  80. }
  81. }
  82. }

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.