簡體   English   中英

Windows Compact Framework自定義彈出通知

[英]Windows Compact Framework custom popup notification

我一直在嘗試為我的窗口創建彈出通知,類似於android中的Toast。

  • 它不應該在乎來自
  • 它應該始終位於頂部(在持續時間內有效)
  • 它不應該阻止當前活動的表單
  • 如果它的點擊槽會很好

我知道Microsoft.WindowsCE.Forms.Notification,但它與應用程序的樣式配合不佳,我嘗試創建繼承Notification的自定義類,但找不到重新設置其樣式的方法。 我也嘗試過創建最上面的表單,但是那也不起作用,除非我使用ShowDialog,否則根本不會顯示該表單,但是它將自動調整為屏幕大小。以下是我計划創建該表單的示例從:

     Form frm = new Form();
     frm.TopMost = true;
     Label lbl = new Label();
     lbl.Text = "TEST";
     lbl.Parent = frm;
     frm.Bounds = new Rectangle(15, 15, 150, 150);
     frm.WindowState = FormWindowState.Normal;
     frm.FormBorderStyle = FormBorderStyle.None;
     frm.AutoScaleMode = AutoScaleMode.None;
     frm.Show();

並非所有平台都支持Microsoft.WindowsCE.Forms.Notification 您可能想要堅持自己的實施。 關於此,這是我要做的( 未測試 ):

創建一個類庫項目。 然后添加一個表格。 現在添加一個Label控件和一個Button控件,如下所示:

在此處輸入圖片說明

編輯表單的屬性:

ControlBox = false
FormBorderStyle = FixedDialog
TopMost = true  

將以下代碼添加到表單中:

public partial class FormNotification : Form
{
    private Timer timer;
    public int Duration { get; private set;}

    public FormNotification(string message, int duration)
    {
        InitializeComponent();

        this.labelMessage.Text = message;
        this.Duration = duration;

        this.timer = new Timer();
        this.timer.Interval = 1000;
        this.timer.Tick += new EventHandler(timer_Tick);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        if (Duration <= 0)
            this.Close();
        this.Duration--;
    }

    private void buttonHide_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void FormNotification_Load(object sender, EventArgs e)
    {
        this.timer.Enabled = true;
    }
}

現在添加一個類:

更新

public class CNotification
{
    public CNotification()
    {

    }

    public static void Show(Form owner, string message, int duration)
    {
        FormNotification formNotification = new FormNotification(message, duration);
        formNotification.Owner = owner;
        formNotification.Show();
    }
}

最后像這樣使用它:

更新

// assuming call from a form
CNotification.Show(this, "Hello World", 5);

擴展思路

  • 提供對表單控件的訪問
  • 指定位置和大小
  • 添加一個圖標。
  • 更改通知的不透明度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM