重绘DevExpress的XtraMessageBox消息提示框控件
阅读原文时间:2021年04月28日阅读:1

先来看提示框,可以看到框其实是一个去掉最大化、最小化按钮后的窗体,窗体的内容就是我们想要提示的内容,重绘提示框其实就是重绘窗体以及中间部分的内容。

首先重绘窗体,消息提示框的窗体不是XtraForm而是XtraMessageBoxForm,重写CreateFormBorderPainter()方法。

internal class MessageBoxForm : XtraMessageBoxForm
{
protected override FormPainter CreateFormBorderPainter()
{
return new MessageBoxFormPainter(this, LookAndFeel);
}

}

MessageBoxFormPainter继承于FormPainter,窗体样式由此类绘制。窗体可重绘的部分包括窗体边框,border(边框)、caption(标题栏)、Frame(提示框中间部分和提示框左下右边之间部分)但不包括中间内容部分。重绘需要用GraphicsCache对选定区域进行重绘(Rectangle),如标题文字框(TextBounds)、标题栏框(CaptionBounds)等等。详细见下:

internal class MessageBoxFormPainter : FormPainter
{
internal MessageBoxFormPainter(Control owner, ISkinProvider provider) : base(owner, provider) { }
///

/// 标题文字样式 ///
///
protected override void DrawText(GraphicsCache cache)
{
string text = Text;
if (text == null || text.Length == 0 || TextBounds.IsEmpty)
return;
AppearanceObject appearance = new AppearanceObject(GetDefaultAppearance());
appearance.Font = Owner.Font;
appearance.TextOptions.Trimming = Trimming.EllipsisCharacter;

        Rectangle r = RectangleHelper.GetCenterBounds(TextBounds, new Size(TextBounds.Width, appearance.CalcDefaultTextSize(cache.Graphics).Height));  
        DrawTextShadow(cache, appearance, r);  


//cache.DrawString(text, appearance.Font, appearance.GetForeBrush(cache),r, appearance.GetStringFormat());
cache.DrawString(text, appearance.Font, Color.Blue, r, appearance.GetStringFormat()); // 字体颜色、样式
}
///

/// 标题阴影 ///
///
///
///
protected override void DrawTextShadow(GraphicsCache cache, AppearanceObject appearance, Rectangle bounds)
{
base.DrawTextShadow(cache, appearance, bounds);
}
/// /// 标题文本框高度 ///
///
///
///
protected override int CalcTextHeight(GraphicsCache cache, AppearanceObject appearance)
{
return (int)(cache.Graphics.MeasureString(Text, Owner.Font).Height);
}
/// /// 标题栏高度 ///
///
protected override int GetCaptionHeight()
{
return 30;
}
/// /// 画标题 ///
///
///
protected override void DrawCaption(GraphicsCache cache, bool doubleBuffer)
{
base.DrawCaption(cache, doubleBuffer);
}
/// /// 画背景 ///
///
protected override void DrawBackground(GraphicsCache cache)
{
base.DrawBackground(cache);

Rectangle r = RectangleHelper.GetCenterBounds(this.CaptionBounds, new Size(CaptionBounds.Width, CaptionBounds.Height));
Brush linGrBrush = new LinearGradientBrush(r,
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255), 45);
cache.FillRectangle(linGrBrush, r);

}
/// /// 画边框 ///
///
///
protected override void DrawBorders(GraphicsCache cache, bool doubleBuffer)
{
base.DrawBorders(cache, doubleBuffer);
//Rectangle r = RectangleHelper.GetCenterBounds(this.CaptionBounds, new Size(CaptionBounds.Width, CaptionBounds.Height));
//Pen myPen = new Pen(Color.Black, 1);
//cache.DrawRectangle(myPen, r);
}
/// /// 画展示内容周边(左、右、下部分) ///
///
///
/// 表示周边为左、右、下的哪一个
protected override void DrawFrameCore(GraphicsCache cache, SkinElementInfo info, FrameKind kind)
{
base.DrawFrameCore(cache, info, kind);

Brush linGrBrush = new LinearGradientBrush(info.Bounds,
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255), 45);
//Color.FromArgb(255, 119, 144, 237),
//Color.FromArgb(255, 180, 154, 202), 45);
cache.FillRectangle(linGrBrush, info.Bounds);

}
/// /// 画标题文字前图标 ///
///
public override void DrawIcon(GraphicsCache cache)
{
base.DrawIcon(cache);
//cache.DrawIcon(SystemIcons.Warning,IconBounds);
}
/// /// 画标题栏按钮(最大化、最小化、关闭) ///
///
///
public override void DrawButtons(GraphicsCache cache, bool useClip)
{
//base.DrawButtons(cache, useClip);
this.Buttons.Clear();
}
}

