1.使用XtraMessageBoxForm,自定義Icon
2.重寫XtraMessageBoxForm,自定義消息字體,標題字體
3.注冊XtraMessageBoxForm的Showing事件,自定義按鈕字體及按鈕大小
具體代碼如下,只寫了簡單兩種方法,可自己擴展,賦值MessageBoxIcon可以顯示想要的Icon
public static class UIMessageBox { static UIMessageBox() { MessageBoxForm.MessageBoxFont = new Font("Arial", 14F); //定義字體類型 } static readonly Icon MessageBoxIcon = null; public static void Show(string message) { ShowInternal(null, message, "Notice", SystemIcons.Information, DialogResult.OK); } public static void Show(Control owner, string message) { ShowInternal(owner, message, "Notice", SystemIcons.Information, DialogResult.OK); }
private static DialogResult ShowInternal(Control owner, string message, string caption, Icon messageIcon, params DialogResult[] dialogResults) { MessageBoxForm form = new MessageBoxForm(); form.Icon = MessageBoxIcon; XtraMessageBoxArgs args = new XtraMessageBoxArgs(owner, message, caption, dialogResults, messageIcon, 0); args.Showing += Args_Showing; return form.ShowMessageBoxDialog(args); } private static void Args_Showing(object sender, XtraMessageShowingArgs e) { MessageButtonCollection buttons = e.Buttons as MessageButtonCollection; SimpleButton btn = null; foreach (var dialog in (DialogResult[])Enum.GetValues(typeof(DialogResult))) { btn = buttons[dialog] as SimpleButton; if (btn != null) { btn.Size = new Size(Convert.ToInt32(btn.Width * 1.2), Convert.ToInt32(btn.Height * 1.2)); //按鈕大小 btn.Font = e.Form.Font; //按鈕字體 } } } } internal class MessageBoxForm : XtraMessageBoxForm { internal static Font MessageBoxFont = new Font("Arial", 10F); public MessageBoxForm() { Appearance.Font = MessageBoxFont; } 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()); } protected override int CalcTextHeight(Graphics graphics, AppearanceObject appearance) { return (int)(graphics.MeasureString(Text, Owner.Font).Height); //標題欄的高度 } }
調用時:
UIMessageBox.Show("This is a message");