在winform中新建一个置顶圆球悬浮窗,点击后触发点击事件
调用
// ✅ 在这里创建并显示置顶圆球悬浮窗
ballWindow = new CircleBallForm();
if (checkBox3.Checked)
{
ballWindow.Show(); // 显示悬浮球
//// 可选:让悬浮球跟随主窗体关闭而关闭
//this.FormClosed += (s, e) => ballWindow?.Close();
}
else
{
ballWindow.Close();
}
public partial class CircleBallForm : Form
{
private bool isDragging = false;
private Point lastCursor;
private Point lastForm;
public CircleBallForm()
{
InitializeComponent();
SetupFloatingBall();
this.SuspendLayout();
this.ClientSize = new Size(50, 50);
this.Name = "FloatingBallForm";
this.ResumeLayout(false);
}
private void SetupFloatingBall()
{
// 设置窗体属性
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.ShowInTaskbar = false;
this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;
this.Size = new Size(30, 30);
this.StartPosition = FormStartPosition.Manual;
// 设置初始位置(屏幕右上角)
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 20, 50);
// 绑定事件
this.MouseDown += FloatingBallForm_MouseDown;
this.MouseMove += FloatingBallForm_MouseMove;
this.MouseUp += FloatingBallForm_MouseUp;
this.Click += FloatingBallForm_Click;
this.Paint += FloatingBallForm_Paint;
// 设置双缓冲
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer, true);
}
private void FloatingBallForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
// 创建渐变刷子
Rectangle rect = new Rectangle(200, 200, this.Width - 10, this.Height -10);
using (LinearGradientBrush brush = new LinearGradientBrush(
rect, Color.FromArgb(100, 135, 206, 250), Color.FromArgb(200, 70, 130, 180), 45f))
{
// 绘制圆球
g.FillEllipse(brush, rect);
// 绘制边框
using (Pen pen = new Pen(Color.FromArgb(10, 5, 5, 12), 1))
{
g.DrawEllipse(pen, rect);
}
// 绘制高光效果
Rectangle highlightRect = new Rectangle(15, 15, 25, 25);
using (LinearGradientBrush highlightBrush = new LinearGradientBrush(
highlightRect, Color.FromArgb(120, 255, 255, 255), Color.Blue, 135f))
{
g.FillEllipse(highlightBrush, highlightRect);
}
}
}
//private void FloatingBallForm_Paint(object sender, PaintEventArgs e)
//{
// Graphics g = e.Graphics;
// g.SmoothingMode = SmoothingMode.AntiAlias;
// // 创建渐变刷子
// Rectangle rect = new Rectangle(5, 5, this.Width - 10, this.Height - 10);
// using (LinearGradientBrush brush = new LinearGradientBrush(
// rect, Color.FromArgb(100, 135, 206, 250), Color.FromArgb(200, 70, 130, 180), 45f))
// {
// // 绘制圆球
// g.FillEllipse(brush, rect);
// // 绘制边框
// using (Pen pen = new Pen(Color.FromArgb(150, 25, 25, 112), 2))
// {
// g.DrawEllipse(pen, rect);
// }
// // 绘制高光效果
// Rectangle highlightRect = new Rectangle(15, 15, 25, 25);
// using (LinearGradientBrush highlightBrush = new LinearGradientBrush(
// highlightRect, Color.FromArgb(120, 255, 255, 255), Color.Transparent, 135f))
// {
// g.FillEllipse(highlightBrush, highlightRect);
// }
// }
//}
// 拖拽相关事件
private void FloatingBallForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastCursor = Cursor.Position;
lastForm = this.Location;
}
}
private void FloatingBallForm_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point currentCursor = Cursor.Position;
Point offset = new Point(currentCursor.X - lastCursor.X, currentCursor.Y - lastCursor.Y);
this.Location = new Point(lastForm.X + offset.X, lastForm.Y + offset.Y);
}
}
private void FloatingBallForm_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
// 点击事件
private void FloatingBallForm_Click(object sender, EventArgs e)
{
// 触发自定义点击事件
OnBallClicked?.Invoke();
//OpenFolder(@"ADBE:\\1\\QtScrcpy-win-x64-v2.1.2\\QtScrcpy-win-x64-v2.1.2\\unlock.bat");
// 可选:添加点击动画效果
AnimateClick();
}
private void OpenFolder(string folderPath)
{
if (folderPath.StartsWith("ADB") && folderPath.Length > 2)
{
folderPath = folderPath.Substring(3);
try
{
string adbPath = @"E:\1\QtScrcpy-win-x64-v2.1.2\QtScrcpy-win-x64-v2.1.2\adb.exe"; // 替换为你的adb实际路径
string batPath = @"E:\1\QtScrcpy-win-x64-v2.1.2\QtScrcpy-win-x64-v2.1.2\unlock.bat";
// 创建进程启动信息
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
// 先设置ADB路径到环境变量,再执行批处理文件
// 注意:&& 用于连接两个命令,先执行set设置环境变量,再执行bat文件
startInfo.Arguments = $"/c \"set PATH={adbPath};%PATH% && \"{folderPath}\"\"";
// 设置工作目录为批处理文件所在目录
startInfo.WorkingDirectory = Path.GetDirectoryName(batPath);
// 启动进程
Process.Start(startInfo);
}
catch (Exception ex)
{
}
//Process.Start("cmd.exe", $"/C \"{folderPath}\"");
return;
}
if (folderPath.StartsWith("KK") && folderPath.Length > 2)
{
folderPath = folderPath.Substring(2);
try
{
Process.Start(folderPath);
}
catch (Exception ex)
{
}
//Process.Start("cmd.exe", $"/C \"{folderPath}\"");
return;
}
//判断 folderPath 当前的文件路径(文件或者文件夹是否存在,如果不存在return;
// 判断路径是否存在(可以是文件或文件夹)
if (!Directory.Exists(folderPath) && !File.Exists(folderPath))
{
return;
}
string system = Environment.OSVersion.Platform.ToString();
if (system.Contains("Win"))
{
Process.Start("explorer.exe", folderPath);
}
else if (system.Contains("Linux"))
{
Process.Start("xdg-open", folderPath);
}
else if (system.Contains("Mac"))
{
Process.Start("open", folderPath);
}
}
// 点击动画效果
//private void AnimateClick()
//{
// Timer animationTimer = new Timer();
// int originalWidth = this.Width;
// int originalHeight = this.Height;
// int step = 0;
// animationTimer.Interval = 50;
// animationTimer.Tick += (s, e) =>
// {
// step++;
// if (step <= 3)
// {
// // 缩小
// this.Size = new Size(originalWidth - step * 5, originalHeight - step * 5);
// }
// else if (step <= 6)
// {
// // 恢复
// this.Size = new Size(originalWidth - (6 - step) * 5, originalHeight - (6 - step) * 5);
// }
// else
// {
// animationTimer.Stop();
// animationTimer.Dispose();
// }
// };
// animationTimer.Start();
//}
// 调整点击动画尺寸(适应小球)
// 点击动画效果
private void AnimateClick()
{
Timer animationTimer = new Timer();
int originalWidth = this.Width;
int originalHeight = this.Height;
int step = 0;
animationTimer.Interval = 50;
animationTimer.Tick += (s, e) =>
{
step++;
if (step <= 3)
{
// 缩小
this.Size = new Size(originalWidth - step * 5, originalHeight - step * 5);
}
else if (step <= 6)
{
// 恢复
this.Size = new Size(originalWidth - (6 - step) * 5, originalHeight - (6 - step) * 5);
}
else
{
animationTimer.Stop();
animationTimer.Dispose();
}
};
animationTimer.Start();
}
// 自定义点击事件
public event Action OnBallClicked;
}