简体   繁体   English

控件范围内的光标

[英]Cursor within bounds of a control

public void TapeMeasure(object sender, EventArgs e)
  {
     if (TrussManager.Truss != null)
     {
        Point containerPoint = trussEditor.PointToClient(Cursor.Position);

        if (!trussEditor.MainMenu.CommandToolStrip.ClientRectangle.Contains(containerPoint))
           execute command A
        else
        execute command B
     }
  }

this event is called from 这个事件是从

ToolStripButton commandButton = new ToolStripButton("Tape", ConvertIconToImage(icon), TapeMeasure);

and

ToolStripMenuItem tsmi = new ToolStripMenuItem("Tape", ConvertIconToImage(icon), TapeMeasure);

(Winforms Application) (Winforms应用程序)

I want to know when my cursor is not my Toolstrip. 我想知道我的光标不是我的工具栏。 However the above code keeps returning the same result regardless of where my cursor is. 但是,无论我的光标在哪里,以上代码始终返回相同的结果。

This code is located in an eventhandler that is called from either a button on Toolstrip or a Button on a contextmenu. 这段代码位于一个事件处理程序中,该事件处理程序从Toolstrip上的按钮或上下文菜单上的Button调用。 If its called on a context menu, I assume the user wants to use the current mousepoint. 如果在上下文菜单上调用它,则假定用户要使用当前的鼠标点。 Otherwise I want the user to go click the point they want 否则我希望用户点击他们想要的点

any suggestions? 有什么建议么?

Since you are using the MouseClick event to initiate your Method the Sender Object of the Click Event will have the object that originated the Event. 由于您使用MouseClick事件来启动您的方法,因此Click事件的发送者对象将具有发起该事件的对象。 In this case I would just determine the Type of the Sender, since one is ToolStripButton and the other is a MenuItem. 在这种情况下,我只确定发送者的类型,因为一个是ToolStripButton,另一个是MenuItem。 As I Mentioned in Chat the Cursor.Point is constantly being updated which is what I think is causing your problems. 正如我在“聊天”中提到的那样,Cursor.Point不断更新,这是我认为引起您问题的原因。

This Example will determine which Object generated the ClickEvent and run the approriate method. 本示例将确定哪个对象生成了ClickEvent并运行适当的方法。

public void TapeMeasure(object sender, EventArgs e) 
{ 
    if (TrussManager.Truss != null) 
    { 
        System.Type sysType = sender.GetType();

        if (!(sysType.Name == "ToolStripButton"))
            //execute command A 
        else 
            //execute command B 
    } 
} 

And this example will take in to account the location of the ContextMenu and process the same Method as Clicking the Button if it is contained in the toolBar. 本示例将考虑ContextMenu的位置,并处理与ToolButton中包含的单击Button相同的方法。

public void TapeMeasure(object sender, EventArgs e) 
{ 
    if (TrussManager.Truss != null) 
    {
        System.Type sysType = sender.GetType();
        if (!(sysType.Name == "ToolStripButton"))
        {
            if (sysType.Name == "ToolStripMenuItem")
            {
                ToolStripMenuItem temp = (ToolStripMenuItem)sender;
                if (trussEditor.MainMenu.CommandToolStrip.ClientRectangle.Contains(trussEditor.MainMenu.CommandToolStrip.PointToClient(temp.Owner.Location)))
                {
                    //execute command A
                }
                else
                {
                    //execute command B
                }
            }
        }
        else
        {
            //execute command A
        }
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM