自定义消息框

是否可以创建我自己的自定义MessageBox,我可以在其中添加图像而不是只添加字符串?

我也想要这个function,所以我创建了WPFCustomMessageBox ,它是本机Windows / .NET MessageBox的WPF克隆,它支持自定义按钮文本等额外function。

WPFCustomMessageBox使用静态方法,就像标准.NET MessageBox一样,因此您可以在不修改任何代码的情况下即插即用新库。 最重要的是,我设计了这个控件,使它看起来与原始的MessageBox完全相同

WPFCustomMessageBox示例

我创建了这个库,因为我想在我的MessageBox按钮中使用动词来帮助用户更好地理解按钮的function 。 使用此库,您可以为用户提供按钮说明,例如Save/Don't SaveEject Fuel Rods/Don't do it! 而不是标准的OK/Cancel或是Yes/No (如果你愿意,你也可以使用它们)。

WPFCustomMessageBox消息框返回标准.NET MessageBoxResults 。 它还提供与MessageBoxIcons和自定义消息框标题等原始MessageBox相同的function。

WPFCustomMessageBox是开源的,所以你可以在这里获取代码或进行改进: https : //github.com/evanwon/WPFCustomMessageBox

您可以通过NuGet将WPFCustomMessage添加到您的项目中: https ://www.nuget.org/packages/WPFCustomMessageBox/

查看这些文章,了解如何使用WPF创建自定义对话框

  • 用于Vista和XP的WPF Common TaskDialog 。

alt text http://karlshifflett.files.wordpress.com/2007/12/sampleone.jpg

  • WPF MessageBox – 自定义控件

以下是创建自己的消息框所需的代码:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyStuff { public class MyLabel : Label { public static Label Set(string Text = "", Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color()) { Label l = new Label(); l.Text = Text; l.Font = (Font == null) ? new Font("Calibri", 12) : Font; l.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor; l.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor; l.AutoSize = true; return l; } } public class MyButton : Button { public static Button Set(string Text = "", int Width = 102, int Height = 30, Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color()) { Button b = new Button(); b.Text = Text; b.Width = Width; b.Height = Height; b.Font = (Font == null) ? new Font("Calibri", 12) : Font; b.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor; b.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor; b.UseVisualStyleBackColor = (b.BackColor == SystemColors.Control); return b; } } public class MyImage : PictureBox { public static PictureBox Set(string ImagePath = null, int Width = 60, int Height = 60) { PictureBox i = new PictureBox(); if (ImagePath != null) { i.BackgroundImageLayout = ImageLayout.Zoom; i.Location = new Point(9, 9); i.Margin = new Padding(3, 3, 2, 3); i.Size = new Size(Width, Height); i.TabStop = false; i.Visible = true; i.BackgroundImage = Image.FromFile(ImagePath); } else { i.Visible = true; i.Size = new Size(0, 0); } return i; } } public partial class MyMessageBox : Form { private MyMessageBox() { this.panText = new FlowLayoutPanel(); this.panButtons = new FlowLayoutPanel(); this.SuspendLayout(); // // panText // this.panText.Parent = this; this.panText.AutoScroll = true; this.panText.AutoSize = true; this.panText.AutoSizeMode = AutoSizeMode.GrowAndShrink; //this.panText.Location = new Point(90, 90); this.panText.Margin = new Padding(0); this.panText.MaximumSize = new Size(500, 300); this.panText.MinimumSize = new Size(108, 50); this.panText.Size = new Size(108, 50); // // panButtons // this.panButtons.AutoSize = true; this.panButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink; this.panButtons.FlowDirection = FlowDirection.RightToLeft; this.panButtons.Location = new Point(89, 89); this.panButtons.Margin = new Padding(0); this.panButtons.MaximumSize = new Size(580, 150); this.panButtons.MinimumSize = new Size(108, 0); this.panButtons.Size = new Size(108, 35); // // MyMessageBox // this.AutoScaleDimensions = new SizeF(8F, 19F); this.AutoScaleMode = AutoScaleMode.Font; this.ClientSize = new Size(206, 133); this.Controls.Add(this.panButtons); this.Controls.Add(this.panText); this.Font = new Font("Calibri", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = FormBorderStyle.FixedSingle; this.Margin = new Padding(4); this.MaximizeBox = false; this.MinimizeBox = false; this.MinimumSize = new Size(168, 132); this.Name = "MyMessageBox"; this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = FormStartPosition.CenterScreen; this.ResumeLayout(false); this.PerformLayout(); } public static string Show(Label Label, string Title = "", List 

这花了我2天的时间来写。 我希望它适用于任何需要它的人。

我已经通过标准WPF控件模板实现了完全可自定义的WPF MessageBox:

http://blogs.microsoft.co.il/blogs/arik/archive/2011/05/26/a-customizable-wpf-messagebox.aspx

特征

  • WPFMessageBox类具有与当前WPF MessageBox类完全相同的接口。
  • 实现为自定义控件,因此可通过标准WPF控件模板完全自定义。
  • 有一个默认的控件模板,看起来像标准的MessageBox。
  • 支持所有常见类型的消息框:错误,警告,问题和信息。
  • 打开标准MessageBox时会发出相同的“嘟嘟”声。
  • 按Escape按钮作为标准MessageBox时支持相同的行为。
  • 提供与标准MessageBox相同的系统菜单,包括在消息框处于Yes-No模式时禁用“关闭”按钮。
  • 处理右对齐和右到左操作系统,与标准MessageBox相同。
  • 支持将所有者窗口设置为WinForms窗体控件。

当然。 我通过inheritanceSystem.Windows.Window并添加显示各种内容(图像,文本和控件)的容量,然后在该窗口上调用ShowDialog()来完成它:

 public partial class MyMessageBox : Window { // perhaps a helper method here public static bool? Show(String message, BitmapImage image) { // NOTE: Message and Image are fields created in the XAML markup MyMessageBox msgBox = new MyMessageBox() { Message.Text = message, Image.Source = image }; return msgBox.ShowDialog(); } } 

在XAML中,这样的事情:

       

我喜欢你,我找到了这个来源并修改了我想要的方式,你可以从中获得最大的收益

这里是链接输入链接描述这是默认情况下的样子 在此处输入图像描述