C#窗口應(yīng)用自定義彈窗Toast

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

            };

            // 若要調(diào)整透明度,可使用以下代碼

            // popupForm.BackColor = Color.FromArgb(255, 255, 255); // 完全不透明的白色

            //popupForm.Opacity = 1; // 設(shè)置 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、調(diào)用方法


private void button1_Click(object sender, RibbonControlEventArgs e)

{

    Toast toast = new Toast();

    string iconPath = "C:\\Users\\PZB\\Downloads\\wanc.png"; // 替換為實際圖標(biāo)路徑

    toast.ShowToast("處理完成!", iconPath);

}

3、效果圖


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容