Reputation: 3689
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...
Upvotes: 0
Views: 1173
Reputation: 1375
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).
Upvotes: 2
Reputation: 62256
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.
Upvotes: 0