WPF line hittest tolerance - c#

I have a canvas where i draw a Line during mouse move. After I have the line, I want to show an overlay when the mouse is over/near the line. I know that I can use the MouseEnter and MouseLeave events from the Line object, but this is really hard when the Line object was created with a StrokeThickness of 1px. So how can I increase the MouseEnter or MouseLeave area to show my overlay even if the mouse pointer is not exactly on the line itself (some defined area around)?

Related

Picturebox blocks MouseEnter event from firing?

as a part of my WinForms application, I want to move a picturebox (blue box) onto the grid as seen below.
I'm moving the picturebox according to the mouse positions (while the left mouse button is pressed down).
Now my problem is, that when dragging the picturebox over the grid, the MouseEnter event is supposed to be called (each box in the grid is a picturebox in itself, with the event attached to it). The event gets called when simply moving with the mouse over it, but as soon as I'm dragging the blue box over it, it doesn't.
So my question is, is it possible that the picturebox on top (blue box) blocks the MouseEnter event of the picturebox on the bottom from firing? And if so, is there a way to work around it? I've thought about using Drag&Drop, but this seemed as the easier solution.
Thanks in advance.

Sensitive border area between MouseEnter and MouseLeave events

I have a PictureBox control on my form for which I have written two events for MouseEnter and MouseLeave. On MouseEnter anther PictureBox enters the form and stands beside the original and with MouseLeave the second PictureBox goes way.
All works fine. Except when the cursor is on the original PictureBox’s border area the MouseEnter and MouseLeave events are repeatedly run. So the second image enters and leaves the form until the cursor is taken away. This makes a strange sight.
How can I avoid this situation?
The border area can be tricky, especially when you want to trigger something the might influence it even if it is only by a few pixels..
One classic situation is when you want to resize or move a control by clicking an dragging it at its border. Unless you use the internal calls and simply code mouseenter, -leave, -move, -down and -up you may well end up with e.g. moving the control away from the mouse and thereby triggering another leave event.
This often occurs only at one set of borders, like left&top or right&bottom.
You need to check you code for any such influences, like the new PictureBox pushing the old one away by a few pixels or a resize that makes it smaller; even one pixel can lead to the effect you see..
If the MouseEnter event is triggering a border to be drawn, or the size of the first PictureBox to change in any way, that can cause the effect you describe.
You could add a check against the mouse coordinates in MouseEnter to ensure that the mouse pointer gets far enough into the control's interior before the event fires. That would prevent an instant firing of the Leave event.

Handling MouseLeftButtonUp over different UIElements in Win Phone 8

I am having an issue with MouseLeftButtonUp not firing when I press my left mouse button down on one UIElement (i.e. a Rectangle) then move my mouse to another Rectangle and release the left mouse button.
According to this page http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttonup.aspx it is because the event only fires when on the same element that the left mouse button was pressed down on,
Occurs when the left mouse button is released while the mouse pointer is over this element.
How can this problem be solved?
I tried adding a Grid element beneath both Rectangles to handle the MouseLeftButtonUp event and that didn't seem to solve the problem. When moving from Rect to Rect it doesn't fire, but when moving from Rect to Grid it fires. When moving from Grid to Rect it doesn't fire either. Different bubbling strategy?
Here is what I'm working with. Examine the second column where each Rectangle is YELLOW. They have been 'highlighted' by pressing on the top rectangle and dragging down to the bottom Rectangle. Once reaching the bottom rectangle I would like to be able to have the mouse button up event occur (by any means) to perform some action.. doesn't work!
If this seems like a weird solution to this type of 'highlight' mechanic or flat-out misuse of Rectangle in this situation don't be shy to say so, I'm new to WP8.
Consider adding a Grid over all the rectangles, that way you would get MouseButtonUp (as long as the mouse was released on the grid), but you would have to calculate which rectangle was moved over based on the MouseEventArgs.GetPosition.
You might also want to look into UIElement.CaptureMouse(), .ReleaseMouseCapture(). Capturing mouse means that the MouseButtonUp will be fired even when the mouse moves away from the MouseButtonDown-Rectangle, but the MouseMove and MouseButtonUp will be raised for the Capturing Rectangle (so you would still have to calculate the rectangles to highlight), until it calls ReleaseMouseCapture().

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

Categories

Resources