简体   繁体   中英

Windows Compact Framework custom popup notification

I've been trying to create popup notification for my windows, something like toasts in android.

  • 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. 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:

     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. 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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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