C# MessageBox.Show()自定义图标?

C# MessageBox.Show()自定义图标?,c#,.net,icons,messagebox,C#,.net,Icons,Messagebox,我想在MessageBox.Show(“Message”、“Title”、MessageBoxButton.OK、MeesageBoxIcon.myIcon)”方法中使用自定义图标 有什么建议吗?您不能覆盖MessageBox的此行为。解决方案是要么使用一些自定义消息框,选中,要么实现您自己的MessageBoxForm,并将您的自定义设置添加到其中,选中。我刚才写了一个,它的工作原理与常规的messagebox类完全相同 CustomMessageBox(类): 消息表单(设计器代码): 部分

我想在
MessageBox.Show(“Message”、“Title”、MessageBoxButton.OK、MeesageBoxIcon.myIcon)”方法中使用自定义图标


有什么建议吗?

您不能覆盖
MessageBox
的此行为。解决方案是要么使用一些自定义消息框,选中,要么实现您自己的
MessageBoxForm
,并将您的自定义设置添加到其中,选中。

我刚才写了一个,它的工作原理与常规的messagebox类完全相同

CustomMessageBox(类):

消息表单(设计器代码):

部分类消息表单
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.picImage=new System.Windows.Forms.PictureBox();
this.lblText=new System.Windows.Forms.Label();
this.btnYes=new Dark.WinForms.Controls.dButton();
this.btnNo=new Dark.WinForms.Controls.dButton();
this.btnCancel=new Dark.WinForms.Controls.dButton();
this.btnOK=new Dark.WinForms.Controls.dButton();
((System.ComponentModel.ISupportInitialize)(this.picImage)).BeginInit();
这个.SuspendLayout();
//
//图片
//
this.picImage.ErrorImage=null;
this.picImage.InitialImage=null;
this.picImage.Location=新系统.图纸.点(15,15);
this.picImage.Name=“picImage”;
this.picImage.Size=新系统.Drawing.Size(64,64);
this.picImage.TabIndex=0;
this.picImage.TabStop=false;
//
//lblText
//
this.lblText.Anchor=((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|系统、窗口、窗体、主播样式(左)
|System.Windows.Forms.AnchorStyles.Right);
this.lblText.AutoSize=true;
this.lblText.Location=新系统图纸点(85,15);
this.lblText.MaximumSize=新系统图纸尺寸(294,0);
this.lblText.Name=“lblText”;
this.lblText.Size=新系统图纸尺寸(28,13);
this.lblText.TabIndex=0;
this.lblText.Text=“Text”;
//
//Btneyes
//
this.btnYes.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right));
this.btnYes.FocusDuesEnabled=false;
this.btnYes.Location=新系统图纸点(139,88);
this.btnYes.Name=“btnYes”;
this.btnYes.Size=新系统图纸尺寸(75,23);
this.btnYes.TabIndex=2;
this.btnYes.Text=“是”;
this.btnYes.Tooltip=“”;
this.btnYes.UseVisualStyleBackColor=true;
this.btnYes.Click+=新建System.EventHandler(this.btnYes\u Click);
//
//btnNo
//
this.btnNo.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right));
this.btnNo.FocusDuesEnabled=false;
this.btnNo.Location=新系统图纸点(220,88);
this.btnNo.Name=“btnNo”;
this.btnNo.Size=新系统图纸尺寸(75,23);
this.btnNo.TabIndex=3;
this.btnNo.Text=“否”;
this.btnNo.Tooltip=“”;
this.btnNo.UseVisualStyleBackColor=true;
this.btnNo.Click+=新建System.EventHandler(this.btnNo\u Click);
//
//BTNCENCEL
//
this.btnCancel.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right));
this.btnCancel.FocusDuesEnabled=false;
this.btnCancel.Location=新系统图纸点(301,88);
this.btnCancel.Name=“btnCancel”;
this.btnCancel.Size=新系统图纸尺寸(75,23);
this.btnCancel.TabIndex=1;
this.btnCancel.Text=“取消”;
this.btnCancel.Tooltip=“”;
this.btnCancel.UseVisualStyleBackColor=true;
this.btnCancel.Click+=新建System.EventHandler(this.btnCancel\u Click);
//
//btnOK
//
this.btnOK.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right));
this.btnOK.FocusDuesEnabled=false;
this.btnOK.Location=新系统图纸点(220,88);
this.btnOK.Name=“btnOK”;
this.btnOK.Size=新系统图纸尺寸(75,23);
this.btnOK.TabIndex=4;
this.btnOK.Text=“确定”;
this.btnOK.Tooltip=“”;
this.btnOK.UseVisualStyleBackColor=true;
this.btnOK.Click+=new System.EventHandler(this.btnOK\u Click);
//
//消息形式
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(394129);
this.Controls.Add(this.btnYes);
this.Controls.Add(this.btnNo);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.picImage);
this.Controls.Add(this.lblText);
this.Controls.Add(this.btnOK);
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedDialog;
this.ebox=false;
这个是电子邮箱
using System;
using System.Drawing;
using System.Windows.Forms;

