winform创建通知弹窗提示
winform创建通知消息
winform显示弹窗提示
//// 创建并显示通知
//var notification = new NotifyIcon
//{
// Visible = true,
// Icon = SystemIcons.Information,
// BalloonTipTitle = "二维码链接已复制",
// BalloonTipText = qrCodeData,
// BalloonTipIcon = ToolTipIcon.Info
//};
//notification.ShowBalloonTip(3000); // 显示3秒
// // 3秒后自动清理
// System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer { Interval = 3000 };
//timer.Tick += (s, e3) => {
// notification.Dispose();
// timer.Stop();
//};
//timer.Start();
自定义淡入淡出提示窗口
首先创建一个自定义提示窗体 FadeMessageForm
public class FadeMessageForm : Form
{
public FadeMessageForm(string message)
{
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 20,
Screen.PrimaryScreen.WorkingArea.Height - Height - 20);
Size = new Size(300, 100);
BackColor = Color.LightGreen;
var label = new Label
{
Text = message,
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("微软雅黑", 10)
};
Controls.Add(label);
// 设置淡入淡出效果
Opacity = 0;
Timer fadeInTimer = new Timer { Interval = 50 };
fadeInTimer.Tick += (s, e) => {
if ((Opacity += 0.1) >= 1)
{
fadeInTimer.Stop();
// 3秒后开始淡出
Timer fadeOutTimer = new Timer { Interval = 3000 };
fadeOutTimer.Tick += (s2, e2) => {
if ((Opacity -= 0.1) <= 0)
{
fadeOutTimer.Stop();
Close();
}
};
fadeOutTimer.Start();
}
};
fadeInTimer.Start();
}
}
然后在你的代码中使用:
if (!string.IsNullOrEmpty(qrCodeData)) { Clipboard.SetText(qrCodeData); new FadeMessageForm("二维码链接已复制到剪贴板").Show(); }完整代码
public class FadeMessageForm : Form
{
//添加阴影效果(使窗体看起来更立体):
protected override CreateParams CreateParams
{
get
{
const int CS_DROPSHADOW = 0x00020000;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
//圆角窗体设计
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var path = new GraphicsPath())
{
path.AddArc(0, 0, 20, 20, 180, 90);
path.AddArc(Width - 20, 0, 20, 20, 270, 90);
path.AddArc(Width - 20, Height - 20, 20, 20, 0, 90);
path.AddArc(0, Height - 20, 20, 20, 90, 90);
path.CloseFigure();
Region = new Region(path);
}
}
//添加鼠标点击关闭功能:
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Close();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Escape)
{
Close();
}
}
public FadeMessageForm(string message)
{
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.Manual;
//Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 20,
// Screen.PrimaryScreen.WorkingArea.Height - Height - 20);
// 替换原来的Location设置
Screen targetScreen = Screen.FromPoint(Cursor.Position); // 或指定其他屏幕
Location = new Point(targetScreen.WorkingArea.Right - Width - 20,
targetScreen.WorkingArea.Bottom - Height - 20); //多显示器支持:
Size = new Size(170, 50);
BackColor = Color.White;
// 设置为置顶窗体
TopMost = true; // 新增这行代码使窗体置顶
var label = new Label
{
Text = message,
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("微软雅黑", 10)
};
// 在添加label后计算合适的大小
//using (Graphics g = CreateGraphics())
//{
// SizeF textSize = g.MeasureString(message, label.Font);
// Size = new Size((int)textSize.Width + 40, (int)textSize.Height + 20);
//}
Controls.Add(label);
// 设置更平滑的淡入淡出效果
Opacity = 0;
Timer fadeInTimer = new Timer { Interval = 16 }; // 约60FPS (1000ms/60≈16ms)
double fadeStep = 0.05; // 减小每次变化的步长
fadeInTimer.Tick += (s, e) =>
{
if ((Opacity += fadeStep) >= 1)
{
Opacity = 1; // 确保最终完全不透明
fadeInTimer.Stop();
// 2秒后开始淡出
Timer fadeOutTimer = new Timer { Interval = 16 }; // 同样60FPS
fadeOutTimer.Tick += (s2, e2) =>
{
if ((Opacity -= fadeStep) <= 0)
{
Opacity = 0; // 确保完全透明
fadeOutTimer.Stop();
Close();
}
};
// 延迟2秒后开始淡出
Timer delayTimer = new Timer { Interval = 900 };
delayTimer.Tick += (s3, e3) =>
{
delayTimer.Stop();
fadeOutTimer.Start();
};
delayTimer.Start();
}
};
fadeInTimer.Start();
}
}