繁体   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