最后新建一个类NewXtraMessageBox,写一个用于弹出窗体的方法,返回值为DialogResult。

private static DialogResult ShowInternal(Control owner, string message, string caption, Icon messageIcon, params DialogResult[] dialogResults)
{
MessageBoxForm form = new MessageBoxForm();
XtraMessageBoxArgs args = new XtraMessageBoxArgs(owner, message, caption, dialogResults, messageIcon, 0);
args.Showing += Args_Showing;
return form.ShowMessageBoxDialog(args);
}

XtraMessageBoxArgs便是提示框展示的内容部分,注册Args_Showing事件,在事件重绘窗体。这里也能重绘窗体,但能重绘的部分只包括在正常设计窗体时的属性部分(字体大小、边框样式、透明度等),提示框中间部分样式只能在此处修改。

///

/// 此处修改form和button ///
///
///
private static void Args_Showing(object sender, XtraMessageShowingArgs e)
{
//改变form相关样式
e.Form.Font = new Font("Arial", 14F);
e.Form.LookAndFeel.UseDefaultLookAndFeel = false;
e.Form.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Skin;
//e.Form.FormBorderStyle = FormBorderStyle.None;

MessageButtonCollection buttons = e.Buttons;
SimpleButton btn = null;
foreach (var dialog in (DialogResult[])Enum.GetValues(typeof(DialogResult)))
{
btn = buttons[dialog];

if (btn != null)
{
btn.Size = new Size(Convert.ToInt32(btn.Width * 1.2), Convert.ToInt32(btn.Height * 1.2)); //按钮大小
btn.Font = e.Form.Font; //按钮字体
btn.ShowFocusRectangle = DefaultBoolean.False;
}
if (dialog == DialogResult.OK) //通过DialogResult确定按钮并改变具体按钮样式
{
btn.Text = "‘是’按钮";
}
}

}
}

最后在此类中写Show方法,输入标题、内容、按钮类型、图标等信息调用ShowInternal方法,可自定义图标。

public static void Show(string message)
{
ShowMessageBox(message, null, MessageBoxButtons.OK, null);
}
public static void Show(Control owner, string message)
{
ShowInternal(owner, message, null, null, DialogResult.OK);
}
public static void Show(string message, string caption)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, null);
}
public static void Show(string message, string caption, MessageBoxButtons messageBox)
{
ShowMessageBox(message, caption, messageBox, null);
}
public static void Show(string message, string caption, MessageBoxIcon messageBoxIcon)
{
if (messageBoxIcon == MessageBoxIcon.Warning)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Warning);
}
else if (messageBoxIcon == MessageBoxIcon.Question)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Question);
}
else if (messageBoxIcon == MessageBoxIcon.Error)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Error);
}
else if (messageBoxIcon == MessageBoxIcon.Information)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Information);
}
else if (messageBoxIcon == MessageBoxIcon.Asterisk)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Asterisk);
}
else if (messageBoxIcon == MessageBoxIcon.Exclamation)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Exclamation);
}
else if (messageBoxIcon == MessageBoxIcon.Hand)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Hand);
}
else
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, null);
}
}
public static void Show(string message, string caption, Icon icon)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, icon);
}
public static void Show(string message, string caption, MessageBoxButtons messageBox, MessageBoxIcon messageBoxIcon)
{
if (messageBoxIcon == MessageBoxIcon.Warning)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Warning);
}
else if (messageBoxIcon == MessageBoxIcon.Question)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Question);
}
else if (messageBoxIcon == MessageBoxIcon.Error)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Error);
}
else if (messageBoxIcon == MessageBoxIcon.Information)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Information);
}
else if (messageBoxIcon == MessageBoxIcon.Asterisk)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Asterisk);
}
else if (messageBoxIcon == MessageBoxIcon.Exclamation)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Exclamation);
}
else if (messageBoxIcon == MessageBoxIcon.Hand)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Hand);
}
else
{
ShowMessageBox(message, caption, messageBox, null);
}
}
public static void Show(string message, string caption, MessageBoxButtons messageBox, Icon icon)
{
ShowMessageBox(message, caption, messageBox, icon);
}
private static void ShowMessageBox(string message, string caption, MessageBoxButtons messageBox, Icon icon)
{
if (messageBox == MessageBoxButtons.OK)
{
ShowInternal(null, message, caption, icon, DialogResult.OK);
}
else if (messageBox == MessageBoxButtons.OKCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.OK, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.YesNo)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Yes, DialogResult.No });
}
else if (messageBox == MessageBoxButtons.YesNoCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Yes, DialogResult.No, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.RetryCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Retry, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.AbortRetryIgnore)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Abort, DialogResult.Retry, DialogResult.Ignore });
}
}

