Windows Compact Framework自定义弹出通知

时间:2016-11-02 11:02:22

标签: c# .net user-interface notifications compact-framework

我一直在尝试为我的Windows创建弹出通知,就像在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();

1 个答案:

答案 0 :(得分:1)

并非所有平台都支持

Microsoft.WindowsCE.Forms.Notification。您可能希望坚持自己的实现。关于这一点,这就是我要做的事情(未经过测试):

创建一个类库项目。然后添加一个表格。现在添加一个Label控件和一个Button控件,如下所示:

enter image description here

编辑表单的属性:

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);

扩展的想法

  • 提供对表单控件的访问
  • 指定位置&amp;大小
  • 添加图标。
  • 更改通知的不透明度