最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WindowsForm實(shí)現(xiàn)警告消息框的實(shí)例代碼

 更新時(shí)間:2020年07月18日 09:23:47   作者:zhuanghamiao  
這篇文章主要介紹了WindowsForm如何實(shí)現(xiàn)警告消息框,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

警告消息框主要是用來(lái)向用戶戶展示諸如警告、異常、完成和提示消息。一般實(shí)現(xiàn)的效果就是從系統(tǒng)窗口右下角彈出,然后加上些簡(jiǎn)單的顯示和消失的動(dòng)畫。

創(chuàng)建警告框窗口

首先我們創(chuàng)建一個(gè)警告框窗口(Form),將窗口設(shè)置為無(wú)邊框(FormBoderStyle=None),添加上圖片和內(nèi)容顯示控件

創(chuàng)建好警告框后,我們先讓他能夠從窗口右下角顯示出來(lái),

  public partial class AlertMessageForm : Form
  {
    public AlertMessageForm()
    {
      InitializeComponent();
    }

    private int x, y;

    public void Show(string message)
    {
      this.StartPosition = FormStartPosition.Manual;
      this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
      this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
      this.Location = new Point(x, y);
      labelContent.Text = message;
      this.Show();
    }
  }

警告框顯示和關(guān)閉動(dòng)畫

添加一個(gè)計(jì)時(shí)器,通過(guò)時(shí)鐘控制窗口背景漸入和淡出

  // 警告框的行為(顯示,停留,退出)
  public enum AlertFormAction
  {
    Start,
    Wait,
    Close
  }

  public partial class AlertMessageForm : Form
  {
    public AlertMessageForm()
    {
      InitializeComponent();
    }

    private int x, y;
    private AlertFormAction action;

    private void timer1_Tick(object sender, EventArgs e)
    {
      switch (action)
      {
        case AlertFormAction.Start:
          timer1.Interval = 50;//警告顯示的時(shí)間
          this.Opacity += 0.1;
          if (this.Opacity == 1.0)
          {
            action = AlertFormAction.Wait;
          }
          break;
        case AlertFormAction.Wait:
          timer1.Interval = 3000;//警告框停留時(shí)間
          action = AlertFormAction.Close;
          break;
        case AlertFormAction.Close:
          timer1.Interval = 50;//警告退出的時(shí)間
          this.Opacity -= 0.1;
          if (this.Opacity == 0.0)
          {
            this.Close();
          }
          break;
        default:
          break;
      }
    }

    public void Show(string message)
    {
      //設(shè)置窗口啟始位置
      this.StartPosition = FormStartPosition.Manual;
      this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
      this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
      this.Location = new Point(x, y);

      labelContent.Text = message;
      this.Opacity = 0.0;

      this.Show();

      action = AlertFormAction.Start;
      //啟動(dòng)時(shí)鐘
      timer1.Start();
    }
  }

處理多種不同類型的警告框

添加AlertType枚舉,讓警告框顯示不同類型的消息,根據(jù)消息類型變換不同的消息主題顏色,并未Show方法添加警告框類型參數(shù)

  public enum AlertType
  {
    Info,
    Success,
    Warning,
    Error
  }

 // 設(shè)置警告框主題
 private void SetAlertTheme(AlertType type)
 {
   switch (type)
   {
     case AlertType.Info:
       this.pictureBox1.Image = Properties.Resources.info;
       this.BackColor = Color.RoyalBlue;
       break;
     case AlertType.Success:
       this.pictureBox1.Image = Properties.Resources.success;
       this.BackColor = Color.SeaGreen;
       break;
     case AlertType.Warning:
       this.pictureBox1.Image = Properties.Resources.warning;
       this.BackColor = Color.DarkOrange;
       break;
     case AlertType.Error:
       this.pictureBox1.Image = Properties.Resources.error;
       this.BackColor = Color.DarkRed;
       break;
     default:
       break;
   }
 }

 // 顯示警告框
 public void Show(string message, AlertType type){
  // ...
  SetAlertTheme(type);
 }

處理多個(gè)警告框重疊問(wèn)題