效果如下:

通过重绘可以还可将背景改为图片、改变按钮位置等等、可进一步拓展。

完整项目可以从我的csdn下载:https://download.csdn.net/download/weixin_52748738/18149812

附完整代码:

using DevExpress.Skins;
using DevExpress.Skins.XtraForm;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace TestMessageBox
{
public static class NewMessageBox
{
#region show部分
public static void Show(string message)
{
ShowMessageBox(message, null, MessageBoxButtons.OK, null);
}
public static void Show(Control owner, string message)
{
ShowInternal(owner, message, null, null, DialogResult.OK);
}
public static void Show(string message, string caption)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, null);
}
public static void Show(string message, string caption, MessageBoxButtons messageBox)
{
ShowMessageBox(message, caption, messageBox, null);
}
public static void Show(string message, string caption, MessageBoxIcon messageBoxIcon)
{
if (messageBoxIcon == MessageBoxIcon.Warning)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Warning);
}
else if (messageBoxIcon == MessageBoxIcon.Question)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Question);
}
else if (messageBoxIcon == MessageBoxIcon.Error)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Error);
}
else if (messageBoxIcon == MessageBoxIcon.Information)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Information);
}
else if (messageBoxIcon == MessageBoxIcon.Asterisk)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Asterisk);
}
else if (messageBoxIcon == MessageBoxIcon.Exclamation)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Exclamation);
}
else if (messageBoxIcon == MessageBoxIcon.Hand)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, SystemIcons.Hand);
}
else
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, null);
}
}
public static void Show(string message, string caption, Icon icon)
{
ShowMessageBox(message, caption, MessageBoxButtons.OK, icon);
}
public static void Show(string message, string caption, MessageBoxButtons messageBox, MessageBoxIcon messageBoxIcon)
{
if (messageBoxIcon == MessageBoxIcon.Warning)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Warning);
}
else if (messageBoxIcon == MessageBoxIcon.Question)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Question);
}
else if (messageBoxIcon == MessageBoxIcon.Error)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Error);
}
else if (messageBoxIcon == MessageBoxIcon.Information)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Information);
}
else if (messageBoxIcon == MessageBoxIcon.Asterisk)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Asterisk);
}
else if (messageBoxIcon == MessageBoxIcon.Exclamation)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Exclamation);
}
else if (messageBoxIcon == MessageBoxIcon.Hand)
{
ShowMessageBox(message, caption, messageBox, SystemIcons.Hand);
}
else
{
ShowMessageBox(message, caption, messageBox, null);
}
}
public static void Show(string message, string caption, MessageBoxButtons messageBox, Icon icon)
{
ShowMessageBox(message, caption, messageBox, icon);
}
private static void ShowMessageBox(string message, string caption, MessageBoxButtons messageBox, Icon icon)
{
if (messageBox == MessageBoxButtons.OK)
{
ShowInternal(null, message, caption, icon, DialogResult.OK);
}
else if (messageBox == MessageBoxButtons.OKCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.OK, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.YesNo)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Yes, DialogResult.No });
}
else if (messageBox == MessageBoxButtons.YesNoCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Yes, DialogResult.No, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.RetryCancel)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Retry, DialogResult.Cancel });
}
else if (messageBox == MessageBoxButtons.AbortRetryIgnore)
{
ShowInternal(null, message, caption, icon, new DialogResult[] { DialogResult.Abort, DialogResult.Retry, DialogResult.Ignore });
}
}
#endregion
private static DialogResult ShowInternal(Control owner, string message, string caption, Icon messageIcon, params DialogResult[] dialogResults)
{
MessageBoxForm form = new MessageBoxForm();
XtraMessageBoxArgs args = new XtraMessageBoxArgs(owner, message, caption, dialogResults, messageIcon, 0);
args.Showing += Args_Showing;
return form.ShowMessageBoxDialog(args);
}

///

