How to start Picturebox events from button - c#

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.

Related

Move around picture inside picturebox with the mouse

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.

How to select a region in a Windows Forms PictureBox?

I want to get a part of picture in a PictureBox by allowing the user to select
a rectangular region (similar to how it can be done in Photoshop with selection tool).
How I can do this?
You would have to draw a rectangle or lines based on where they clicked and dragged. Your starting point would be the mouse x, y and then you would get the last point from mouse release x, y.
Catch the mouse events on the Picture Box to define your selection rectangle and use Graphics.DrawRectangle to draw your selection rectangle in the Paint Event of the Picture Box.

WinForm: Selection box flickers when dragging the mouse

I made a click-drag selection box in a picture box. In the picturebox Paint event hander I use
e.Graphics.DrawRectangle(pen, rectangle);
and update the rectangle and refreshe the picturebox in the mouse move event handler.
The selection box looks smooth as long as the mouse remains at the bottom-right corner (i.e. drag to right/bottom). However if I want to drag the mouse to the left or up, rectangle.X/rectangle.Y has to be re-set constantly and the box flickers very noticeably.
Is there a better/more efficient way to do the drawing? Much appreciated!
another thing to take into account is DoubleBuffering
How do I enable double-buffering of a control using C# (Windows forms)?
have a look at the excepted answer here for the correct double buffering code.
I just found the solution: Replacing pictureBox.Refresh() with pictureBox.Invalidate() would make the redrawing smooth at all times. It seems Refresh() adds huge overhead in this case, that even setting the main Form or the PictureBox's DoubleBuffered property to true will not help.

Change mouse pointer on component border

I'm trying to change the mouse pointer as it moves over the outer borders of a component. Sort of the same way it changes when moved over the borders of a resizable form. Any ideas?
Controls have events like
MouseEnter
MouseLeave
MouseHover
and so on. Use one of them

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