WPF,窗口拖动事件与窗口内控件点击事件

帮手程序(assister.exe)在运行鼠标自动操作脚本时,可以弹出置顶的悬浮窗口,实时显示程序运行状态。悬浮窗口上一个小按钮可以快速切换回帮手程序主窗口。如图:

这个悬浮窗口可以自由拖动到屏幕任意位置,避免遮挡其他操作窗口。实现该功能时,通常只需在窗口控件中处理PreviewMouseLeftButtonDown事件,并执行以下代码:

private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

if (e.LeftButton == MouseButtonState.Pressed)

{

this.DragMove();

}

}

然而,由于窗口内部包含按钮控件需要响应点击操作,上述代码会导致按钮点击事件无法触发。

修改后的解决方案是让窗口处理以下三个关键事件:

<Window

...

PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown"
PreviewMouseMove="Window_PreviewMouseMove"
PreviewMouseLeftButtonUp="Window_PreviewMouseLeftButtonUp"

...

Topmost="True"

WindowStyle="None"

AllowsTransparency="True"

Background="Transparent"

>

private void Window_PreviewMouseLeftButtonDown (object sender, MouseButtonEventArgs e)

{

_mousePressedPosition = e.GetPosition(this);

}

private voidWindow_PreviewMouseMove (object sender, MouseEventArgs e)

{

if (Mouse.LeftButton == MouseButtonState.Pressed && _mousePressedPosition != e.GetPosition(this))

{

_isMouseDragMove = true;

DragMove();

}

}

private void Window_PreviewMouseLeftButtonUp (object sender, MouseButtonEventArgs e)

{

if (_isMouseDragMove)

{

_isMouseDragMove = false;

e.Handled = true;

}

}

【免费】帮手Pro1.2.3自动化鼠标点击资源-CSDN下载

相关推荐
做个文艺程序员3 天前
MQ第12篇:AI原生消息架构演进:从MQ到事件驱动的全链路设计
架构·消息队列·wpf·事件驱动架构·ai-native·cqrs
donk13145 天前
React Native for OpenHarmony 实战:三方库 react-native-circular-action-menu 的鸿蒙化适配指南
javascript·分布式·react native·react.js·wpf·harmonyos
donk13146 天前
React Native for OpenHarmony 实战:三方库 react-native-step-indicator 的鸿蒙化适配指南
分布式·react native·react.js·wpf·harmonyos
He BianGu6 天前
【笔记】WPF中RenderOptions类的详细功能
笔记·wpf
VidDown6 天前
上线三个月后的第一次事故:一个 Django 服务的日志、监控与故障复盘
数据库·python·django·wpf·音视频·视频编解码
He BianGu6 天前
【笔记】WPF中CachingHint的功能
笔记·wpf
He BianGu6 天前
【笔记】在WPF中BitmapScalingMode的作用
笔记·wpf
Vae_Mars7 天前
幂等性学习笔记
笔记·学习·wpf
samble8 天前
从零搭建灌装监控系统(十九):单元测试实战,xUnit + 内存SQLite + 反射测试私有方法
c#·wpf·mvvm·modbus·工业控制