public static class CustomMessageBox
{
    public static DialogResult Show(string Text, string Title, eDialogButtons Buttons, Image Image)
    {
        MessageForm message = new MessageForm();
        message.Text = Title;

        if (Image.Height < 0 || Image.Height > 64)
            throw new Exception("Invalid image height. Valid height ranges from 0 to 64.");
        else if (Image.Width < 0 || Image.Width > 64)
            throw new Exception("Invalid image width. Valid width ranges from 0 to 64.");
        else
        {
            message.picImage.Image = Image;
            message.lblText.Text = Text;

            switch (Buttons)
            {
                case eDialogButtons.OK:
                    message.btnYes.Visible = false;
                    message.btnNo.Visible = false;
                    message.btnCancel.Visible = false;
                    message.btnOK.Location = message.btnCancel.Location;
                    break;
                case eDialogButtons.OKCancel:
                    message.btnYes.Visible = false;
                    message.btnNo.Visible = false;
                    break;
                case eDialogButtons.YesNo:
                    message.btnOK.Visible = false;
                    message.btnCancel.Visible = false;
                    message.btnYes.Location = message.btnOK.Location;
                    message.btnNo.Location = message.btnCancel.Location;
                    break;
                case eDialogButtons.YesNoCancel:
                    message.btnOK.Visible = false;
                    break;
            }

            if (message.lblText.Height > 64)
                message.Height = (message.lblText.Top + message.lblText.Height) + 78;

            return (message.ShowDialog());
        }
    }

    public enum eDialogButtons { OK, OKCancel, YesNo, YesNoCancel }
}
using System;
using System.Windows.Forms;

internal partial class MessageForm : Form
{
    internal MessageForm() => InitializeComponent();

    private void btnYes_Click(object sender, EventArgs e) =>
        DialogResult = DialogResult.Yes;

    private void btnNo_Click(object sender, EventArgs e) =>
        DialogResult = DialogResult.No;

    private void btnCancel_Click(object sender, EventArgs e) =>
        DialogResult = DialogResult.Cancel;

    private void btnOK_Click(object sender, EventArgs e) =>
        DialogResult = DialogResult.OK;
}
partial class MessageForm
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.picImage = new System.Windows.Forms.PictureBox();
        this.lblText = new System.Windows.Forms.Label();
        this.btnYes = new Dark.WinForms.Controls.dButton();
        this.btnNo = new Dark.WinForms.Controls.dButton();
        this.btnCancel = new Dark.WinForms.Controls.dButton();
        this.btnOK = new Dark.WinForms.Controls.dButton();
        ((System.ComponentModel.ISupportInitialize)(this.picImage)).BeginInit();
        this.SuspendLayout();
        //
        // picImage
        //
        this.picImage.ErrorImage = null;
        this.picImage.InitialImage = null;
        this.picImage.Location = new System.Drawing.Point(15, 15);
        this.picImage.Name = "picImage";
        this.picImage.Size = new System.Drawing.Size(64, 64);
        this.picImage.TabIndex = 0;
        this.picImage.TabStop = false;
        //
        // lblText
        //
        this.lblText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.lblText.AutoSize = true;
        this.lblText.Location = new System.Drawing.Point(85, 15);
        this.lblText.MaximumSize = new System.Drawing.Size(294, 0);
        this.lblText.Name = "lblText";
        this.lblText.Size = new System.Drawing.Size(28, 13);
        this.lblText.TabIndex = 0;
        this.lblText.Text = "Text";
        //
        // btnYes
        //
        this.btnYes.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.btnYes.FocusDuesEnabled = false;
        this.btnYes.Location = new System.Drawing.Point(139, 88);
        this.btnYes.Name = "btnYes";
        this.btnYes.Size = new System.Drawing.Size(75, 23);
        this.btnYes.TabIndex = 2;
        this.btnYes.Text = "Yes";
        this.btnYes.Tooltip = "";
        this.btnYes.UseVisualStyleBackColor = true;
        this.btnYes.Click += new System.EventHandler(this.btnYes_Click);
        //
        // btnNo
        //
        this.btnNo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.btnNo.FocusDuesEnabled = false;
        this.btnNo.Location = new System.Drawing.Point(220, 88);
        this.btnNo.Name = "btnNo";
        this.btnNo.Size = new System.Drawing.Size(75, 23);
        this.btnNo.TabIndex = 3;
        this.btnNo.Text = "No";
        this.btnNo.Tooltip = "";
        this.btnNo.UseVisualStyleBackColor = true;
        this.btnNo.Click += new System.EventHandler(this.btnNo_Click);
        //
        // btnCancel
        //
        this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.btnCancel.FocusDuesEnabled = false;
        this.btnCancel.Location = new System.Drawing.Point(301, 88);
        this.btnCancel.Name = "btnCancel";
        this.btnCancel.Size = new System.Drawing.Size(75, 23);
        this.btnCancel.TabIndex = 1;
        this.btnCancel.Text = "Cancel";
        this.btnCancel.Tooltip = "";
        this.btnCancel.UseVisualStyleBackColor = true;
        this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
        //
        // btnOK
        //
        this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.btnOK.FocusDuesEnabled = false;
        this.btnOK.Location = new System.Drawing.Point(220, 88);
        this.btnOK.Name = "btnOK";
        this.btnOK.Size = new System.Drawing.Size(75, 23);
        this.btnOK.TabIndex = 4;
        this.btnOK.Text = "OK";
        this.btnOK.Tooltip = "";
        this.btnOK.UseVisualStyleBackColor = true;
        this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
        //
        // MessageForm
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(394, 129);
        this.Controls.Add(this.btnYes);
        this.Controls.Add(this.btnNo);
        this.Controls.Add(this.btnCancel);
        this.Controls.Add(this.picImage);
        this.Controls.Add(this.lblText);
        this.Controls.Add(this.btnOK);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "MessageForm";
        this.Padding = new System.Windows.Forms.Padding(15);
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Title";
        ((System.ComponentModel.ISupportInitialize)(this.picImage)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    internal Dark.WinForms.Controls.dButton btnCancel;
    internal Dark.WinForms.Controls.dButton btnNo;
    internal Dark.WinForms.Controls.dButton btnYes;
    internal Dark.WinForms.Controls.dButton btnOK;
    internal System.Windows.Forms.PictureBox picImage;
    internal System.Windows.Forms.Label lblText;
}