Move around picture inside picturebox with the mouse - c#

I want to be able to drag around a 100% zoomed picture in a picturebox: http://spunit.tk/x/dragpic1.png.
I want it to work exactly like the Windows Photo Viewer: http://spunit.tk/x/dragpic2.png.
How is this possible?

I believe you need to maintain the coordinates of that picturebox, also set its view style to full-image, without any stretching.
Then, you will need three mouse events: mouse down, mouse up and mouse move, where you can get the mouse coordinates and capture or release mouse to translate the picture box according to mouse delta translation.

Related

How to check whether mouse click was in a rectangle in c#?

I have a couple of rectangles drawn on a visio sheet. I am able to get the rectangles coordinates. I would like to track the mouse coordinates and if the mouse click is in one of the rectangles i would like to execute some code.
Is there a basic example on how to check whether the mouse click was in one of the rectangles?
Thanks in advance!

Getting mouse coordinates relative to target control in drag/drop operations

I see how to get the coordinates of the mouse relative to the entire screen when doing Drag & Drop events, but I'm a bit unclear on the best way to get the mouse coordinates relative to the control the drop occurs in. Do I have to calculate it based on the controls position in the form and the form's position in the screen or is there a more straightforward way of doing it?
You can use Control.PointToClient method:
yourTargetControl.PointToClient(screenPoint);

Bigger mouse cursor in Silverlight

I have a silverlight application in which I have to click on some pictures, if I hover over them for 3 seconds approx.
The problem is that if the pictures are a bit small in size, and the mouse moves a little, it moves out of the respective picture clicking area and selects another picture.
I have tried using a custom image in place of the default mouse cursor, but can this mouse be enlarged in some way so that it has a larger clicking area under it and not only the tip of the mouse pointer?
I think you're thinking about this the wrong way around. The mouse pointer simply defines a coordinate on the screen, rather than an area. If you want mouseover/click etc to be more generous, and give a wider area of interaction, you should make the target area larger.
So in the case of some small image, you can surround it with a larger area to handle the mouseover or click events, for example by surrounding it by a transparent border (note that elements with a transparent background will receive mouse events, unlike elements with no background).

How to start Picturebox events from button

I have button named "Paint" which should allow drawing rectangle on my picturebox after a click, i.e. it acts like a switch to allow drawing(on/off).
I've drawn rectangle using mouse positions as explained here: How Can I Capture Mouse Coordinate on PictureBox? . But rectangle is drawn whenever I move over PictureBox.
How can I implement the functionality where drawing must be implemented only when "Paint" is 'on'
I've tried starting implementation from events of Picturebox: Paint, MouseDown, MoseMove, Mouse Up...
set a flag(bool) in your application telling you the mode you are in whether drawing or not(can be activated from the button you are telling about).
in mouse down take the start point(e.x, e.y) from the mouse event handler.
now you have the top left point of the rectangle.
3.while mouse move take e.x and e.y and which is the bottom right point and draw your rectangle. put the drawing code in mouse move so that the it draws like the "Paint" Program(do this if the draw flag is true).
in mouse up reset the drawing flag
5.in the paint event of the picturebox draw the all the shapes you have so that if you minimized your application windows and then maximized it you will find your shapes drawn this can be achieved by making the rectangle is a class and make some instances of it(for loop over your shapes and draw it).
Simplest solution is to add a boolean bDraw variable, which becomes TRUE only on button click. All other drawing methods do not do anything if this variable is FALSE.
Other solution could be simply to subscribe to
Mouse events inside button click event handler. So if button is NOT clicked, no event raise happen.

How do I "Stick" a shape to the mouse in WPF?

I have a wpf application that has some shapes on a canvas I want to allow the user to click on a shape and then the shape gets stuck the the mouse until they click again.
So far I know very little about WPF so go easy on me ;)
Hopefully this is what you're looking for.
"Mouse dragging logic is fairly straightforward: In the OnMouseDown handler, you save the position of both the object you want to drag and the mouse pointer, and you call CaptureMouse. In OnMouseMove, you calculate the difference between the coordinates of the current mouse pointer position and the saved position, and add that to the original object position. (If you're on a Canvas, you can move the object by calling Canvas.SetLeft and Canvas.SetTop for the object; otherwise you can adjust a TranslateTransform object set to the object's RenderTransform property.) In OnMouseUp, you call ReleaseCapture.
Because your app can lose the mouse capture in other ways (such as the appearance of a system modal message box), you'll also want to override OnLostMouseCapture to abort the dragging operation (if it hasn't terminated with OnMouseUp) and perform cleanup. You might also want to override OnTextInput to abort the drag if the user presses the Escape key."
Copied from http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b6c51eef-269e-4c85-96af-b5b1e4cb9bd5/ there's also code up on this site for how to do it.
Check out this Thread - http://silverlight.net/forums/t/68889.aspx
Since your 'Stick' is on the Canvas, keep setting the Canvas.Left and Canvas.Top on MouseMove with MousePositions

Categories