123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Bird_tool
- {
- public static class TopMostMessageBox
- {
- public static void Show(string message)
- {
- // 确保在UI线程执行
- if (Application.OpenForms.Count > 0)
- {
- Form mainForm = Application.OpenForms[0];
- if (mainForm.InvokeRequired)
- {
- mainForm.Invoke(new Action(() => CreateMessageBox(message)));
- }
- else
- {
- CreateMessageBox(message);
- }
- }
- else
- {
- // 如果没有主窗体,直接创建
- CreateMessageBox(message);
- }
- }
- private static void CreateMessageBox(string message)
- {
- using (var form = new Form
- {
- Width = 350,
- Height = 180,
- FormBorderStyle = FormBorderStyle.FixedDialog,
- MaximizeBox = false,
- MinimizeBox = false,
- StartPosition = FormStartPosition.CenterScreen,
- Text = "系统提示",
- TopMost = true,
- BackColor = System.Drawing.Color.WhiteSmoke,
- Padding = new Padding(10),
- Font = new System.Drawing.Font("微软雅黑", 10)
- })
- {
- // 消息标签 - 带自动换行
- var label = new Label
- {
- Text = message,
- Dock = DockStyle.Fill,
- TextAlign = System.Drawing.ContentAlignment.MiddleCenter,
- AutoSize = false,
- Padding = new Padding(0, 10, 0, 20)
- };
- // 确定按钮
- var panel = new Panel
- {
- Dock = DockStyle.Bottom,
- Height = 50,
- BackColor = Color.Transparent
- };
- // 确定按钮
- var button = new Button
- {
- Text = "确定",
- Size = new Size(80, 35),
- Location = new Point((panel.Width - 80) / 2, 5)
- };
- button.Click += (s, e) => form.Close();
-
- // 添加控件
- panel.Controls.Add(button);
- form.Controls.Add(label);
- form.Controls.Add(panel);
- // 为主窗体添加激活事件处理程序
- foreach (Form mainForm in Application.OpenForms)
- {
- if (mainForm != form)
- {
- // 安全添加事件处理
- mainForm.Activated += (s, e) =>
- {
- if (!form.IsDisposed)
- {
- form.TopMost = true;
- }
- };
- }
- }
- form.AcceptButton = button;
- form.ShowDialog();
- }
- }
- }
- }
|