How to easily detect click inside of rectangle/image? - c#

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.

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!

Creating a Drag and Drop game

I'm creating a game that first requires the user to drag a certain textblock into a rectangle. Depending on which textblock is dropped into the rectangle, I want to store the contents of that specific textblock. It's important to note: I want the values stored only when the textblock is dropped inside the rectangle.
The problem: How can I make it so the computer knows that the rectangle contains the textblock?
Here's a screenshot to make things a little more clear: http://gyazo.com/3aa3a8678f11260889261fcd46366616.png
As of now, I can drag and drop the textblock fine, but the computer has no way of knowing if the textblock and rectangle intersect.
I've been trying to solve this problem for a few days now, and have spent a lot of time trying to use the system.drawing.rectangle.IntersectsWith()...only to find out that you can't dynamically add a system.drawing.rectangle to the canvas.
I've also thought of doing it by coordinates: if(textblock's coordinates are within the bounds of the rectangles coordinates)... However, I've spent time trying to figure out how to dynamically get the coordinate position of a xaml control, only to find out that you can't.
Could someone please offer some guidance? I've been working hard towards this and I've hit a dead end, so any degree of help will be great.
Thanks!

Unable to pan a canvas - Silverlight

I have seen lot of posts which demonstrate how to move objects in a Canvas but what I need is, a way to move either
1. the entire canvas along with its every child
or
2. move every object manually (which is certainly not advisable)
I have put my Canvas in a ScrollViewer.
My actual issue is : I m trying to zoom the canvas using ScaleTransform but after zooming, i also need to move the scroll viewer to a point such that the clicked point is at the center after zooming.
I tried Canvas.SetLeft() and Canvas.SetTop() but bad luck..
Any idea?
Thanks in Advance..
You can communicate with the ScrollViewer to make it scroll its content to a particular offset by using ScrollToVerticalOffset and ScrollToHorizontalOffset.
You'll need to calculate the correct offset by taking into account the size of the "viewport" i.e. the area that you can see of the content, and the zoom level.
http://go4answers.webhost4life.com/Example/center-zoom-wpf-problem-159135.aspx
This might be useful for what you are doing:
http://autoscroller.codeplex.com/

get the text under the mouse pointer

i am relatively new to C#. can anybody please tell me how to display the tool tip on the text editor...just as VS intellisense expands a particular method when we move the mouse over it??? Since i am Developing an adding i want the tool tip to be displayed on document (there is no form associated with it) how to get the text under the mouse pointer thanx in advance
Off the top of my head look at the ToolTip class. I am sure you need to supply it with the x,y coordinates of where you want it to be displayed. So you just need to get the x,y coordinates of the mouse which should be simple enough.

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