1. 程式人生 > 實用技巧 >對話方塊訊息在c#中為。net Framework 4.5

對話方塊訊息在c#中為。net Framework 4.5

表的內容 在我們繼續實現表單設計程式碼之前介紹 結構事件和方法 結論歷史 介紹 在我開始程式設計的時候,我在標準的。net框架庫中發現了MessageBox類。這是令人興奮的,因為它允許我試驗不同的方式來提供我自己的資訊,關於我的應用程式正在發生什麼。這很好,直到我在個人使用Windows時看到了一個特殊的訊息框。我最終了解到這個訊息框實際上叫做任務對話方塊,它是在Windows Vista中引入的。它的頂部有藍色的文字,底部有一些較小的黑色文字。瞭解如何使用TaskDialog超出了我的技能範圍,所以我決定嘗試使用我當時擁有的技能建立一個類似的。 在這個專案中,我們將使用Visual Studio和使用c#的設計器編寫我們自己的對話方塊訊息框。它將支援兩種文字樣式、三種按鈕配置和六種圖示配置。圖示的使用來自SystemIcons類。 在我們繼續之前 這並不是關於如何使用Visual Studio或c#程式語言的一步一步的教程,而是開發自己的訊息框所需邏輯的概述。程式碼部分中的程式碼片段有許多註釋,以幫助理解每個片段的作用。以下是假設您熟悉的主題列表: Windows窗體設計if-else開關列舉型別方法與返回值傳遞值型別引數靜態類和靜態類成員動態連結庫(DLL) 實現 下面是訊息的實現。這段程式碼將顯示出現在本文開頭影象中的訊息。 隱藏,收縮,複製Code

using DialogMessage;

