How to check whether mouse click was in a rectangle in c#? - 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!

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.

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 easily detect click inside of rectangle/image?

When I draw an image, I know I could find out whether the user clicks on it by comparing X,Y of mouse with position and size of that image.
However is there a quicker way? I know for two rectangles there is an intersect methods.
Thanks
Ultimately, the same thing has to happen. The point coordinates need to be tested against the rectangle coordinates. But if you already have Rectangle r and Point p, you can do if (r.Contains(p)).
Not sure if this is possible (as I'm a bit unclear on what you are trying to accomplish), but maybe you can put a button behind the area you want to make clickable and assign an image to that button.

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.

How to make a resizeable rectangle selection tool?

I'm trying to write a very simple photo editor using C# 2008 or QT4.
How to make a resizeable rectangle selection tool like the photoshop did?
If you are talking about the "classic" "rubber band" type of selection rectangle, check out How to draw a rubber band rectangle or a focus rectangle in Visual C#.
WPF Code Example: http://www.codeproject.com/KB/WPF/wpfmarchingants.aspx?display=Print
This one is a bit more complicated because it involves image cropping, but if you scroll down to the selection rectangle, you can see the basic formulas for calculating the rectangle: http://69.10.233.10/KB/WPF/ImageCropper.aspx
Lastly, another one using GDI: http://codelog.blogial.com/2008/10/31/rubber-band-selection-rectangle-in-c/
If you provide more specifics, I'm sure we can help you out more.
Qt has a class for that:
http://doc.trolltech.com/4.5-snapshot/qrubberband.html
I don't know the specific calls, but the idea is this:
You want to draw a transparent rectangle with an opaque or dotted border. The rectangle appears when the mouse button is clicked. While the button is held, the dimension of the rectangle will change as the mouse moves, with the top-left point at the position where the button was clicked and the bottom-right following the mouse as it moves. Releasing the button causes the rectangle to fix its position over the selection area.
You should be able to figure out the particulars from a resource on the C# Drawing namespace.

Categories