How do I position an ellipse on a Silverlight Grid? - c#

I am creating a silverlight application which will allow you to click at two places on the screen and will draw an ellipse whose major axis starts and ends at the click locations. The clickable area is a Silverlight Grid control. Currently:
When you first click, I am:
Dropping a marker at the click
point.
Creating an ellipse and parenting it
to the Grid.
Creating and setting an
AngleTransform on the ellipse.
As you move the mouse, I am:
Calculating the distance to the
first click point.
Setting the Width of the ellipse to
this length.
Calculating the angle of a line to
the click point and the Grid's
X-Axis.
Setting the Ellipse's AngleTransform Angle to this angle.
So far, so good. The ellipse is displayed, and its length and angle of rotation follow the mouse as it moves.
However, the major axis of the ellipse is offset from the click point.
How do I position the Ellipse so its major axis starts at the click point and ends at the current mouse position?

The answer turned out to be:
Don't use System.Windows.Shapes.Ellipse. Instead use System.Windows.Shapes.Path and embed an EllipseGeometry in it.
Also set the Path.RenderTransform to a RotateTransform.
Don't set Width or Height or Stretch on the Path. Instead set the Center, RadiusX, and RadiusY of the EllipseGeometry.
Finally, set the RotateTransform.Angle to the angle of intersection of the Ellipse major-axis and the X-axis (the ArcTan of the major-axis slope). Also set RotateTransform.CenterX and CenterY to the EllipseGeometry Center.

If I had to guess (code would help), I would think that you could add padding from the difference of the beginning click point to the left side of the grid, which should help move it over by the offset.

May be it's a good idea to use Canvas instead of grid for your application, than you will be able to set up shapes coordianates directly.

Related

How can I change the size of two rectangles in a canvas by moving an ellipse?

I have a canvas with two rectangles in it, side by side. I want to have an ellipse over where they meet that I can drag horizontally and have the rectangles change size to always meet under this movable elipse, just like a slider, but with shapes. I've read this
http://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1, snapshot as below:
and so far I have a clickable, moving ellipse and two rectangles beneath it. How do I pass the changing horizontal position of the ellipse to the two rectangles so that they can resize?
Any help appreciated, C# just seems like magic to me.
Not sure if all you want is to get the location of the ellipse, if yes try below snippet
var left=Canvas.GetLeft(ellipse1);
var top=Canvas.GetTop(ellipse1);
To set its location and size:
Canvas.SetLeft(ellipse1, left+100);
ellipse1.ActualWidth= ...;

Drag a dynamic line in winform

I just want to know how can i drag and drop a line which was created dynamically at runtime (mouse draw line) in c# . The dynamic line was placed over a panel.
I have used two ways in the past:
On MouseDown calculate the distance between the line and the mouse cursor. If it is within a couple of pixels start dragging. While the MouseButton is down respond to the MouseMove by translating the startpoint and the endpoint of the line over the same vector (current mouse position - start dragging mouse position)
On MouseDown test the color of the pixel under the mouse cursor to see if it is over the line. If so do the same dragging as above.
The tricky thing is that option 1 is hard when there are multiple lines that are close and you need to find out which line needs to be dragged
Option 2 is hard when the line is very thin.
Another way is to draw a thicker line on an invisible bitmap when a line is drawn and test the pixel on the invisible bitmap. That way you can give a bit more tolerance AND you could give each invisible line a color that is distinct so it is easier to identify what line has been clicked.
Does this make sense?

How to do the opposite to Width="150*" in WPF?

I would like for the Width attribute to dynamically expand until it reaches 150, and after this, user should be able to expand it further via drag (MaxWidth won't allow this).
Width="150*"
has similar functionality, but in the opposite direction(minimum width and expand as needed). I want to have "expand as needed" and maximum expansion width.
Is there any way this can be done using XAML?
Put a line on the edge that you want the user to be able to drag. Set the Cursor property to "SizeWE" on that line so that the user knows he can drag it. From that line, handle the mouse down, mouse move, and mouse up events. In the MouseDown call CaptureMouse() from the line control and save the mouse position. On mouse move, get the new mouse position, calculate the change from the mouse down position, and increase the width of the control that you were constraining to 150 by the amount of the mouse move in the right direction and then reset the original mouse position to the current mouse position. On MouseUp, call ReleaseMouse() from the line control.
Hope this helps.
You can programmatically set the width depending on the size of the item. Bind the width to a GridLength and then you can let the user control it, or control it yourself up to 150
private GridLength _marquee1Width = new GridLength(150, GridUnitType.Pixel);
You can set this to GridUnitType.Pixel, GridUnitType.Auto, or GridUnitType.Star

UserControl ClientRectangle is smaller than it says

I am trying to make my UserControl with custom borders (I changed default border style to "none" in properties - I created control and I am invoking Graphics.DrawRectangle(...,ClientRectangle). And what I see is only the top and left borders of my painted rectangle - it seems that real size of the area where I can draw is different than ClientRectangle... How to change it / get the right ClientRectangle size?
ClientRectangle returns the rectangle in exclusive coordinates so you need to subtract 1 from the bottom and right hand sides. This will work regardless of how many units per pixel there are in the current graphics mode, someone correct me if I am wrong.

Drawing squares in WPF

I want to achieve the following with a WPF application (in a certain area/defined area):
When clicking and holding on the app, you can draw a square
You can do this as many times, but not overlap any squares
You can drag squares around the application
What do I need to achieve this, I assume a bunch of onclick/onmove's. Is there an easier way, such as using canvas? Any insight would be great.
You will have to use a canvas if you want the squares to appear where the user clicks and drags.
The mouse down event would define one corner and the mouse up the second. You'd have to constrain the movement of the cursor so that the x and y dimensions of the rectangle were the same.
On each mouse move event you'd have to check if the cursor were over one of the existing squares and prevent the square growing any further.
For the dragging of existing squares modify the mouse down event to check what's under the cursor. If it's the canvas start the square drawing mode, if it's a rectangle (square) then start the dragging mode. Again you'd need to use the mouse move event to check that the square doesn't intersect with any existing squares.
There's a code project article describing how to drag elements inside a Canvas: Dragging Elements in a Canvas
Speaking of ChrisF's mentioning of using a Canvas, I would suggest you use DragCanvas (found in the article)

Categories

Resources