I am implementing a custom drag and drop interface with winForm Buttons and after viewing several solutions on how to obtain mouse position and check it against control bound have not been able to get it to work.
I have tried:
button.ClientRectangle.Contains(PointToClient(Cursor.Position))
and
button.ClientRectangle.Contains(PointToClient(Control.MousePosition))
Both of these have failed to work. Checking the mouse bounds seem like a simple operation, but I really am stumped.
My speculation of the unexpected values are:
Process of obtaining cursor position may be in wrong corner of cursor image
Method/Function does not work on Buttons for some reason
You are using the wrong object reference, calculating the mouse position relative to the form instead of the button. And you are writing it in a way that make this very hard to debug. Fix:
var pos = button.PointToClient(Cursor.Position);
System.Diagnostics.Debug.WriteLine(pos); // Now it is easy
if (button.ClientRectangle.Contains(pos)) {
// etc...
}
Related
On the CodedUI WpfEdit class there is a way to get the selected text, but I cannot find a way to get the cursor position when nothing is selected (i.e. the index of the caret in the text). Is there anything available for that in the CodedUI framework?
My goal is to assert the position of the cursor in the text contained by the control.
There isn't a codedui method for that but try the following:
add the reference:
using System.Windows.Forms
in the code where you need to get the moue coordinated type:
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
remember that this is not a relative position to the control but the position of the mouse on the screen.
calculating the position of the point relative to control shouldn't be much problem.
I'm not sure there is a way, and I would imagine that there is a different requirement than actually finding the cursor position.
If you are trying to insert some text, you can always copy the text out to the test method, insert the text, and write it back.
Or, if you need to not do that, you could always use the Keyboard.SendKeys method to send a home command and then any number of right arrow commands you need to place the cursor where you'd like it.
Can you elaborate further as to what exactly you need with the cursor position?
Part of my particular dilemma is that I would like to be able to get the initial position of a drag gesture. Depending on the initial position of that drag gesture, the application would either 1) pan the view or 2) display a menu, but not perform these gestures at the same time (which is where part of struggle lies).
For example, if the user initiated a drag from the very left side of their screen and dragged inwards, a menu would pop in instead of the view panning.
I would also like to be able to execute a double tap gesture without also activating a tap gesture, if that's at all possible. I've tried working with boolean flags - for example,
// ...
if (gesture.GestureType == GestureType.DoubleTap)
{
isDoubleTap == true;
}
// ...
public static Vector2 Tap
{
get
{
if (!isDoubleTap)
return gesture.Position;
// ...
But that doesn't work.
I've tried using TouchCollection - if anyone would like me to elaborate on my issues with that I can, but for now I'll say what I tried hasn't worked. It's entirely possible I may have just goofed as I am a novice when it comes to working with touch input.
I've been working on this for a few days and have done as much searching as I can, and nothing I've found has alleviated my issue - if I happened to have missed something, I apologize.
Thank you for your time!
Concerning start position of a drag:
There is a gesture for a drag ending, so if you receive a drag and its the first one since the last drag ended, thats the initial position.
Concerning tap/doubletap:
MonoGame works the same way as XNA as documented here:
The user tapped the screen twice in quick succession. This
always is preceded by a Tap gesture.
This sounds like a input-bindings design problem more than a technical question imo. Consider also what you can move to instead occur on press or release rather than only making use of gestures.
I've fallen down a deep rabbit hole. There are actually two issues that I've dealt with. I don't think anyone will have a solution for the first issue (mentioned in the next paragraph), so I guess the real question is how the heck to use PointToScreen/PointToClient reliably.
I want to use balloons to show validation failures for a user control I've written. The way to do this is to use a tooltip with IsBalloon set. In my validation error handler, I call Show() on the tooltip with my user-control as the parameter. This works if I click on another control, but not if I use tab to leave the control. I've even tried hacks like queuing a task on another thread to sleep a bit, then try to show the balloon. Nothing works.
So, I decided to try to go around this and use the main window ("this") as the parameter and specify coordinates, i.e.:
_balloon.Show(text, this, pos);
I can't seem to calculate the position (pos), though.
I'd like for the position to be the bottom right corner of the control, so here that is:
var clientBottomRight = new Point(_ctrlCallbackPhone.Width, _ctrlCallbackPhone.Height);
Then, I try to get the actual screen coordinates of that spot like so:
var screenPosition = _ctrlCallbackPhone.PointToScreen(clientBottomRight);
Finally, I try to get the coordinates on the main form ("this") like so:
var mainPosition = this.PointToClient(screenPosition);
Then I show the balloon. You have to do it twice because of a Windows bug.
_balloon.Show(string.Empty, this, mainPosition);
_balloon.Show(text, this, mainPosition);
The result is well above and slightly to the left of where it should be. Although the margin of error looks like about the size of the title bar, I don't see how that's it. I'm getting the screen coordinates of a spot, and asking for the client coordinates of that same spot.
Now, of course, if someone has a real solution to making the balloon work when I tab away from the control, I'd stop caring about the screen coordinates thing. I imagine this is some sort of Windows weirdness, though, that I won't be able to solve.
I don't fully understand why this works, but this results in the correct coordinates:
var mainPosition = this.PointToClient(screenPosition);
mainPosition = new Point(
mainPosition.X + SystemInformation.VerticalResizeBorderThickness,
mainPosition.Y + SystemInformation.CaptionHeight + SystemInformation.HorizontalResizeBorderThickness);
I can't seem to find any real documentation on what TouchDevice.Capture() really does. When and / or how should I use it? Or where can I read more about it?
It captures touch input for the specified IInputElement, in the same way as mouse input is captured (see the Remarks in MouseDevice.Capture).
When touch input is captured, the element continues to receive touch events even if the touch position lies outside the hit test area of the element.
You may try the example code in TouchDevice with and without capture and observe the different behaviour.
I am making a form which has a ListView. I want to show a "Picture Window" whenever mouse hovers above one of the listView Item in the ListView. I implemented this by using "ListView"'s "ItemMouseHover" event. I want to show this "Picture Window" just near to the mouse position. But , I couldn't get the mouse position from the itemmousehover event. Can anybody say how to achieve this?
Note: I am using .Net 4.0
EDIT:
"Danbystrom" gave the exact answer. But I found the root cause is some other. See my answer for that.
The static method Control.MousePosition will give you the coordinates in screen space. Then you can convert that into client coordinates with the Control.PointToClient instance method.
var pt = listView.PointToClient( Control.MousePosition );
The new window will be placed on your will, only if you choose "Manual" instead of "WindowsDefaultLocation" in the Property "StartPosition". Otherwise "OS" will try to put your new window's location somewhere near to the mouse position. If the new window hides the mouse in that position, then it will try through some nearby points which will result in flickering.