1、新建这个自定义类Toast.cs,其中ExcelAddIn1要改为自己项目包名。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ExcelAddIn1
{
internal class Toast
{
private CustomForm popupForm;
private TableLayoutPanel layoutPanel;
private PictureBox checkIcon;
private Label messageLabel;
private const int maxIconSize = 20;
private const int marginHorizontal = 15;
private const int cornerRadius = 15;
private const int spacing = 5;
public Toast()
{
InitializeForm();
InitializeControls();
}
private void InitializeForm()
{
popupForm = new CustomForm
{
FormBorderStyle = FormBorderStyle.None,
StartPosition = FormStartPosition.CenterScreen,
// 修改背景颜色为白色
BackColor = Color.LightGreen,
TopMost = true,
ShowIcon = false,
ShowInTaskbar = false
};
// 若要调整透明度,可使用以下代码
// popupForm.BackColor = Color.FromArgb(255, 255, 255); // 完全不透明的白色
//popupForm.Opacity = 1; // 设置 95% 透明度
}
private void InitializeControls()
{
layoutPanel = new TableLayoutPanel
{
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Padding = new Padding(spacing),
ColumnCount = 2,
RowCount = 1,
CellBorderStyle = TableLayoutPanelCellBorderStyle.None,
RowStyles = { new RowStyle(SizeType.Percent, 100F) },
};
layoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
layoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
checkIcon = new PictureBox
{
SizeMode = PictureBoxSizeMode.Zoom,
Margin = new Padding(spacing),
Dock = DockStyle.Fill,
Anchor = AnchorStyles.None
};
messageLabel = new Label
{
AutoSize = true,
Font = new Font("微软雅黑", 11, FontStyle.Regular),
ForeColor = Color.Black,
Margin = new Padding(spacing),
Dock = DockStyle.Fill,
Anchor = AnchorStyles.None,
TextAlign = ContentAlignment.MiddleLeft
};
layoutPanel.Controls.Add(checkIcon, 0, 0);
layoutPanel.Controls.Add(messageLabel, 1, 0);
popupForm.Controls.Add(layoutPanel);
}
public void ShowToast(string message, string iconPath = "", int duration = 2000)
{
checkIcon.Visible = false;
messageLabel.Text = message;
if (!string.IsNullOrEmpty(iconPath))
{
try
{
Image iconImage = Image.FromFile(iconPath);
float ratio = Math.Min((float)maxIconSize / iconImage.Width, (float)maxIconSize / iconImage.Height);
int newWidth = (int)(iconImage.Width * ratio);
int newHeight = (int)(iconImage.Height * ratio);
checkIcon.Size = new Size(newWidth, newHeight);
checkIcon.Image = iconImage;
checkIcon.Visible = true;
}
catch
{
checkIcon.Visible = false;
}
}
popupForm.Size = layoutPanel.Size;
using (GraphicsPath path = new GraphicsPath())
{
path.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
path.AddArc(popupForm.Width - cornerRadius, 0, cornerRadius, cornerRadius, 270, 90);
path.AddArc(popupForm.Width - cornerRadius, popupForm.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path.AddArc(0, popupForm.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path.CloseFigure();
popupForm.Region = new Region(path);
}
popupForm.Show();
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = duration;
timer.Tick += (sender, e) =>
{
popupForm.Close();
timer.Stop();
};
timer.Start();
}
}
internal class CustomForm : Form
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
}
}
2、调用方法
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Toast toast = new Toast();
string iconPath = "C:\\Users\\PZB\\Downloads\\wanc.png"; // 替换为实际图标路径
toast.ShowToast("处理完成!", iconPath);
}
3、效果图