/// 此处修改form和button ///
///
///
private static void Args_Showing(object sender, XtraMessageShowingArgs e)
{
e.Form.Font = new Font("Arial", 14F);
e.Form.LookAndFeel.UseDefaultLookAndFeel = false;
e.Form.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Skin;

    //e.Form.FormBorderStyle = FormBorderStyle.None;  


MessageButtonCollection buttons = e.Buttons;
SimpleButton btn = null;
foreach (var dialog in (DialogResult[])Enum.GetValues(typeof(DialogResult)))
{
btn = buttons[dialog];

if (btn != null)
{
btn.Size = new Size(Convert.ToInt32(btn.Width * 1.2), Convert.ToInt32(btn.Height * 1.2)); //按钮大小
btn.Font = e.Form.Font; //按钮字体
btn.ShowFocusRectangle = DefaultBoolean.False;
}
if (dialog == DialogResult.OK)
{
btn.Text = "‘是’按钮";
}
}

}
}

internal class MessageBoxForm : XtraMessageBoxForm
{
protected override FormPainter CreateFormBorderPainter()
{
return new MessageBoxFormPainter(this, LookAndFeel);
}

}
internal class MessageBoxFormPainter : FormPainter
{
internal MessageBoxFormPainter(Control owner, ISkinProvider provider) : base(owner, provider) { }

///

/// 标题文字样式 ///
///
protected override void DrawText(GraphicsCache cache)
{
string text = Text;
if (text == null || text.Length == 0 || TextBounds.IsEmpty)
return;
AppearanceObject appearance = new AppearanceObject(GetDefaultAppearance());
appearance.Font = Owner.Font;
appearance.TextOptions.Trimming = Trimming.EllipsisCharacter;

    Rectangle r = RectangleHelper.GetCenterBounds(TextBounds, new Size(TextBounds.Width, appearance.CalcDefaultTextSize(cache.Graphics).Height));  
    DrawTextShadow(cache, appearance, r);  


//cache.DrawString(text, appearance.Font, appearance.GetForeBrush(cache),r, appearance.GetStringFormat());
cache.DrawString(text, appearance.Font, Color.Blue, r, appearance.GetStringFormat()); // 字体颜色、样式
}
///

/// 标题阴影 ///
///
///
///
protected override void DrawTextShadow(GraphicsCache cache, AppearanceObject appearance, Rectangle bounds)
{
base.DrawTextShadow(cache, appearance, bounds);
}
/// /// 标题文本框高度 ///
///
///
///
protected override int CalcTextHeight(GraphicsCache cache, AppearanceObject appearance)
{
return (int)(cache.Graphics.MeasureString(Text, Owner.Font).Height);
}
/// /// 标题栏高度 ///
///
protected override int GetCaptionHeight()
{
return 30;
}
/// /// 画标题 ///
///
///
protected override void DrawCaption(GraphicsCache cache, bool doubleBuffer)
{
base.DrawCaption(cache, doubleBuffer);
}
/// /// 画背景 ///
///
protected override void DrawBackground(GraphicsCache cache)
{
base.DrawBackground(cache);

Rectangle r = RectangleHelper.GetCenterBounds(this.CaptionBounds, new Size(CaptionBounds.Width, CaptionBounds.Height));
Brush linGrBrush = new LinearGradientBrush(r,
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255), 45);
cache.FillRectangle(linGrBrush, r);

}
/// /// 画边框 ///
///
///
protected override void DrawBorders(GraphicsCache cache, bool doubleBuffer)
{
base.DrawBorders(cache, doubleBuffer);
//Rectangle r = RectangleHelper.GetCenterBounds(this.CaptionBounds, new Size(CaptionBounds.Width, CaptionBounds.Height));
//Pen myPen = new Pen(Color.Black, 1);
//cache.DrawRectangle(myPen, r);
}
/// /// 画展示内容周边(左、右、下部分) ///
///
///
/// 表示周边为左、右、下的哪一个
protected override void DrawFrameCore(GraphicsCache cache, SkinElementInfo info, FrameKind kind)
{
base.DrawFrameCore(cache, info, kind);

Brush linGrBrush = new LinearGradientBrush(info.Bounds,
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255), 45);
//Color.FromArgb(255, 119, 144, 237),
//Color.FromArgb(255, 180, 154, 202), 45);
cache.FillRectangle(linGrBrush, info.Bounds);

}
/// /// 画标题文字前图标 ///
///
public override void DrawIcon(GraphicsCache cache)
{
base.DrawIcon(cache);
//cache.DrawIcon(SystemIcons.Warning,IconBounds);
}
/// /// 画标题栏按钮(最大化、最小化、关闭) ///
///
///
public override void DrawButtons(GraphicsCache cache, bool useClip)
{
//base.DrawButtons(cache, useClip);
this.Buttons.Clear();
}
}
}