當(dāng)然,完成上面的處理是不夠的,當(dāng)有多個(gè)消息的時(shí)候,消息框會(huì)重疊在一起;多個(gè)消息時(shí),需要將消息窗口按一定的規(guī)則排列,這里我們?cè)O(shè)置每個(gè)消息窗口間隔一定的距離

    public void Show(string message, AlertType type)
    {
      // 設(shè)置窗口啟始位置
      this.StartPosition = FormStartPosition.Manual;

      // 設(shè)置程序每個(gè)打開的消息窗口的位置,超過(guò)10個(gè)就不做處理,這個(gè)可以根據(jù)自己的需求設(shè)定
      string fname;
      for (int i = 1; i < 10; i++)
      {
        fname = "alert" + i.ToString();
        AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
        if (alert == null)
        {
          this.Name = fname;
          this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
          this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
          this.Location = new Point(x, y);
          break;
        }
      }

      labelContent.Text = message;
      this.Opacity = 0.0;
      SetAlertTheme(type);
      this.Show();

      action = AlertFormAction.Start;
      //啟動(dòng)時(shí)鐘
      timer1.Start();
    }

鼠標(biāo)懸停警告框處理

想要警告框停留的時(shí)間長(zhǎng)一些,一中方式是直接設(shè)置警告框停留的時(shí)間長(zhǎng)一些,另一種方式是通過(guò)判斷鼠標(biāo)在警告框窗口是否懸停,所以可以通過(guò)鼠標(biāo)的懸停和離開事件進(jìn)行處理

    private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
    {
      this.Opacity = 1.0;
      timer1.Interval = int.MaxValue;//警告框停留時(shí)間
      action = AlertFormAction.Close;
    }

    private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
    {
      this.Opacity = 1.0;
      timer1.Interval = 3000;//警告框停留時(shí)間
      action = AlertFormAction.Close;
    }

警告框的完整代碼

  public enum AlertType
  {
    Info,
    Success,
    Warning,
    Error
  }

  public enum AlertFormAction
  {
    Start,
    Wait,
    Close
  }

  public partial class AlertMessageForm : Form
  {
    public AlertMessageForm()
    {
      InitializeComponent();
    }

    private int x, y;
    private AlertFormAction action;

    private void timer1_Tick(object sender, EventArgs e)
    {
      switch (action)
      {
        case AlertFormAction.Start:
          timer1.Interval = 50;//警告顯示的時(shí)間
          this.Opacity += 0.1;
          if (this.Opacity == 1.0)
          {
            action = AlertFormAction.Wait;
          }
          break;
        case AlertFormAction.Wait:
          timer1.Interval = 3000;//警告框停留時(shí)間
          action = AlertFormAction.Close;
          break;
        case AlertFormAction.Close:
          timer1.Interval = 50;//警告關(guān)閉的時(shí)間
          this.Opacity -= 0.1;
          if (this.Opacity == 0.0)
          {
            this.Close();
          }
          break;
        default:
          break;
      }
    }

    public void Show(string message, AlertType type)
    {
      // 設(shè)置窗口啟始位置
      this.StartPosition = FormStartPosition.Manual;

      // 設(shè)置程序每個(gè)打開的消息窗口的位置,超過(guò)10個(gè)就不做處理,這個(gè)可以根據(jù)自己的需求設(shè)定
      string fname;
      for (int i = 1; i < 10; i++)
      {
        fname = "alert" + i.ToString();
        AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
        if (alert == null)
        {
          this.Name = fname;
          this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
          this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
          this.Location = new Point(x, y);
          break;
        }
      }

      labelContent.Text = message;
      this.Opacity = 0.0;
      SetAlertTheme(type);
      this.Show();

      action = AlertFormAction.Start;
      //啟動(dòng)時(shí)鐘
      timer1.Start();
    }

    private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
    {
      this.Opacity = 1.0;
      timer1.Interval = int.MaxValue;//警告框停留時(shí)間
      action = AlertFormAction.Close;
    }

    private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
    {
      this.Opacity = 1.0;
      timer1.Interval = 3000;//警告框停留時(shí)間
      action = AlertFormAction.Close;
    }

    private void buttonClose_Click(object sender, EventArgs e)
    {
      // 注銷鼠標(biāo)事件
      this.MouseLeave-= new System.EventHandler(this.AlertMessageForm_MouseLeave);
      this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.AlertMessageForm_MouseMove);

      timer1.Interval = 50;//警告關(guān)閉的時(shí)間
      this.Opacity -= 0.1;
      if (this.Opacity == 0.0)
      {
        this.Close();
      }
    }

    // 設(shè)置警告框主題
    private void SetAlertTheme(AlertType type)
    {
      switch (type)
      {
        case AlertType.Info:
          this.pictureBox1.Image = Properties.Resources.info;
          this.BackColor = Color.RoyalBlue;
          break;
        case AlertType.Success:
          this.pictureBox1.Image = Properties.Resources.success;
          this.BackColor = Color.SeaGreen;
          break;
        case AlertType.Warning:
          this.pictureBox1.Image = Properties.Resources.warning;
          this.BackColor = Color.DarkOrange;
          break;
        case AlertType.Error:
          this.pictureBox1.Image = Properties.Resources.error;
          this.BackColor = Color.DarkRed;
          break;
        default:
          break;
      }
    }
  }

以上就是WindowsForm實(shí)現(xiàn)警告消息框的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于WindowsForm實(shí)現(xiàn)警告消息框的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 如何搭建新的WPF項(xiàng)目框架

    如何搭建新的WPF項(xiàng)目框架

    這篇文章主要介紹了如何搭建新的WPF項(xiàng)目框架,在項(xiàng)目開發(fā)中比較常見(jiàn)的開發(fā)模式就是MVVM模式,使用MVVM框架開發(fā)好處:1、框架較輕,2、學(xué)習(xí)成本低、3、適用大多數(shù)中小型項(xiàng)目,4、相對(duì)于微軟的prism框架更容易上手,需要的朋友可以參考下
    2015-07-07
  • C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件

    C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-03-03
  • C#在Excel表格中插入、編輯和刪除批注

    C#在Excel表格中插入、編輯和刪除批注

    這篇文章主要為大家詳細(xì)介紹了C#如何在Excel表格中插入、編輯和刪除批注,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • DevExpress實(shí)現(xiàn)TreeList按條件隱藏節(jié)點(diǎn)CheckBox的方法

    DevExpress實(shí)現(xiàn)TreeList按條件隱藏節(jié)點(diǎn)CheckBox的方法

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList按條件隱藏節(jié)點(diǎn)CheckBox的方法,需要的朋友可以參考下
    2014-08-08
  • 利用FlubuCore用C#來(lái)寫DevOps腳本的方法詳解

    利用FlubuCore用C#來(lái)寫DevOps腳本的方法詳解

    這篇文章主要介紹了利用FlubuCore用C#來(lái)寫DevOps腳本的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • C#中的正則表達(dá)式介紹

    C#中的正則表達(dá)式介紹

    關(guān)于正則表達(dá)式,我們都知道挺繁瑣的。本文介紹的是C#中的正則表達(dá)式,希望對(duì)你有幫助,一起來(lái)看。
    2015-10-10
  • 淺析C# 9.0 新特性之 Lambda 棄元參數(shù)

    淺析C# 9.0 新特性之 Lambda 棄元參數(shù)

    這篇文章主要介紹了C# 9.0 新特性之 Lambda 棄元參數(shù)的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),想學(xué)習(xí)c#的朋友可以了解下
    2020-06-06
  • C#使用Json.Net對(duì)JSON與對(duì)象的序列化與反序列化

    C#使用Json.Net對(duì)JSON與對(duì)象的序列化與反序列化

    這篇文章介紹了Json.Net對(duì)JSON與對(duì)象的序列化與反序列化,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 完成OSS.Http底層HttpClient重構(gòu)封裝 支持標(biāo)準(zhǔn)庫(kù)

    完成OSS.Http底層HttpClient重構(gòu)封裝 支持標(biāo)準(zhǔn)庫(kù)

    OSS.Http項(xiàng)目對(duì)于.Net Standard標(biāo)準(zhǔn)庫(kù)的支持已經(jīng)遷移完畢,OSS開源系列兩個(gè)最底層的類庫(kù)已經(jīng)具備跨運(yùn)行時(shí)支持的能力。本篇文章主要包含 1. HttpClient的介紹,2. 重構(gòu)的思路, 3. 容易遇到的問(wèn)題。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • C#面向?qū)ο缶幊讨虚_閉原則的示例詳解

    C#面向?qū)ο缶幊讨虚_閉原則的示例詳解

    在面向?qū)ο缶幊讨?,SOLID?是五個(gè)設(shè)計(jì)原則的首字母縮寫,旨在使軟件設(shè)計(jì)更易于理解、靈活和可維護(hù)。本文將通過(guò)實(shí)例詳細(xì)講講C#面向?qū)ο缶幊讨虚_閉原則,需要的可以參考一下
    2022-07-07

最新評(píng)論

临高县| 马关县| 平昌县| 陵水| 尖扎县| 色达县| 闻喜县| 桐乡市| 崇文区| 徐汇区| 南康市| 政和县| 阳谷县| 九江市| 绩溪县| 湖口县| 喀喇| 本溪市| 利川市| 甘南县| 凌海市| 日喀则市| 阜南县| 新巴尔虎左旗| 双峰县| 安阳市| 双鸭山市| 台州市| 法库县| 合阳县| 大竹县| 山东省| 普兰县| 邹城市| 平远县| 密山市| 眉山市| 怀宁县| 海南省| 长子县| 启东市|