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.
Related
I have a RichTextBox with a SpellCheck implemented. I want to place the cursor/caret exactly where the rightclick is positioned. For example, if i have two misspelled words such as :
I belive this is wroking
and I right click on "belive", the context menu that opens is based on Wroking, because my cursor was last positioned there. To open the list of suggested words of "belive", I have to first Left click on the word, to position the caret and then RightClick.
So to make it clear, I want to automatically position the caret where my cursor is on RightClick. Is it possible to do that? Thanks in advance.
I Found a way to solve the problem, by getting the mouseposition on MouseRightButtonUp event and attributing it to a TextPointer. From there, I can select the exact Range of the current word, and then do something with it.
Point mousePoint = Mouse.GetPosition(textBox);
TextPointer current = textBox.GetPositionFromPoint(mousePoint, true);
When using this code to bring an element into the browser view, it works most of the time.
protected void MakeVisible(IWebElement link, bool always)
{
var actions = new Actions(this.Driver);
actions.MoveToElement(link).Perform();
...
However, when the element is just (say a button that is 50% there) on screen, it doesn't, but it isn't onscreen enough to register for a click event.
(The bottom of the snip is the bottom of the browser viewport)
How can I handle this edge case?
This was encountered using the FireFox driver.
I've never seen this behavior before. Generally when I click on an element, if it's offscreen, the viewport (automatically) is scrolled so that it's visible and it is clicked. I'll give you my best guess and hopefully it will give you some ideas that you can investigate and hopefully solve the problem.
So I think the issue is that .MoveToElement() moves to the center of the element. If the center of the element in question is on the page but is not able to be clicked because it's not fully visible, then it seems like a solution is to make sure the entire element is on the page. One way I can think of to do this is to use MoveToElement(IWebElement, Int32, Int32). The offsets are from the top-left corner of the element. If you use this to move to the top-left and then the bottom-right, that combination should fully display the element then it can be clicked. The top-left would be 0,0 and the bottom right would be found using IWebElement.Size to get the width and height of the element.
A function would look something like this
public void ShowElement(IWebElement e)
{
Actions action = new Actions(Driver);
action.MoveToElement(e, 0, 0).MoveToElement(e, e.Size.Width, e.Size.Height).Build().Perform();
}
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...
}
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?
The thing is I have some graphics shown in a form, rectangle for example, and I want to capture when the point gets over thees fields. So I Thoght I try to find the corrrdinates for thees rectangles, but as thay are the coords in the form it dose not match the ones with the mouse location.
So I wonder is there a way to find what coord on the screen a Point has on the screen and not in the form or controller?
Each control hs PointToFoo methods for conversion. Note that you should call this from the parent of the object who's location you want:
Point scrPos = this.PointToScreen(panel1.Location);
Alternatively, you can get the panel's screen coordinates with:
Point scrPos = panel1.PointToScreen(new Point(0,0));
Note that the above two examples could gve different result due to the border-size of the panel.
If you are using the graphics object for the form by calling this.CreateGraphics() within the form, then the coordinates used when you draw the rectangle should be exactly the same as those returned by the click event on the form.
Do you know what coordinates your pointer is in? You can get the coordinates for your window with a call to GetWindowRect() and subtract the top/left from your mouse cursor to get client coordinates.
I seem to remember there being a function to do that for you in fact, but it's been some time since I dabbled in custom GUI controls.