简体   繁体   English

Windows Compact Framework自定义弹出通知

[英]Windows Compact Framework custom popup notification

I've been trying to create popup notification for my windows, something like toasts in android. 我一直在尝试为我的窗口创建弹出通知,类似于android中的Toast。

  • It Shouldn't care about active from 它不应该在乎来自
  • It Should always be on top (for duration it's active) 它应该始终位于顶部(在持续时间内有效)
  • It Shouldn't block currently active form 它不应该阻止当前活动的表单
  • It Would be nice if its click trough 如果它的点击槽会很好

I know about Microsoft.WindowsCE.Forms.Notification but it doesn't go well with style of application, i tried creating custom class that inherits Notification, but i couldn't find a way to restyle it. 我知道Microsoft.WindowsCE.Forms.Notification,但它与应用程序的样式配合不佳,我尝试创建继承Notification的自定义类,但找不到重新设置其样式的方法。 I also tried creating topmost form, but that didn't work either, form wouldn't be show at all unless i used ShowDialog, but then it would be autosized to screen size.Here is sample of the way i was planning to create that from: 我也尝试过创建最上面的表单,但是那也不起作用,除非我使用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 is not supported in all platforms. 并非所有平台都支持Microsoft.WindowsCE.Forms.Notification You might want to stick to your own implementation. 您可能想要坚持自己的实施。 And about that, here is what I'd do( not tested ): 关于此,这是我要做的( 未测试 ):

Create a Class Library project. 创建一个类库项目。 Then add a Form. 然后添加一个表格。 Now add a Label control and a Button control as below: 现在添加一个Label控件和一个Button控件,如下所示:

在此处输入图片说明

Edit Form's properties: 编辑表单的属性:

ControlBox = false
FormBorderStyle = FixedDialog
TopMost = true  

Add the following code to form: 将以下代码添加到表单中:

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

Now add a class: 现在添加一个类:

updated 更新

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

Finally use it like: 最后像这样使用它:

updated 更新

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

Ideas for Extending 扩展思路

  • Provide access to Form's controls 提供对表单控件的访问
  • Specify Location & Size 指定位置和大小
  • Add an icon. 添加一个图标。
  • Change opacity of notification 更改通知的不透明度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM