winform 关于Messagebox自动定时关闭和自定义提示框总按钮上文本的问题的整理

7 篇文章 0 订阅

如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实现,因为C#没有对Messagebox提供相关的关闭方法。

第一种方法(自定义winform窗口实现):

 

[csharp]  view plain copy
  1. [DllImport("user32.dll", CharSet = CharSet.Auto)]  
  2. private extern static bool MessageBeep(uint type);  
  3.   
  4. [DllImport("Shell32.dll")]  
  5. public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);  
  6.   
  7. private IntPtr[] largeIcon;  
  8. private IntPtr[] smallIcon;  
  9.   
  10. private MyMsgBox newMessageBox;  
  11. private Label frmTitle;  
  12. private Label frmMessage;  
  13. private PictureBox pIcon;  
  14. private FlowLayoutPanel flpButtons;  
  15. private Icon frmIcon;  
  16.   
  17. private Button btnOK;  
  18. private Button btnAbort;  
  19. private Button btnRetry;  
  20. private Button btnIgnore;  
  21. private Button btnCancel;  
  22. private Button btnYes;  
  23. private Button btnNo;  
  24.   
  25. private DialogResult CYReturnButton;  
  26. /// <summary>  
  27. /// 对话框图标  
  28. /// </summary>  
  29. public enum MyIcon  
  30. {  
  31.     Error,  
  32.     Explorer,  
  33.     Find,  
  34.     Information,  
  35.     Mail,  
  36.     Media,  
  37.     Print,  
  38.     Question,  
  39.     RecycleBinEmpty,  
  40.     RecycleBinFull,  
  41.     Stop,  
  42.     User,  
  43.     Warning  
  44. }  
  45. /// <summary>  
  46. /// 对话框上的按钮  
  47. /// </summary>  
  48. public enum MyButtons  
  49. {  
  50.     AbortRetryIgnore,  
  51.     OK,  
  52.     OKCancel,  
  53.     RetryCancel,  
  54.     YesNo,  
  55.     YesNoCancel  
  56. }  
  57. /// <summary>  
  58. /// 绘制对话框  
  59. /// </summary>  
  60. /// <param name="title">对话框标题</param>  
  61. private void BuildMessageBox(string title)  
  62. {  
  63.     newMessageBox = new MyMsgBox();  
  64.     newMessageBox.Text = title;  
  65.     newMessageBox.Size = new System.Drawing.Size(400, 200);  
  66.     newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;  
  67.     newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  
  68.     newMessageBox.Paint += new PaintEventHandler(newMessageBox_Paint);  
  69.     newMessageBox.BackColor = System.Drawing.Color.White;  
  70.   
  71.     TableLayoutPanel tlp = new TableLayoutPanel();  
  72.     tlp.RowCount = 3;  
  73.     tlp.ColumnCount = 0;  
  74.     tlp.Dock = System.Windows.Forms.DockStyle.Fill;  
  75.     tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22));  
  76.     tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));  
  77.     tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50));  
  78.     tlp.BackColor = System.Drawing.Color.Transparent;  
  79.     tlp.Padding = new Padding(2, 5, 2, 2);  
  80.   
  81.     frmTitle = new Label();  
  82.     frmTitle.Dock = System.Windows.Forms.DockStyle.Fill;  
  83.     frmTitle.BackColor = System.Drawing.Color.Transparent;  
  84.     frmTitle.ForeColor = System.Drawing.Color.White;  
  85.     frmTitle.Font = new Font("Tahoma", 9, FontStyle.Bold);  
  86.   
  87.     frmMessage = new Label();  
  88.     frmMessage.Dock = System.Windows.Forms.DockStyle.Fill;  
  89.     frmMessage.BackColor = System.Drawing.Color.White;  
  90.     frmMessage.Font = new Font("Tahoma", 9, FontStyle.Regular);  
  91.     frmMessage.Text = "hiii";  
  92.   
  93.     largeIcon = new IntPtr[250];  
  94.     smallIcon = new IntPtr[250];  
  95.     pIcon = new PictureBox();  
  96.     ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250);  
  97.   
  98.     flpButtons = new FlowLayoutPanel();  
  99.     flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;  
  100.     flpButtons.Padding = new Padding(0, 5, 5, 0);  
  101.     flpButtons.Dock = System.Windows.Forms.DockStyle.Fill;  
  102.     flpButtons.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);  
  103.   
  104.     TableLayoutPanel tlpMessagePanel = new TableLayoutPanel();  
  105.     tlpMessagePanel.BackColor = System.Drawing.Color.White;  
  106.     tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill;  
  107.     tlpMessagePanel.ColumnCount = 2;  
  108.     tlpMessagePanel.RowCount = 0;  
  109.     tlpMessagePanel.Padding = new Padding(4, 5, 4, 4);  
  110.     tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50));  
  111.     tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));  
  112.     tlpMessagePanel.Controls.Add(pIcon);  
  113.     tlpMessagePanel.Controls.Add(frmMessage);  
  114.   
  115.     tlp.Controls.Add(frmTitle);  
  116.     tlp.Controls.Add(tlpMessagePanel);  
  117.     tlp.Controls.Add(flpButtons);  
  118.     newMessageBox.Controls.Add(tlp);  
  119. }  
  120. /// <summary>  
  121. /// 显现对话框  
  122. /// </summary>  
  123. /// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>  
  124. /// <param name="Message">对话框中显示的提示内容</param>  
  125. /// <returns></returns>  
  126. public DialogResult ShowMsg(int timeout, string Message)  
  127. {  
  128.     if (timeout > 0)  
  129.     {  
  130.         StartTimer(timeout);  
  131.     }  
  132.     BuildMessageBox("");  
  133.     frmMessage.Text = Message;  
  134.     ShowOKButton();  
  135.     newMessageBox.ShowDialog();  
  136.     return CYReturnButton;  
  137. }  
  138. /// <summary>  
  139. /// 显示对话框  
  140. /// </summary>  
  141. /// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>  
  142. /// <param name="Message">对话框中显示的提示内容</param>  
  143. /// <param name="Title">对话框的标题</param>  
  144. /// <returns></returns>  
  145. public DialogResult ShowMsg(int timeout, string Message, string Title)  
  146. {  
  147.     if (timeout > 0)  
  148.     {  
  149.         StartTimer(timeout);  
  150.     }  
  151.     BuildMessageBox(Title);  
  152.     frmTitle.Text = Title;  
  153.     frmMessage.Text = Message;  
  154.     ShowOKButton();  
  155.     newMessageBox.ShowDialog();  
  156.     return CYReturnButton;  
  157. }  
  158. /// <summary>  
  159. /// 显示对话框  
  160. /// </summary>  
  161. /// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>  
  162. /// <param name="Message">对话框中显示的提示内容</param>  
  163. /// <param name="Title">对话框的标题</param>  
  164. /// <param name="MButtons">对话框中的按钮</param>  
  165. /// <returns></returns>  
  166. public DialogResult ShowMsg(int timeout, string Message, string Title, MyButtons MButtons)  
  167. {  
  168.     if (timeout > 0)  
  169.     {  
  170.         StartTimer(timeout);  
  171.     }  
  172.     BuildMessageBox(Title);               
[csharp]  view plain copy
  1. frmTitle.Text = Title;               
[csharp]  view plain copy
  1. frmMessage.Text = Message;               
[csharp]  view plain copy
  1. ButtonStatements(MButtons);               
[csharp]  view plain copy
  1. newMessageBox.ShowDialog();               
[csharp]  view plain copy
  1. return CYReturnButton;          
[csharp]  view plain copy
  1. }  
  2.         /// <summary>  
  3.         /// 显示对话框  
  4.         /// </summary>  
  5.         /// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>  
  6.         /// <param name="Message">对话框中显示的提示内容</param>  
  7.         /// <param name="Title">对话框的标题</param>  
  8.         /// <param name="MButtons">对话框中的按钮</param>  
  9.         /// <param name="MIcon">对话框中显示的图标</param>  
  10.         /// <returns></returns>  
  11.         public DialogResult ShowMsg(int timeout, string Message, string Title, MyButtons MButtons, MyIcon MIcon)  
  12.         {  
  13.             if (timeout > 0)  
  14.             {  
  15.                 StartTimer(timeout);  
  16.             }  
  17.             BuildMessageBox(Title);  
  18.             frmTitle.Text = Title;  
  19.             frmMessage.Text = Message;  
  20.             ButtonStatements(MButtons);  
  21.             IconStatements(MIcon);  
  22.             Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38);  
  23.             pIcon.Image = imageIcon;  
  24.             newMessageBox.ShowDialog();  
  25.             return CYReturnButton;  
  26.         }  
  27.         /// <summary>  
  28.         /// OK按钮单击事件  
  29.         /// </summary>  
  30.         /// <param name="sender"></param>  
  31.         /// <param name="e"></param>  
  32.         void btnOK_Click(object sender, EventArgs e)  
  33.         {  
  34.             CYReturnButton = DialogResult.OK;  
  35.             newMessageBox.Dispose();  
  36.         }  
  37.         /// <summary>  
  38.         /// Abort按钮单击事件  
  39.         /// </summary>  
  40.         /// <param name="sender"></param>  
  41.         /// <param name="e"></param>  
  42.         void btnAbort_Click(object sender, EventArgs e)  
  43.         {  
  44.             CYReturnButton = DialogResult.Abort;  
  45.             newMessageBox.Dispose();  
  46.         }  
  47.         /// <summary>  
  48.         /// Retry按钮单击事件  
  49.         /// </summary>  
  50.         /// <param name="sender"></param>  
  51.         /// <param name="e"></param>  
  52.         void btnRetry_Click(object sender, EventArgs e)  
  53.         {  
  54.             CYReturnButton = DialogResult.Retry;  
  55.             newMessageBox.Dispose();  
  56.         }  
  57.         /// <summary>  
  58.         /// Ignore按钮单击事件  
  59.         /// </summary>  
  60.         /// <param name="sender"></param>  
  61.         /// <param name="e"></param>  
  62.         void btnIgnore_Click(object sender, EventArgs e)  
  63.         {  
  64.             CYReturnButton = DialogResult.Ignore;  
  65.             newMessageBox.Dispose();  
  66.         }  
  67.         /// <summary>  
  68.         /// Cancel按钮单击事件  
  69.         /// </summary>  
  70.         /// <param name="sender"></param>  
  71.         /// <param name="e"></param>  
  72.         void btnCancel_Click(object sender, EventArgs e)  
  73.         {  
  74.             CYReturnButton = DialogResult.Cancel;  
  75.             newMessageBox.Dispose();  
  76.         }  
  77.         /// <summary>  
  78.         /// YSE按钮单击事件  
  79.         /// </summary>  
  80.         /// <param name="sender"></param>  
  81.         /// <param name="e"></param>  
  82.         void btnYes_Click(object sender, EventArgs e)  
  83.         {  
  84.             CYReturnButton = DialogResult.Yes;  
  85.             newMessageBox.Dispose();  
  86.         }  
  87.         /// <summary>  
  88.         /// NO按钮单击事件  
  89.         /// </summary>  
  90.         /// <param name="sender"></param>  
  91.         /// <param name="e"></param>  
  92.         void btnNo_Click(object sender, EventArgs e)  
  93.         {  
  94.             CYReturnButton = DialogResult.No;  
  95.             newMessageBox.Dispose();  
  96.         }  
  97.         /// <summary>  
  98.         /// OK按钮样式  
  99.         /// </summary>  
  100.         private void ShowOKButton()  
  101.         {  
  102.             btnOK = new Button();  
  103.             btnOK.Text = "确定";  
  104.             btnOK.Size = new System.Drawing.Size(80, 25);  
  105.             btnOK.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  106.             btnOK.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  107.             btnOK.Click += new EventHandler(btnOK_Click);  
  108.             flpButtons.Controls.Add(btnOK);  
  109.         }  
  110.         /// <summary>  
  111.         /// Abort按钮样式  
  112.         /// </summary>  
  113.         private void ShowAbortButton()  
  114.         {  
  115.             btnAbort = new Button();  
  116.             btnAbort.Text = "中止";  
  117.             btnAbort.Size = new System.Drawing.Size(80, 25);  
  118.             btnAbort.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  119.             btnAbort.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  120.             btnAbort.Click += new EventHandler(btnAbort_Click);  
  121.             flpButtons.Controls.Add(btnAbort);  
  122.         }  
  123.         /// <summary>  
  124.         /// Retry按钮样式  
  125.         /// </summary>  
  126.         private void ShowRetryButton()  
  127.         {  
  128.             btnRetry = new Button();  
  129.             btnRetry.Text = "重试";  
  130.             btnRetry.Size = new System.Drawing.Size(80, 25);  
  131.             btnRetry.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  132.             btnRetry.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  133.             btnRetry.Click += new EventHandler(btnRetry_Click);  
  134.             flpButtons.Controls.Add(btnRetry);  
  135.         }  
  136.         /// <summary>  
  137.         /// Ignore按钮样式  
  138.         /// </summary>  
  139.         private void ShowIgnoreButton()  
  140.         {  
  141.             btnIgnore = new Button();  
  142.             btnIgnore.Text = "忽略";  
  143.             btnIgnore.Size = new System.Drawing.Size(80, 25);  
  144.             btnIgnore.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  145.             btnIgnore.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  146.             btnIgnore.Click += new EventHandler(btnIgnore_Click);  
  147.             flpButtons.Controls.Add(btnIgnore);  
  148.         }  
  149.         /// <summary>  
  150.         /// Cancel按钮样式  
  151.         /// </summary>  
  152.         private void ShowCancelButton()  
  153.         {  
  154.             btnCancel = new Button();  
  155.             btnCancel.Text = "取消";  
  156.             btnCancel.Size = new System.Drawing.Size(80, 25);  
  157.             btnCancel.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  158.             btnCancel.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  159.             btnCancel.Click += new EventHandler(btnCancel_Click);  
  160.             flpButtons.Controls.Add(btnCancel);  
  161.         }  
  162.         /// <summary>  
  163.         /// Yes按钮样式  
  164.         /// </summary>  
  165.         private void ShowYesButton()  
  166.         {  
  167.             btnYes = new Button();  
  168.             btnYes.Text = "是";  
  169.             btnYes.Size = new System.Drawing.Size(80, 25);  
  170.             btnYes.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  171.             btnYes.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  172.             btnYes.Click += new EventHandler(btnYes_Click);  
  173.             flpButtons.Controls.Add(btnYes);  
  174.         }  
  175.         /// <summary>  
  176.         /// No按钮样式  
  177.         /// </summary>  
  178.         private void ShowNoButton()  
  179.         {  
  180.             btnNo = new Button();  
  181.             btnNo.Text = "否";  
  182.             btnNo.Size = new System.Drawing.Size(80, 25);  
  183.             btnNo.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);  
  184.             btnNo.Font = new Font("Tahoma", 8, FontStyle.Regular);  
  185.             btnNo.Click += new EventHandler(btnNo_Click);  
  186.             flpButtons.Controls.Add(btnNo);  
  187.         }  
  188.         /// <summary>  
  189.         /// 对话框中添加按钮  
  190.         /// </summary>  
  191.         /// <param name="MButtons">要添加的按钮(MyButtons枚举)</param>  
  192.         private void ButtonStatements(MyButtons MButtons)  
  193.         {  
  194.             if (MButtons == MyButtons.AbortRetryIgnore)  
  195.             {  
  196.                 ShowIgnoreButton();  
  197.                 ShowRetryButton();  
  198.                 ShowAbortButton();  
  199.             }  
  200.   
  201.             if (MButtons == MyButtons.OK)  
  202.             {  
  203.                 ShowOKButton();  
  204.             }  
  205.   
  206.             if (MButtons == MyButtons.OKCancel)  
  207.             {  
  208.                 ShowCancelButton();  
  209.                 ShowOKButton();  
  210.             }  
  211.   
  212.             if (MButtons == MyButtons.RetryCancel)  
  213.             {  
  214.                 ShowCancelButton();  
  215.                 ShowRetryButton();  
  216.             }  
  217.   
  218.             if (MButtons == MyButtons.YesNo)  
  219.             {  
  220.                 ShowNoButton();  
  221.                 ShowYesButton();  
  222.             }  
  223.   
  224.             if (MButtons == MyButtons.YesNoCancel)  
  225.             {  
  226.                 ShowCancelButton();  
  227.                 ShowNoButton();  
  228.                 ShowYesButton();  
  229.             }  
  230.         }  
  231.         /// <summary>  
  232.         /// 对话框中添加图标  
  233.         /// </summary>  
  234.         /// <param name="MIcon">要添加的图标(MyIcon枚举)</param>  
  235.         private void IconStatements(MyIcon MIcon)  
  236.         {  
  237.             if (MIcon == MyIcon.Error)  
  238.             {  
  239.                 MessageBeep(30);  
  240.                 frmIcon = Icon.FromHandle(largeIcon[109]);  
  241.             }  
  242.   
  243.             if (MIcon == MyIcon.Explorer)  
  244.             {  
  245.                 MessageBeep(0);  
  246.                 frmIcon = Icon.FromHandle(largeIcon[220]);  
  247.             }  
  248.   
  249.             if (MIcon == MyIcon.Find)  
  250.             {  
  251.                 MessageBeep(0);  
  252.                 frmIcon = Icon.FromHandle(largeIcon[22]);  
  253.             }  
  254.   
  255.             if (MIcon == MyIcon.Information)  
  256.             {  
  257.                 MessageBeep(0);  
  258.                 frmIcon = Icon.FromHandle(largeIcon[221]);  
  259.             }  
  260.   
  261.             if (MIcon == MyIcon.Mail)  
  262.             {  
  263.                 MessageBeep(0);  
  264.                 frmIcon = Icon.FromHandle(largeIcon[156]);  
  265.             }  
  266.   
  267.             if (MIcon == MyIcon.Media)  
  268.             {  
  269.                 MessageBeep(0);  
  270.                 frmIcon = Icon.FromHandle(largeIcon[116]);  
  271.             }  
  272.   
  273.             if (MIcon == MyIcon.Print)  
  274.             {  
  275.                 MessageBeep(0);  
  276.                 frmIcon = Icon.FromHandle(largeIcon[136]);  
  277.             }  
  278.   
  279.             if (MIcon == MyIcon.Question)  
  280.             {  
  281.                 MessageBeep(0);  
  282.                 frmIcon = Icon.FromHandle(largeIcon[23]);  
  283.             }  
  284.   
  285.             if (MIcon == MyIcon.RecycleBinEmpty)  
  286.             {  
  287.                 MessageBeep(0);  
  288.                 frmIcon = Icon.FromHandle(largeIcon[31]);  
  289.             }  
  290.   
  291.             if (MIcon == MyIcon.RecycleBinFull)  
  292.             {  
  293.                 MessageBeep(0);  
  294.                 frmIcon = Icon.FromHandle(largeIcon[32]);  
  295.             }  
  296.   
  297.             if (MIcon == MyIcon.Stop)  
  298.             {  
  299.                 MessageBeep(0);  
  300.                 frmIcon = Icon.FromHandle(largeIcon[27]);  
  301.             }  
  302.   
  303.             if (MIcon == MyIcon.User)  
  304.             {  
  305.                 MessageBeep(0);  
  306.                 frmIcon = Icon.FromHandle(largeIcon[170]);  
  307.             }  
  308.   
  309.             if (MIcon == MyIcon.Warning)  
  310.             {  
  311.                 MessageBeep(30);  
  312.                 frmIcon = Icon.FromHandle(largeIcon[217]);  
  313.             }  
  314.         }  
  315.   
  316.         void newMessageBox_Paint(object sender, PaintEventArgs e)  
  317.         {  
  318.             Graphics g = e.Graphics;  
  319.             Rectangle frmTitleL = new Rectangle(0, 0, (newMessageBox.Width / 2), 22);  
  320.             Rectangle frmTitleR = new Rectangle((newMessageBox.Width / 2), 0, (newMessageBox.Width / 2), 22);  
  321.             Rectangle frmMessageBox = new Rectangle(0, 0, (newMessageBox.Width - 1), (newMessageBox.Height - 1));  
  322.             LinearGradientBrush frmLGBL = new LinearGradientBrush(frmTitleL, Color.FromArgb(87, 148, 160), Color.FromArgb(209, 230, 243), LinearGradientMode.Horizontal);  
  323.             LinearGradientBrush frmLGBR = new LinearGradientBrush(frmTitleR, Color.FromArgb(209, 230, 243), Color.FromArgb(87, 148, 160), LinearGradientMode.Horizontal);  
  324.             Pen frmPen = new Pen(Color.FromArgb(63, 119, 143), 1);  
  325.             g.FillRectangle(frmLGBL, frmTitleL);  
  326.             g.FillRectangle(frmLGBR, frmTitleR);  
  327.             g.DrawRectangle(frmPen, frmMessageBox);  
  328.         }  
  329.   
  330.         private void StartTimer(int interval)  
  331.         {  
  332.             Timer timer = new Timer();  
  333.             timer.Interval = interval;  
  334.             timer.Tick += new EventHandler(Timer_Tick);  
  335.             timer.Enabled = true;  
  336.         }  
  337.   
  338.         private void Timer_Tick(object sender, EventArgs e)  
  339.         {  
  340.             newMessageBox.Close();  
  341.             //停止计时器  
  342.             ((Timer)sender).Enabled = false;  
  343.         }  


第二种方法(调用API实现):

1、实现定时关闭Messagebox,通过Messagebox对话框的标题查找相应的句柄来进行关闭操作

[csharp]  view plain copy
  1. public class MessageBoxTimeOut  
  2.     {  
  3.         private string _caption;  
  4.         private string username;  
  5.         private string pwd;  
  6.         public DialogResult Show(int timeout, string text, string caption, MessageBoxButtons buttons)  
  7.         {  
  8.             this._caption = caption;  
  9.             StartTimer(timeout);  
  10.             DialogResult dr = MessageBox.Show(text, caption, buttons, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);  
  11.             return dr;  
  12.         }  
  13.         private void StartTimer(int interval)  
  14.         {  
  15.             Timer timer = new Timer();  
  16.             timer.Interval = interval;  
  17.             timer.Tick += new EventHandler(Timer_Tick);  
  18.             timer.Enabled = true;  
  19.         }  
  20.         private void Timer_Tick(object sender, EventArgs e)  
  21.         {  
  22.             KillMessageBox();  
  23.             //停止计时器  
  24.             ((Timer)sender).Enabled = false;  
  25.         }  
  26.         [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]  
  27.         private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);  
  28.         [DllImport("User32.dll", CharSet = CharSet.Auto)]  
  29.         public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);  
  30.         public const int WM_CLOSE = 0x10;  
  31.         public void KillMessageBox()  
  32.         {  
  33.             //查找MessageBox的弹出窗口,注意对应标题  
  34.             IntPtr ptr = FindWindow(nullthis._caption);  
  35.             if (ptr != IntPtr.Zero)  
  36.             {  
  37.                 //查找到窗口则关闭  
  38.                 PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);  
  39.             }  
  40.         }  
  41.     }  

2、关闭Messagebox对话框中按钮的文本文字

[csharp]  view plain copy
  1. public class MessageBoxManager  
  2.     {  
  3.         private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);  
  4.         private delegate bool EnumChildProc(IntPtr hWnd, IntPtr lParam);  
  5.   
  6.         private const int WH_CALLWNDPROCRET = 12;  
  7.         private const int WM_DESTROY = 0x0002;  
  8.         private const int WM_INITDIALOG = 0x0110;  
  9.         private const int WM_TIMER = 0x0113;  
  10.         private const int WM_USER = 0x400;  
  11.         private const int DM_GETDEFID = WM_USER + 0;  
  12.   
  13.         private const int MBOK = 1;  
  14.         private const int MBCancel = 2;  
  15.         private const int MBAbort = 3;  
  16.         private const int MBRetry = 4;  
  17.         private const int MBIgnore = 5;  
  18.         private const int MBYes = 6;  
  19.         private const int MBNo = 7;  
  20.   
  21.   
  22.         [DllImport("user32.dll")]  
  23.         private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);  
  24.   
  25.         [DllImport("user32.dll")]  
  26.         private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);  
  27.   
  28.         [DllImport("user32.dll")]  
  29.         private static extern int UnhookWindowsHookEx(IntPtr idHook);  
  30.               
  31.         [DllImport("user32.dll")]  
  32.         private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);  
  33.   
  34.         [DllImport("user32.dll", EntryPoint = "GetWindowTextLengthW", CharSet = CharSet.Unicode)]  
  35.         private static extern int GetWindowTextLength(IntPtr hWnd);  
  36.   
  37.         [DllImport("user32.dll", EntryPoint = "GetWindowTextW", CharSet = CharSet.Unicode)]  
  38.         private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);  
  39.   
  40.         [DllImport("user32.dll")]  
  41.         private static extern int EndDialog(IntPtr hDlg, IntPtr nResult);  
  42.   
  43.         [DllImport("user32.dll")]  
  44.         private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);  
  45.   
  46.         [DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)]  
  47.         private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);  
  48.   
  49.         [DllImport("user32.dll")]  
  50.         private static extern int GetDlgCtrlID(IntPtr hwndCtl);  
  51.   
  52.         [DllImport("user32.dll")]  
  53.         private static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);  
  54.   
  55.         [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]  
  56.         private static extern bool SetWindowText(IntPtr hWnd, string lpString);  
  57.   
  58.   
  59.         [StructLayout(LayoutKind.Sequential)]  
  60.         public struct CWPRETSTRUCT  
  61.         {  
  62.             public IntPtr lResult;  
  63.             public IntPtr lParam;  
  64.             public IntPtr wParam;  
  65.             public uint   message;  
  66.             public IntPtr hwnd;  
  67.         };  
  68.   
  69.         private static HookProc hookProc;  
  70.         private static EnumChildProc enumProc;  
  71.         [ThreadStatic]  
  72.         private static IntPtr hHook;  
  73.         [ThreadStatic]  
  74.         private static int nButton;  
  75.   
  76.         /// <summary>  
  77.         /// OK text  
  78.         /// </summary>  
  79.         public static string OK = "&OK";  
  80.         /// <summary>  
  81.         /// Cancel text  
  82.         /// </summary>  
  83.         public static string Cancel = "&Cancel";  
  84.         /// <summary>  
  85.         /// Abort text  
  86.         /// </summary>  
  87.         public static string Abort = "&Abort";  
  88.         /// <summary>  
  89.         /// Retry text  
  90.         /// </summary>  
  91.         public static string Retry = "&Retry";  
  92.         /// <summary>  
  93.         /// Ignore text  
  94.         /// </summary>  
  95.         public static string Ignore = "&Ignore";  
  96.         /// <summary>  
  97.         /// Yes text  
  98.         /// </summary>  
  99.         public static string Yes = "&Yes";  
  100.         /// <summary>  
  101.         /// No text  
  102.         /// </summary>  
  103.         public static string No = "&No";  
  104.   
  105.         static MessageBoxManager()  
  106.         {  
  107.             hookProc = new HookProc(MessageBoxHookProc);  
  108.             enumProc = new EnumChildProc(MessageBoxEnumProc);  
  109.             hHook = IntPtr.Zero;  
  110.         }  
  111.           
  112.         public static void Register()  
  113.         {  
  114.             if (hHook != IntPtr.Zero)  
  115.                 throw new NotSupportedException("One hook per thread allowed.");  
  116.             hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());  
  117.         }  
  118.   
  119.         public static void Unregister()  
  120.         {  
  121.             if (hHook != IntPtr.Zero)  
  122.             {  
  123.                 UnhookWindowsHookEx(hHook);  
  124.                 hHook = IntPtr.Zero;  
  125.             }  
  126.         }  
  127.           
  128.         private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)  
  129.         {  
  130.             if (nCode < 0)  
  131.                 return CallNextHookEx(hHook, nCode, wParam, lParam);  
  132.   
  133.             CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));  
  134.             IntPtr hook = hHook;  
  135.   
  136.             if (msg.message == WM_INITDIALOG)  
  137.             {  
  138.                 int nLength = GetWindowTextLength(msg.hwnd);  
  139.                 StringBuilder className = new StringBuilder(10);  
  140.                 GetClassName(msg.hwnd, className, className.Capacity);  
  141.                 if (className.ToString() == "#32770")  
  142.                 {  
  143.                     nButton = 0;  
  144.                     EnumChildWindows(msg.hwnd, enumProc, IntPtr.Zero);  
  145.                     if (nButton == 1)  
  146.                     {  
  147.                         IntPtr hButton = GetDlgItem(msg.hwnd, MBCancel);  
  148.                         if (hButton != IntPtr.Zero)  
  149.                             SetWindowText(hButton, OK);  
  150.                     }  
  151.                 }  
  152.             }  
  153.   
  154.             return CallNextHookEx(hook, nCode, wParam, lParam);  
  155.         }  
  156.   
  157.         private static bool MessageBoxEnumProc(IntPtr hWnd, IntPtr lParam)  
  158.         {  
  159.             StringBuilder className = new StringBuilder(10);  
  160.             GetClassName(hWnd, className, className.Capacity);  
  161.             if (className.ToString() == "Button")  
  162.             {  
  163.                 int ctlId = GetDlgCtrlID(hWnd);  
  164.                 switch (ctlId)  
  165.                 {  
  166.                     case MBOK:  
  167.                         SetWindowText(hWnd, OK);  
  168.                         break;  
  169.                     case MBCancel:  
  170.                         SetWindowText(hWnd, Cancel);  
  171.                         break;  
  172.                     case MBAbort:  
  173.                         SetWindowText(hWnd, Abort);  
  174.                         break;  
  175.                     case MBRetry:  
  176.                         SetWindowText(hWnd, Retry);  
  177.                         break;  
  178.                     case MBIgnore:  
  179.                         SetWindowText(hWnd, Ignore);  
  180.                         break;  
  181.                     case MBYes:  
  182.                         SetWindowText(hWnd, Yes);  
  183.                         break;  
  184.                     case MBNo:  
  185.                         SetWindowText(hWnd, No);  
  186.                         break;  
  187.   
  188.                 }  
  189.                 nButton++;  
  190.             }  
  191.   
  192.             return true;  
  193.         }  
  194.   
  195.   
  196.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值