开发者

How should I display a notification bar in WinForms?

开发者 https://www.devze.com 2022-12-28 12:07 出处:网络
You all know the \"You\'ve got new answers!\" notification bar on SO. I\'d like the same thing in a Form, preferably just as smooth. Is t开发者_如何学编程here a simple way? Or do I have to completely

You all know the "You've got new answers!" notification bar on SO. I'd like the same thing in a Form, preferably just as smooth. Is t开发者_如何学编程here a simple way? Or do I have to completely create this myself?

My searches did not yield any good results, only lots of progress bars and popups in the system notification area, but that's not what I'm looking for.

The messages I want to display belong to a Form, not to the whole application


You could simply animate a panel dropping down from the top of the client area of the form.

Increasing the y coordinate of the panel in a timed loop. The panel would start invisible and slowly become visible. (The panel would start at -panel.height and work its way down to 0.)


Create two panels in your form, a notification panel docked to top, and below that a content panel anchored to top. In your Form.Load, set the height of the notification panel to zero. Don't set the height to zero in Design View, you won't be able to click on the notification panel to edit it.

Then when you get a notification, draw the contents in the notification panel and create a System.Windows.Form.Timer that increases the height of the notification panel by a few pixels every few dozen milliseconds or so. Stop when the panel is of the desired height. Do the same with a negative height to hide the panel.

This does not require repainting or recalculating sizes or positions of anything, does not overdraw anything, and looks slick. I have done this and it works.


If you want it constrained to a particular form, it should be easy enough to put a Panel on the form with its Dock set to DockStyle.Top, then place a label for the description and a button that hides it.


It's not difficult to do with a panel or a UserControl, but the fiddly part is making the contents of the form slide down as the bar slides down. To simplify that I would use a SplitContainer. The top splitpanel contains the notification bar and the splitter distance is initially 0. Slide the bar into view by incrementing the SplitterDistance property. Doing it this way means you don't have to worry about making the other contents of the form slide down (which is a hassle because it prevents you from using docking).

The only downside to using SplitContainer I can think of is that the animation of the bar will be slightly different: the text won't scroll down with the bar, it will be revealed in place as the bar slides down. If this bothers you, you can fix it by having the text (or your panel/custom control) slide down as you increase the splitter distance (only a couple more lines of code).

Showing the bar:

    for (int i = 0; i <= 33; i++)
    {
        splitContainer1.SplitterDistance = i;
        Thread.Sleep(5);
        Refresh();
    }

Hiding the bar:

    for (int i = 33; i >= 0; i--)
    {
        splitContainer1.SplitterDistance = i;
        Thread.Sleep(5);
        Refresh();
    }

Of course, if you don't mind the notification bar simply covering the top part of your form, then you can just do the whole thing very easily with a panel. :)


I was looking for the same thing just now and found this on code project

I have not used it yet, So I have no idea how solid it is.

0

精彩评论

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

关注公众号