How to move elements within picturebox? - winforms

Is there any simple way to move picture parts (any stuff created with DrawImage or DrawString)?
My smart idea:
Override DrawImage(DrawString) method adding ability to store width, height, left, top, of drawn image (and may be boolean variable 'sizable' if I use DrawImage method) to a list of objects. Attach function "WhatToMove" to PictureBox's MouseMove event, which walks through a list of stored rectangle objects, detects what rectangle cursor is over, in case of resizeable rectangle detect if cursor is over any of rectangle's borders and pass this stuff as parameters to the MouseDown function, when I click. Then MouseDown function will attach new function "MoverResizer" to MouseMove event, which redraws picture box with moved or resized parts of image, updates proper rectangle object in list, and also detachs "WhatToMove" from MouseMove. On MouseUp and Leave events of PictureBox detach "MoverResizer" and attach "WhatToMove".
If you think this idea can live you will really help me if tell me how to override DrawImage.
My dumb idea:
Create Labels without text and with borders and locate them over PictureBox right over proper image parts. Then make them draggable and sizeable with their own events. Then translate positions of labels to positions of parts of picture and redraw picturebox. This is simply but I think not so perfect as method above.

Related

The right event to detect a moving UIElement inside a Canvas

I have a Rectangle within a Canvas. The rectangle has the MouseDragElementBehavior attached to it, so it can move freely within the canvas.
I need to do certain calculations when the smallest changes happen in the position of the rectangle, but i can not find a single best event.
So far I have resorted to MouseMove event for the rectangle which is an overkill. Any alternative?
You should base on the events from MouseDragElementBehaviour class, DragBegun, DragFinished and Draggging.

Canvas.GetTop not updated after Element drag

I have a Rectangle nested within a Canvas. The rectangle has a MouseDragElementBehavior attached to it, so it can move freely in the canvas.
I need to calculate the position of the rectangle after each drag. The problem is that the
Canvas.GetTop(rectangle1)
only works for the first time, i.e. before the rectangle is dragged. After the drag, the method call returns the initial position.
Those behaviors usually work by applying a RenderTransform, if it uses a TranslateTransform you can get the offset value from that and add it to the canvas position.
Alternatively you might just want to implement your own dragging logic.

scrollviewer and canvas - scrolling view to given position

I have some problems with implementing autoscrolling in WPF (I think I could call it that way).
I have a canvas placed inside a scrollviwer. On my canvas I can dynamicly add different shapes. The position of this shapes can be changed with mouse. Everytime I add new shape on canvas or change position of shape I fire measureOverride function.Thanks to this scrollview "know" the real size of canvas and the scrollbars appear. However even if scrolbars appear, the view doesn't "follow" shape which I currently move. I mean if I reach visible part of canvas I would like canvas to srcoll.
I was trying to use this function
ScrollToHorizontalOffset()
However I have problem with proper use of that function. I was trying to use (as a parameter) canvas actualwidth but it didn't work well. I also was trying to use as a parameter current position of shape (which I move) but it works only one way. I the viewer follow the moving element if I was moving this element to right side of canvas. However if I move shape back(to the left) the view don't follow the shape.
I hope somebody will understand this :) It is hard to explain my problem.
I also was trying to use as a
parameter current position of shape
That is the correct way to implement. Waht you need is a converter, which will returns the position according to the direction you move the object.

Silverlight Drag and Drop (without a Canvas)

I'm trying to drag and drop (slide) a Silverlight element from one part of a window to another.
I've implemented the MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp event handlers on the element, but I've run into a bit of a problem.
All of the examples I've seen involve moving the element by setting the Canvas.Left and Canvas.Top properties. None of the elements I'm trying to manipulate live inside a Canvas. Is there a way to set the absolute position of the element being dragged, based on the coordinates of the mouse? Or is there a prepackaged solution to this problems somewhere that I've missed?
All panels but Canvas use some kind of constraint to position their children. Only Canvas lets you use absolute positioning. That's why I think it is the only way to implement drag and drop .
Feel free to use a Canvas on top of your existing panel. Just remember to remove the dragged element from its original parent and put it in the Canvas (or drag some kind of copy) and do the reverse on mouse up.
One way of achieving absolutely positioning an item inside any container, not only Canvas, is to use transformation instead of Left/Top properties. For instance, to set at Left=50 Top=80 you can modify the margin values through transformation.

WPF - how to best implement a panel with draggable/zoomable children?

I'm trying to modify the default graph viewer of the Graph# library because its user interface is awful (just try dragging a node outside of the boundaries, you'll see!)
The basic setup is this: there is a GraphCanvas control (inherited from Panel) which has children of Vertex and Edge control types. What I want to achieve is:
GraphCanvas has scroll bars if the contents do not fit in the screen;
GraphCanvas can also be scrolled by "dragging" it (just click on an empty space and drag);
GraphCanvas can be zoomed in and out (via CTRL+mouse wheel);
Vertices can be dragged around. If a vertex is dragged outside the current boundaries of GraphCanvas, the boundaries are increased. The scroll bars should reflect this, however the current viewport should not scroll away while the vertex is being dragged . The same goes if dragging a vertex reduces the boundaries of GraphCanvas - it should stay the same size until the drag operation is finished and resize only then. Automatically scrolling the viewport during a drag operation is awfully confusing and easily introduces dragging errors. See the original implementation if you want to know what I mean.
Although I've got a fair bit of experience with .NET, I'm still a complete beginner in WPF. My current attempt is (in the measure/arrange layout phase) to give each vertext the XY coordinate it desires (even if negative) and implement zooming/scrolling by handling mouse events on the GraphCanvas and modifying the RenderTransform property. Dragging just changes the XY coordinates on the specific vertex (probably triggering the re-layout of the whole thing which would be nice to avoid too). Scrollbars are implemented by placing the GraphCanvas inside a ScrollViewer and implementing IScrollInfo on the GraphCanvas.
Unfortunately there seems to be a problem: I can get mouse events on the GraphCanvas itself only if it has a background at the point. That would be OK, I want a white background anyway, but in the negative coordinates of the GraphCanvas it does not draw the background - and thus does not respond to mouse events.
I'm also wondering if I'm doing the Right Thing by allowing all my child controls (vertices and edges) to go into negative coordinates. How would you implement this?
Added: To clarify about the background problem check out the following screenshot:
(source: valts.21.lv)
What you see here is a simple Windows Forms form with a WPF Host control on it. That has a ScrollViewer in it, and the ScrollViewer has the GraphCanvas in it. The GraphCanvas contains 4 vertices and 6 edges.
The GraphCanvas is stretched to fill the ScrollViewer. But since some of the vertices are at negative coordinates, it has a RenderTransform applied which simply shifts everything to the right (TranslateTransform). It also has a white background brush.
Note the gray area on the left. That's still a part of the GraphCanvas, but the background brush somehow doesn't exted there. Also, if I left-click there with my mouse (not on a node, but on the gray area), I do NOT get an event. If I left-click on the white area, I get all events just fine.
Call CaptureMouse on canvas.mouseDown and ReleaseMouseCapture on mouse up. Also, if you set your canvas background to transparent it will still be hit testable
You can attach a 'Draggable' behavior to each element.

Resources