visual-studio 热获取相对于控件左上角的光标位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2294559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:34:04  来源:igfitidea点击:

Hot to get cursor position relative to upper left corner of the control?

c#visual-studiovisual-studio-2005

提问by Alexander Stalt

When I clicked on a control,

当我点击一个控件时,

How to get cursor position relative to upper left corner of a (winforms) control ?

如何获取相对于(winforms)控件左上角的光标位置?

C#, VS 2005

C#,VS 2005

PS: I'm asking on context of tooltip "show" method which need that coordinates ..

PS:我在询问需要该坐标的工具提示“显示”方法的上下文..

回答by Bolek Tekielski

This is my code to set tooltips on a composite control, might give you a clue (LED derivers from UserControl):

这是我在复合控件上设置工具提示的代码,可能会给您一个线索(来自 UserControl 的 LED 派生器):

    public LED()
    {
        InitializeComponent();
        m_Image = global::AdvAdmittance.Controls.Properties.Resources.ledgray_small;
        m_ToolTip = new ToolTip();
        m_ToolTip.AutoPopDelay = 5000;
        m_ToolTip.InitialDelay = 1000;
        m_ToolTip.ReshowDelay = 500;
        m_ToolTip.ShowAlways = true;
        m_LedPictureBox.MouseHover += new EventHandler(m_LedPictureBox_MouseHover);
        m_LedPictureBox.MouseLeave += new EventHandler(m_LedPictureBox_MouseLeave);
        m_LedPictureBox.Click += new EventHandler(m_LedPictureBox_Click);
    }

    void m_LedPictureBox_MouseHover(object sender, EventArgs e)
    {
        if (m_ToolTipText != string.Empty)
        {
            Point toolTipPoint = this.Parent.PointToClient(Cursor.Position);
            toolTipPoint.Y -= 20;
            m_ToolTip.Show(m_ToolTipText, this.Parent, toolTipPoint);
        }
    }

    void m_LedPictureBox_MouseLeave(object sender, EventArgs e)
    {
        m_ToolTip.Hide(this.m_LedPictureBox);
    }

回答by Alexander Stalt

Ahh, Thanks for an answer.

啊,谢谢解答。

All I need is a PointToClient method.

我只需要一个 PointToClient 方法。

I hope (maybe) it will be useful for other people, here "my" code.

我希望(也许)它对其他人有用,这里是“我的”代码。

I took almost all code from http://support.microsoft.com/kb/322634and modified three lines:

我从http://support.microsoft.com/kb/322634 中获取了几乎所有代码 并修改了三行:

   void treeView1_MouseMove(object sender, MouseEventArgs e)
    {
        // Get the node at the current mouse pointer location.
        TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);

        // Set a ToolTip only if the mouse pointer is actually paused on a node.
        if ((theNode != null))
        {
            // Verify that the tag property is not "null".
            if (theNode.Tag != null)
            {
                // Change the ToolTip only if the pointer moved to a new node.
                if (theNode.Tag.ToString() != this.toolTip1.GetToolTip(this.treeView1))
                {
                    //this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
                    Point c = System.Windows.Forms.Cursor.Position;
                    Point p = treeView1.PointToClient(c);
                    this.toolTip1.Show(theNode.Tag.ToString(), treeView1, p);
                }
            }
            else
            {
                this.toolTip1.SetToolTip(this.treeView1, "");
            }
        }
        else     // Pointer is not over a node so clear the ToolTip.
        {
            this.toolTip1.SetToolTip(this.treeView1, "");
        }
    }