if (DMessage.ShowMessage(

  // Window Title
  "Window Title",

  // Main Instruction
  "Want to learn how to write your own message box?",

  // Dialog buttons
  DMessage.MsgButtons.YesNo,

  // Dialog Icons
  DMessage.MsgIcons.Question,

  // Content
  "In this project we will learn the logic necessary "
+ "to write your own dialog message box in Windows") // Checks DialogResult of the button clicked by user == DialogResult.Yes) // Show the Windows standard MessageBox to test result MessageBox.Show("You clicked Yes!"); else MessageBox.Show("You clicked No!");

表單設計 下圖是MainForm.Designer.cs的表單設計器檢視,帶有控制元件和一些值得注意的屬性。需要注意的重要屬性有Anchor、MaximumSize和FormBorderStyle。 錨確保窗體上的控制元件在窗體重新調整大小時適當移動。 標籤的最大尺寸確保文字不會溢位窗體,並將換行到新行。 FormBorderStyle設定為FixedDialog可確保使用者不能根據提供的文字量調整表單的大小。 程式碼 結構 訊息框被分成兩個主檔案;MainForm.cs DialogMessage.cs。 cs包含以下表單。載入事件: 隱藏,複製Code

// Form.Load event
private void DialogMessage_Load(object sender, EventArgs e) 
{
      // Set the starting locations and sizes of the labels
      // Adjust the locations and sizes of the labels to properly show the information
}

cs包含下面的三個程式碼塊;a公共靜態方法和兩個列舉: 隱藏,複製Code

/// <summary>
/// A public static method with a return value of System.Windows.Forms.DialogResult
/// </summary>
/// <paramname="_windowTitle"></param>
/// <paramname="_mainInstruction"></param>
/// <paramname="_msgButtons"></param>
/// <paramname="_msgIcons"></param> // Optional parameter with default value of None
/// <paramname="_content"></param> // Optional parameter with empty default value
/// <returns></returns>
public static DialogResult ShowMessage(string _windowTitle,
                                       string _mainInstruction, 
                                       MsgButtons _msgButtons,
                                      MsgIcons _msgIcons = MsgIcons.None,
                                       string _content = "") 
{
      // Set button and icon configurations and show the information to the user
}

隱藏,複製Code

// Message button enum for switch statement in ShowMessage
// This will set the properties of the form buttons and their DialogResult
public enum MsgButtons
{
      OK = 0,
      OKCancel = 1,
      YesNo = 2
}

隱藏,複製Code

// Message icon enum for switch statement in ShowMessage
// This will set the Image for the PictureBox
public enum MsgIcons
{
    None = 0,
    Question = 1,
    Info = 2,
    Warning = 3,
    Error = 4,
    Shield = 5
}

事件和方法 讓我們進入每個程式碼塊,看看它是做什麼的。 的形式。在MainForm.cs中的載入事件: 隱藏,收縮,複製Code

private void DialogMessage_Load(object sender, EventArgs e)
{
  // Once the ShowMessage function is called and the form appears
  // the code below makes the appropriate adjustments so the text appears properly

  // If no icon will be shown then shift the MainInstruction and Content
  // left to an appropriate location

  // Adjust the MaximumSize to compensate for the shift left.
  if (msgIcon.Visible == false)
  {
    mainInstruction.Location = new Point(12, mainInstruction.Location.Y);
    mainInstruction.MaximumSize = new Size(353, 0);

    content.Location = new Point(12, content.Location.Y);
    content.MaximumSize = new Size(353, 0);
  }

  // Gets the Y location of the bottom of MainInstruction
  int mainInstructionBottom = mainInstruction.Location.Y + mainInstruction.Height;

  // Gets the Y location of the bottom of Content
  int contentBottom = content.Location.Y + content.Height;

  // Offsets the top of Content from the bottom of MainInstruction
  int contentTop = mainInstructionBottom + 18; // 18 just looked nice to me

  // Sets new location of the top of Content
  content.Location = new Point(content.Location.X, contentTop);

  if (content.Text == string.Empty)

    // If only MainInstruction is provided then make the form a little shorter
    Height += (mainInstruction.Location.Y + mainInstruction.Height) - 50;
  else
    Height += (content.Location.Y + content.Height) - 60;
}

DialogMessage.cs中的ShowMessage方法: 隱藏,收縮,複製Code

public static DialogResult ShowMessage(string _windowTitle,
                    string _mainInstruction,
                    MsgButtons _msgButtons,
                    MsgIcons _msgIcons = MsgIcons.None,
                    string _content = "")
{
  // Creates a new instance of MainForm so we can set the properties of the controls
  MainForm main = new MainForm();

  // Sets the initial height of the form
  main.Height = 157;

  // Sets Window Title
  main.Text = _windowTitle;

  // Sets MainInstruction
  main.mainInstruction.Text = _mainInstruction;

  // Sets Content
  main.content.Text = _content;

  // Sets the properties of the buttons based on which enum was provided
  switch (_msgButtons)
  {
    // Button1 is the left button
    // Button2 is the right button

    case MsgButtons.OK:
     
      main.Button1.Visible = false;
      main.Button2.DialogResult = DialogResult.OK;
      main.Button2.Text = "OK";
      main.AcceptButton = main.Button2;
      main.Button2.TabIndex = 0;
      main.ActiveControl = main.Button2;

      break;

    case MsgButtons.OKCancel:

      main.Button1.DialogResult = DialogResult.OK;
      main.Button2.DialogResult = DialogResult.Cancel;
      main.Button1.Text = "OK";
      main.Button2.Text = "Cancel";
      main.AcceptButton = main.Button2;
      main.Button1.TabIndex = 1;
      main.Button2.TabIndex = 0;
      main.ActiveControl = main.Button2;

      break;

    case MsgButtons.YesNo:

      main.Button1.DialogResult = DialogResult.Yes;
      main.Button2.DialogResult = DialogResult.No;
      main.Button1.Text = "Yes";
      main.Button2.Text = "No";
      main.AcceptButton = main.Button2;
      main.Button1.TabIndex = 1;
      main.Button2.TabIndex = 0;
      main.ActiveControl = main.Button2;

      break;

    default:
      break;
  }

  // Sets the Image for the PictureBox based on which enum was provided
  if (_msgIcons != MsgIcons.None)
  {
    main.msgIcon.Visible = true;

    switch (_msgIcons)
    {
      case MsgIcons.Question:

        main.msgIcon.Image = SystemIcons.Question.ToBitmap();
        break;

      case MsgIcons.Info:

        main.msgIcon.Image = SystemIcons.Information.ToBitmap();
        break;

      case MsgIcons.Warning:

        main.msgIcon.Image = SystemIcons.Warning.ToBitmap();
        break;

      case MsgIcons.Error:

        main.msgIcon.Image = SystemIcons.Error.ToBitmap();
        break;

      case MsgIcons.Shield:

        main.msgIcon.Image = SystemIcons.Shield.ToBitmap();
        break;

      default:
        break;
    }
  }
  else
  {
    main.msgIcon.Visible = false;
  }

  // Shows the message and gets the result selected by the user
  return main.ShowDialog();
}

結論 我希望這篇文章對您有用。我知道已經有一些關於CodeProject的深入文章介紹了Windows TaskDialog的訊息框替代品和包裝器(正是它們激發了這個專案的靈感),但是,我想把它作為學習如何編寫自己的訊息框的參考。 歷史 2020年4月25日 新增圖示功能。圖示的使用來自SystemIcons類。 2020年4月18日 重新設計程式碼以使用靜態類和方法。這樣就不需要建立一個新的DialogMessage例項。 2020年4月13日 初始版本 本文轉載於:http://www.diyabc.com/frontweb/news2561.html