C# tooltip reporting mouse coordinates on 3d party window

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 19:37:12

问题


this question is about a tooltip that you can implement very easy in order
to track mouse location via it's coordinates the only problem for me is to add the ability to track the coordinates on a specific window after setting it to foreground ... and it's not a form , but a 3rd party application .

the code which works for me on the visual studio windows form is

ToolTip trackTip;

    private void TrackCoordinates()
    {
        trackTip = new ToolTip();
        this.MouseMove += new MouseEventHandler(Form1_MouseMove);
    }

    void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        String tipText = String.Format("({0}, {1})", e.X, e.Y);
        trackTip.Show(tipText, this, e.Location);
    }

//thats a code i have seen somewhere on the web and then again after some more googling found the msdn source at the url :

msdn source url

so the question remains if you'll be kind to answer : how do i get tool tip coordinates of a 3rd party (other than Vs winform window)


回答1:


subsclass the target window and listen for WM_MOUSEMOVE messages.

Or

Use a timer and grab the mouse screen coordinates.




回答2:


You need to use one of the following (as it is explained in this question):

1.Using Windows Forms. Add a reference to System.Windows.Forms

public static Point GetMousePositionWindowsForms() 
{ 
    System.Drawing.Point point = Control.MousePosition; 
    return new Point(point.X, point.Y); 
} 

2.Using Win32

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
internal static extern bool GetCursorPos(ref Win32Point pt); 

[StructLayout(LayoutKind.Sequential)] 
internal struct Win32Point 
{ 
    public Int32 X; 
    public Int32 Y; 
}; 
public static Point GetMousePosition() 
{ 
    Win32Point w32Mouse = new Win32Point(); 
    GetCursorPos(ref w32Mouse); 
    return new Point(w32Mouse.X, w32Mouse.Y); 
} 


来源:https://stackoverflow.com/questions/10132804/c-sharp-tooltip-reporting-mouse-coordinates-on-3d-party-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!