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:
-
- <Usercontrol X: Class="Sample. dragrect"
-
- Xmlns=Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- Xmlns: x=Http://schemas.microsoft.com/winfx/2006/xaml"
-
- Width="780" Height="400">
-
- <Stackpanel Background="Green"
-
- Orientation="Horizontal">
- <Canvas X: Name="Layoutroot"
-
- Background="Greenyellow"
-
- Width="650" Height="400"
-
- Mousemove="Canvas_mousemove"
- Mouseleftbuttondown="Canvas_mouseleftbuttondown"
-
- Mouseleftbuttonup="Canvas_mouseleftbuttonup"/>
-
- <Stackpanel Background="Gold" Margin="10">
- <Textblock Text="Select color :"/>
-
- <Button X: Name="Btnred"
-
- Width="100" Height="50"
- Fontsize="20" Content="Red" Margin="5"
-
- Click="Btnred_click"/>
-
- <Button X: Name="Btnblue"
- Width="100" Height="50"
-
- Fontsize="20" Content="Blue" Margin="5"
-
- Click="Btnblue_click"/>
- <Button X: Name="Btngreen"
-
- Width="100" Height="50"
-
- Fontsize="20" Content="Green" Margin="5"
- Click="Btngreen_click"/>
-
- <Button X: Name="Btnclear"
-
- Width="100" Height="50"
- Fontsize="20" Content="Clear" Margin="5"
-
- Background="Red"
-
- Click="Btnclear_click"/>
- </Stackpanel>
-
- </Stackpanel>
-
- </Usercontrol>
C # code:
-
- UsingSystem;
-
- UsingSystem. Collections. Generic;
-
- UsingSystem. LINQ;
-
- UsingSystem. net;
-
- UsingSystem. windows;
-
- UsingSystem. Windows. controls;
- UsingSystem. Windows. documents;
-
- UsingSystem. Windows. input;
-
- UsingSystem. Windows. Media;
-
- UsingSystem. Windows. Media. animation;
-
- UsingSystem. Windows. shapes;
-
-
- NamespaceSample
-
- {
-
- PublicPartialClassDragrect: usercontrol
-
- {
-
- PublicDragrect ()
- {
-
- Initializecomponent ();
-
- }
-
-
- BoolMousemoveing =False;
-
- Point mousepoint;
-
- Color rectcolor = colors. Red;
-
-
- Private VoidCanvas_mousemove (ObjectSender, mouseeventargs E)
- {
-
- // Return if the cursor does not drag the rectangle
-
- If(! Mousemoveing)
-
- Return;
-
- // Obtain the current mouse Coordinate
-
- Point curpos = E. getposition (Null);
-
- // Obtain the minimum coordinate value
- DoublePosx = mousepoint. X;
-
- DoublePosy = mousepoint. Y;
-
- // Calculate the width and height of the rectangle
-
- DoubleRectwidth = math. Abs (curpos. X-mousepoint. X );
-
- DoubleRectheight = math. Abs (curpos. Y-mousepoint. y );
- // Create a rectangle Element
-
- Rectangle rect =NewRectangle ();
-
- // Declare the width and height of the rectangle
-
- Rect. width = rectwidth;
-
- Rect. Height = rectheight;
-
- // Fill color
- Rect. Fill =NewSolidcolorbrush (rectcolor );
-
- // Declare the position of the rectangle created in the canvas
-
- Canvas. setleft (rect, posx );
-
- Canvas. settop (rect, Posy );
-
- // Add a rectangle to the canvas.
-
- Layoutroot. Children. Add (rect );
-
- }
-
- Private VoidCanvas_mouseleftbuttondown (ObjectSender, mousebuttoneventargs E)
-
- {
-
- // Obtain the current mouse position
-
- Mousepoint = E. getposition (Null);
-
- // Start creating a rectangle
-
- Mousemoveing =True;
- }
-
-
- Private VoidCanvas_mouseleftbuttonup (ObjectSender, mousebuttoneventargs E)
-
- {
-
- // Rectangle created
-
- Mousemoveing =False;
-
- }
-
- Private VoidBtnred_click (ObjectSender, routedeventargs E)
-
- {
-
- // Declare the rectangle color as red
-
- Rectcolor = colors. Red;
-
- }
-
-
- Private VoidBtnblue_click (ObjectSender, routedeventargs E)
- {
-
- // Declare the rectangle color as blue
-
- Rectcolor = colors. blue;
-
- }
-
-
- Private VoidBtngreen_click (ObjectSender, routedeventargs E)
-
- {
-
- // Declare the rectangle color as green
- Rectcolor = colors. Green;
-
- }
-
-
- Private VoidBtnclear_click (ObjectSender, routedeventargs E)
-
- {
-
- // Clear all the rectangular elements in the canvas
-
- Layoutroot. Children. Clear ();
-
- }
-
- }
- }