WPF - 按住鼠标左键打开上下文菜单

发布于 2024-10-19 14:35:10 字数 127 浏览 3 评论 0原文

在 Google Chrome 中,我真的很喜欢用鼠标左键按住“后退”按钮来获取完整浏览历史记录的功能。

在我的 WPF 应用程序中:对于带有上下文菜单的按钮,如何在按住鼠标左键(当然仍然使用常规右键单击)的情况下打开菜单?

In Google Chrome I really like the functionality of the left mouse hold down on the Back button to get the full browse history.

In my WPF app: for a button with a context menu, how can I open the menu on hold down of the left mouse (and of course still with the regular right click)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-10-26 14:35:10

我建议通过启动计时器来处理 MouseDown 事件。如果触发 MouseUp 事件,则需要停止计时器。您可以使用 DispatcherTimer 来实现此目的。然后,您可以设置一个时间,在该时间后触发 Timer_Tick 事件,您可以在其中执行您想要执行的操作。为了避免冒泡 MouseDownMouseUp 事件出现问题,我建议在窗口构造函数中添加这两个处理程序,而不是在 XAML 中添加它们(至少将事件在我的示例代码中没有触发,所以我改变了)通过使用

button1.AddHandler(FrameworkElement.MouseDownEvent, new MouseButtonEventHandler(button1_MouseDown), true);
button1.AddHandler(FrameworkElement.MouseUpEvent, new MouseButtonEventHandler(button1_MouseUp), true);

此外,您需要在那里设置计时器:

向窗口类添加一个字段:

DispatcherTimer timer = new DispatcherTimer();

并使用您想要等待的时间设置计时器<代码>Timer_Tick 事件被触发(也在窗口构造函数中):

timer.Tick += new EventHandler(timer_Tick);
// time until Tick event is fired
timer.Interval = new TimeSpan(0, 0, 1);

然后您只需要处理事件即可完成:

private void button1_MouseDown(object sender, MouseButtonEventArgs e) {
    timer.Start();
}

private void button1_MouseUp(object sender, MouseButtonEventArgs e) {
    timer.Stop();
}

void timer_Tick(object sender, EventArgs e) {
    timer.Stop();
    // perform certain action
}

希望有所帮助。

I would suggest to handle the MouseDown event by starting a timer there. If the MouseUp event is fired, the timer needs to be stopped. You can use a DispatcherTimer for that. Then you can set up a time after that the Timer_Tick event should be fired, where you can perform the action you would like to perform. In order to avoid problems with the bubbling MouseDown and MouseUp events, I would suggest to add the two handlers in the window constructor instead of adding them in XAML (at least the events did not fire in my example code, so i changed that) by using

button1.AddHandler(FrameworkElement.MouseDownEvent, new MouseButtonEventHandler(button1_MouseDown), true);
button1.AddHandler(FrameworkElement.MouseUpEvent, new MouseButtonEventHandler(button1_MouseUp), true);

Additionally, you need to set up the timer there:

Add a field to the window class:

DispatcherTimer timer = new DispatcherTimer();

and set up the timer with the time you want to wait until the Timer_Tick event is fired (also in the window constructor):

timer.Tick += new EventHandler(timer_Tick);
// time until Tick event is fired
timer.Interval = new TimeSpan(0, 0, 1);

Then you only need to handle the events and you are done:

private void button1_MouseDown(object sender, MouseButtonEventArgs e) {
    timer.Start();
}

private void button1_MouseUp(object sender, MouseButtonEventArgs e) {
    timer.Stop();
}

void timer_Tick(object sender, EventArgs e) {
    timer.Stop();
    // perform certain action
}

Hope that helps.

万水千山粽是情ミ 2024-10-26 14:35:10

我认为你唯一的方法是手动处理按钮上的 MouseDown/Move/Up 事件,在 MouseDown 发生后等待一段时间,如果在这段时间内你没有 MouseMove 或 MouseUp 事件,那么手动显示上下文菜单。如果显示上下文菜单,则必须注意按钮不要在此之后生成 Click 事件并执行默认的单击操作。

I think your only way is to manually process MouseDown/Move/Up events over the button, wait for a certain amount of time to pass after MouseDown occured and if in this amount of time you don't have a MouseMove or MouseUp event, then manually show the ContextMenu. If you show the context menu, you'll have to take care for the button not to generate a Click event after that and to do it's default click action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文