如何创建一个简单的菜单,它在C#WPF中的应用程序之外

时间:2011-12-08 18:40:28

标签: c# wpf menu

如何使用C#创建一个在WPF中光标旁边弹出的小菜单。此菜单将在应用程序窗口之外运行。例如;

我移动光标并将其停在桌面上。当它停止时,会有一个小菜单,它位于光标旁边并显示出来。

由于

代码:

    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

    public void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {
        big.Visibility = Visibility.Hidden;
        myTimer.Stop();              
    }

    public void TimerEventProcessor2(Object myObject, EventArgs myEventArgs)
    {
        big.Visibility = Visibility.Visible;
        myTimer.Stop();
    }

    public MainWindow()
    {
        InitializeComponent();
        // Sets the timer interval to 5 seconds.
        myTimer.Interval = 5000;
        myTimer.Start();
        myTimer.Tick += new EventHandler(TimerEventProcessor);

        myTimer.Start(); 
        myTimer.Tick += new EventHandler(TimerEventProcessor2);
    }

EDIT2

这是代码的一部分。我创建了另一个dispatchertimer whish有一个名字hidetimer。我在代码上看到的时间定义为3秒。这个计时器调用deneme_Tick,然后我在你的代码中执行与HideWindow()相同的操作。

        timer.Interval = new TimeSpan(0, 0, 0, 1);
        timer.Tick += (sd, args) =>
        {
            movingCount++;

            if (movingCount >= menuShowDelay)
            {
                this.Visibility = System.Windows.Visibility.Visible;
                mouse.Enabled = false;
                timer.Stop();
                this.Left = mouseLeft;
                this.Top = mouseTop;
                this.Topmost = true;

                hidingtimer.Interval = new TimeSpan(0, 0, 0, 3);
                hidingtimer.Start();
                hidingtimer.Tick += new EventHandler(deneme_Tick);

                movingCount = 0;
            }



        };

2 个答案:

答案 0 :(得分:0)

我只是有一个裂缝,看看这里,

http://www.a2zdotnet.com/View.aspx?Id=92

如果可能的话,在主窗口中添加一个上下文菜单,然后以编程方式显示它,

Show menu programmatically in WPF

使用计时器让它在1秒后发生。

它是否会出现在光标所在的位置我不确定。

答案 1 :(得分:0)

(抱歉我的英语不好)

我就是这样做的:

  • 以任何您想要的方式在WPF中创建菜单(它可以是真实的菜单或列表框,堆叠面板上的按钮等)。
  • 将您的WindowStyle设置为无。
  • 要检测鼠标移动和位置,您可以使用this library
  • 编写一些逻辑来显示/隐藏鼠标位置上的窗口(使用计时器检测鼠标何时不移动X秒)

如果您需要有关如何完成这些步骤的更多详细信息,请告诉我们:)

编辑1

您可以使用该库检测鼠标移动,如下所示:

mouse = new MouseKeyboardActivityMonitor.MouseHookListener(new GlobalHooker());
mouse.MouseMove += (sd, args) =>
        {
            movingCount = 0;
            mouseLeft = args.X; //set the window.left to mouseLeft before showing it
            mouseTop = args.Y; //set the window.top to mouseTop before showing it
        };

mouse.Enabled = true;