代码之家  ›  专栏  ›  技术社区  ›  Timothy Khouri

C#/Windows窗体-如何确定当前是否有人在“拖动”?(不在DragOver事件中)

  •  0
  • Timothy Khouri  · 技术社区  · 17 年前

    假设我在Windows窗体类中的计时器中调用的函数中。。。我如何判断用户当前是否正在尝试“拖动”某些内容?

    例子:

    public void SomeMethod()
    {
        // This doesn't exist of course :)
        if (Mouse.IsDragging) ...
    }
    

    编辑:我应该指定我知道我可以覆盖DragEnter和DragLeave来设置我自己的私有变量。。。但我正在寻求一个“.Nety”解决方案(如果有的话)。

    1 回复  |  直到 17 年前
        1
  •  2
  •   Hans Passant    17 年前

    容易的:

            bool mDragging;
    ...
                mDragging = true;
                DoDragDrop("test", DragDropEffects.All);
                mDragging = false;
    

    通用:

        public static bool IsDragging()
        {
            StackFrame[] frames = new StackTrace(false).GetFrames();
            foreach (StackFrame frame in frames)
            {
                System.Reflection.MethodBase mb = frame.GetMethod();
                if (mb.Module.Name == "System.Windows.Forms.dll" && mb.Name == "DoDragDrop")
                    return true;
            }
            return false;
        }
    
    推荐文章