WPF下自定义MessageBox消息提示

使用系统MessageBox与自己项目风格存在明显差异,定义自己风格的MessageBox

源程序地址https://download.csdn.net/download/hongbo1515/10609410,需要的朋友可自行下载,调整为所需要的风格

首先看一下确认窗口,

确认窗口

再看提示窗口

提示窗口

使用与系统MessageBox类似,下面说一下代码

1.确认窗口代码,MessageBoxOKCancel.xaml.cs

 public partial class MessageBoxOKCancel : Window
    {
        public MessageBoxOKCancel()
        {
            InitializeComponent();
        }
        public MessageBoxOKCancel(string message)
        {
            InitializeComponent();
            this.message.Text = message;
        }

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = true;
            }
            catch (Exception ex) { }
            this.Close();
        }
        private void Cancle_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = false;
            }
            catch (Exception ex) { }
            this.Close();
        }
    }

  

2.提示窗口代码, MessageBoxOK.xaml.cs

public partial class MessageBoxOK : Window
    {
        public MessageBoxOK()
        {
            InitializeComponent();
        }
        public MessageBoxOK(string mess)
        {
            InitializeComponent();
            message.Text = mess;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = false;
            }
            catch (Exception ex) { }

            this.Close();
        }
    }

  

3.测试代码

private void Button_Click(object sender, RoutedEventArgs e)
        {
            //测试
            string display = this.textBox.Text;
            if (MyMessageBox.ShowDialog(display, MyMessageBox.OKCANCLE).Value == true)
            {
                MyMessageBox.Show("确认");
            }
            else
            {
                MyMessageBox.Show("取消");
            }
        }

  

使用确认窗,MyMessageBox.ShowDialog(display, MyMessageBox.OKCANCLE);

使用提示窗,MyMessageBox.Show("确认");

源程序下载地址 https://download.csdn.net/download/hongbo1515/10609410,需要的朋友可自行下载,调整为所需要的风格

猜你喜欢

转载自www.cnblogs.com/hubert17/p/9491472.html