【问题标题】:Windows Compact Framework custom popup notificationWindows Compact Framework 自定义弹出通知
【发布时间】:2016-11-02 11:02:22
【问题描述】:

我一直在尝试为我的窗口创建弹出通知,类似于 android 中的 toasts。

  • 它不应该关心主动来自
  • 它应该始终位于顶部(在它处于活动状态期间)
  • 它不应该阻止当前活动的表单
  • 如果有点击槽就好了

我知道 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();

【问题讨论】:

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


    【解决方案1】:

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

    扩展思路

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

    【讨论】:

    • 这行得通,但是几乎没有问题,即使它被标记为保持在顶部,当它失去焦点时它会落后于当前聚焦的表单,当通知出现在顶部栏时,所有表单也会最大化(带有电池,wifi信号等)出现它不应该(不能使用最大化,因为它不是点击。
    • 你说得对,我已经添加了一些东西来保持它的顶部,请参阅我的更新答案。对于顶栏,根据平台,您可以隐藏此栏:简单的谷歌搜索将显示答案。
    • 是的,我设法通过覆盖focused并使其每次都返回false来解决这个问题:'public override bool Focused { get { return false; } }' 无论如何,感谢您的帮助,我成功地完成了我想做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 2010-12-11
    相关资源
    最近更新 更多