开发者

How to show non-interactive, auto-hiding notification popup message with Windows Forms in c#?

开发者 https://www.devze.com 2022-12-12 03:26 出处:网络
Using Windows Forms, .NET 3.5 framework, language: c# I would like to show a popup window for 1 second to notify users of actions that are performed. For example, when I copy a file X I want to show a

Using Windows Forms, .NET 3.5 framework, language: c# I would like to show a popup window for 1 second to notify users of actions that are performed. For example, when I copy a file X I want to show a no开发者_JAVA技巧tification like "Copied file X to File X-copy". Should be shown for a second, then autohide.


You can use a timer. Something along the lines of the following where ShowFloating does the initial display and HideFloating does, you know.

public void ShowFloatingForXMilliSeconds(int milliSeconds) {
    ShowFloating();
    if (_autoOffTimer == null) {
        _autoOffTimer = new System.Timers.Timer();
        _autoOffTimer.Elapsed += OnAutoOffTimerElapsed;
        _autoOffTimer.SynchronizingObject = this;
    }
    _autoOffTimer.Interval = milliSeconds;
    _autoOffTimer.Enabled = true;
}

void OnAutoOffTimerElapsed(Object sender, System.Timers.ElapsedEventArgs ea) {
    if ((_autoOffTimer != null) && _autoOffTimer.Enabled) {
        _autoOffTimer.Enabled = false;
        HideFloating();
    }
}

Also detach the timer handler and dispose the timer in Dispose.


This topic will help you to make topmost window without stealing focus from currently active window.

To complete your solution, in simple case you need to add a timer on your form to make sure the form auto-closes after 1 second and locate your notification window properly (you probably want it in the bottom right part of the screen? - that's a simple arithmetic exercise).

For more advanced solution, you should create NotificationManager class and manage lifetime of your notification message forms there.


Dispite the answers given. I think a message that pops up is somewhat not user friendly. What about using a statusbar link? It's not that evasive (and you can show the progress)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号