欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)畫(huà)。

創(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)畫(huà)

添加一個(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();
    }
  }

處理多種不同類(lèi)型的警告框

添加AlertType枚舉,讓警告框顯示不同類(lèi)型的消息,根據(jù)消息類(lèi)型變換不同的消息主題顏色,并未Show方法添加警告框類(lèi)型參數(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è)打開(kāi)的消息窗口的位置,超過(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)的懸停和離開(kāi)事件進(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è)打開(kāi)的消息窗口的位置,超過(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)
    {
      // 注銷(xiāo)鼠標(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)文章

  • C#任務(wù)并行Parellel.For和Parallel.ForEach

    C#任務(wù)并行Parellel.For和Parallel.ForEach

    這篇文章介紹了C#任務(wù)并行Parellel.For和Parallel.ForEach的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • unity實(shí)現(xiàn)文字滾動(dòng)效果

    unity實(shí)現(xiàn)文字滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)文字滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片

    Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片

    這篇文章主要介紹了Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片,在window系統(tǒng)中,自帶的鼠標(biāo)外觀可能看起來(lái)比較小,因此我們需要使用自己的鼠標(biāo)圖片外觀
    2020-11-11
  • C# 對(duì)PDF文檔加密、解密(基于Spire.Cloud.SDK for .NET)

    C# 對(duì)PDF文檔加密、解密(基于Spire.Cloud.SDK for .NET)

    這篇文章主要介紹了C# 基于Spire.Cloud.SDK for .NET對(duì)PDF文檔進(jìn)行加密解密,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Quartz.Net使用方法詳解

    Quartz.Net使用方法詳解

    本文詳細(xì)講解了Quartz.Net的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成

    asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成

    web程序的高訪問(wèn)量、大數(shù)據(jù)量、高效的用戶體驗(yàn)度,使靜態(tài)頁(yè)技術(shù)在越來(lái)越多的網(wǎng)站上發(fā)揮作用。這篇文章主要介紹asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成,有需要的朋友可以參考下
    2015-08-08
  • 詳解C#泛型的類(lèi)型參數(shù)約束

    詳解C#泛型的類(lèi)型參數(shù)約束

    這篇文章主要介紹了C#泛型的類(lèi)型參數(shù)約束的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-07-07
  • C# 中的??操作符淺談

    C# 中的??操作符淺談

    (??) 用于如果類(lèi)不為空值時(shí)返回它自身,如果為空值則返回之后的操作
    2013-04-04
  • C#縮略圖多路徑多格式保存的實(shí)例

    C#縮略圖多路徑多格式保存的實(shí)例

    這篇文章介紹了C#縮略圖多路徑多格式保存的實(shí)例,有需要的朋友可以參考一下
    2013-07-07
  • C#快速配置NLog日志的教程詳解

    C#快速配置NLog日志的教程詳解

    這篇文章主要為大家詳細(xì)介紹了C#快速配置NLog日志的教程相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下
    2024-02-02

最新評(píng)論