<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.toString() &#187; C#.NET Scroller Ticker Message Labels Smooth</title>
	<atom:link href="http://www.dottostring.com/tag/cnet-scroller-ticker-message-labels-smooth/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dottostring.com</link>
	<description>Programming is our passion</description>
	<lastBuildDate>Sun, 04 Oct 2009 15:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Bar in C#.NET</title>
		<link>http://www.dottostring.com/2008/11/how-to-make-pop-up-notification-bar-with-message-ticker-smooth-message-scroller-over-the-task-bar-in-cnet/</link>
		<comments>http://www.dottostring.com/2008/11/how-to-make-pop-up-notification-bar-with-message-ticker-smooth-message-scroller-over-the-task-bar-in-cnet/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 20:53:10 +0000</pubDate>
		<dc:creator>babarjehangir</dc:creator>
				<category><![CDATA[C#.NET]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#.NET Scroller Ticker Message Labels Smooth]]></category>

		<guid isPermaLink="false">http://www.dottostring.com/?p=97</guid>
		<description><![CDATA[few weeks ago I had to work on an application that had a client piece which was required to act like the a one-way messenger e.g. Supervisors wanted to send in messages to their sub ordinates. one of the requirements was whenever a new message arrived, a notification bar should pop-up over the task bar [...]]]></description>
			<content:encoded><![CDATA[<p>few weeks ago I had to work on an application that had a client piece which was required to act like the a one-way messenger e.g. Supervisors wanted to send in messages to their sub ordinates. one of the requirements was whenever a new message arrived, a notification bar should pop-up over the task bar with all the new messages appearing in a ticker fashion ( also messages should scroll smoothly).</p>
<p>There is no .net control available to achieve this. so I searched the internet in hope to get hold of some (free) third party custom controls, but dint find anything of significant help, all I found was how to make characters within the label text scroll horizontally. so I decided to make something of my own.</p>
<p>problems I had to cope with here were:</p>
<ul>
<li>how to make a windows form act as a pop-up/Notification bar over the task bar?</li>
<li>how to implement message ticker (i.e. smooth scrolling of the messages on the pop-up/Notification bar)?</li>
</ul>
<h5>how to make a windows form act as a pop-up/Notification bar over the task bar?</h5>
<ul>
<li>As always, we begin by creating a new Windows Application (C#.NET)</li>
</ul>
<div id="attachment_99" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-99" title="An illustration of creating a new windows application (C#.NET)" src="http://www.dottostring.com/wp-content/uploads/2008/11/notification-tickernew-project.jpg" alt="An illustration of creating a new windows application (C#.NET)" width="500" height="363" /><p class="wp-caption-text">An illustration of creating a new windows application (C#.NET)</p></div>
<ul>
<li>now by quickly doing the following:
<ul>
<li>Rename the Form1.cs with something meaningful, such as &#8220;NotificationTicker.cs&#8221;, Press &#8220;Yes&#8221; when it asks for renaming reference content.</li>
<li>Re size the form such that it looks like a notification bar.</li>
<li>Drop in a group box from the toolbox and re size it to stretch towards the boundaries.</li>
<li>Drop in a label in the group box area and assign some dummy text to it.</li>
<li>Set ControlBox Property of the Form to false.</li>
<li>Set ShowInTaskbar Property of the Form to false.</li>
</ul>
<p>we get</li>
</ul>
<div id="attachment_106" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-106" title="Notification Ticker Initial Form Sample" src="http://www.dottostring.com/wp-content/uploads/2008/11/notification-tickernotification-tiker-form.jpg" alt="Notification Ticker Initial Form Sample" width="500" height="350" /><p class="wp-caption-text">Notification Ticker Initial Form Sample</p></div>
<ul>
<li>verify that everything is working fine by compiling and running the application (F5)</li>
<li>Width of the form should be equal to the width of the user screen, this is simple, all we need here is to find out the working area of the client machine.</li>
<li>Working Area of the Client Machine is the space on screen available for the Forms to appear (excluding Taskbars, Docked Windows e.t.c).</li>
<li>we get this space in the form of rectangle.</li>
</ul>
<pre name="code" class="c#">Rectangle recWorkingArea = new Rectangle();
recWorkingArea = Screen.PrimaryScreen.WorkingArea;</pre>
<ul>
<li>by using the working area and a bit of mathematics we can get what we want.</li>
</ul>
<pre name="code" class="c#">        private int getScreenWidth()
        {
            Rectangle recWorkingArea = new Rectangle();
            recWorkingArea = Screen.PrimaryScreen.WorkingArea;
            return recWorkingArea.Width;
        }
        private int getScreenHeight()
        {
            Rectangle recWorkingArea = new Rectangle();
            recWorkingArea = Screen.PrimaryScreen.WorkingArea;
            return recWorkingArea.Height;
        }
        private void FitToScreen()
        {
            this.Size = new Size(getScreenWidth(), this.FormHeight);
            this.Location = new System.Drawing.Point(0, this.Location.Y);
        }</pre>
<ul>
<li>FitToScreen() when called will span the form to the width of the user screen.</li>
<li>Now we need to re size the group box as well, that&#8217;s also done with a bit of simple mathematics</li>
</ul>
<pre name="code" class="c#">private void SetGroupBox()
{
    int diff = (this.Location.X + this.Width) - (this.gbNotificationCenter.Location.X + gbNotificationCenter.Width);
    this.gbNotificationCenter.Size = new Size((getScreenWidth() - diff) - this.gbNotificationCenter.Location.X, this.gbNotificationCenter.Height);
}</pre>
<ul>
<li>call these functions ( FitToScreen and SetGroupBox ) in the below sequence in the form constructor and then Run(F5) your application</li>
</ul>
<pre name="code" class="c#">        public NotificationTicker()
        {
            InitializeComponent();
            SetGroupBox();
            FitToScreen();
        }</pre>
<ul>
<li>if you have done everything as right, you should see a form with the same height (as in the design time) but with a width equal to the your screen width.</li>
</ul>
<p>now the pop-up part, this requires a bit of tinkling with the Location.Y ( Top-Left ) of the form. if we can somehow place the form initially at the height of the working area and then start decrementing it till Location.Y(of the form) + Height(of the form) equals Height of the working area, this would do the trick. Also note that all of this code would be written in Activated Event of the form, so that whenever the form is activated this form gives an effect of pop-up notification bar.</p>
<p>define a variable FormHeight and initialize it to a static value of the form height shown at the design time. because of at the runtime Height (of the form) becomes (form height (at design time) + ControlBox height).</p>
<pre name="code" class="c#">private void NotificationTicker_Activated(object sender, EventArgs e)
{
    //firsttime is a global boolean variable initialized to true
    if (firsttime)
    {
        //setting this to false, right away. because activated is called again whenever
        //form gets the focus.
        firsttime = false;
        //Initializing the form to the bottom of the working area ( out of visible area )
        this.Location = new System.Drawing.Point(0, getScreenHeight());
        this.Refresh();
        //Read about the below line in the next section, where we will enable it
        //InitiateScrollingThread();
        //calculating the seed, so that opacity increases from 0% to 100% during the course of popping up.
        double opacityIncreaseSeed = 100.0 / (Convert.ToDouble(this.FormHeight) * 100.0);
        //Starting Index for the loop
        int ScreenHeight = getScreenHeight();
        //End Index for the loop to stop
        int StartY = getScreenHeight() - this.FormHeight;
        for (int i = ScreenHeight; i &gt;= StartY; i--)
        {
            //Increasing opacity gradually
            this.Opacity = this.Opacity + opacityIncreaseSeed;
            //updating the location to give the move up effect
            this.Location = new System.Drawing.Point(0, i);
            this.Refresh();
            //to put some delay.
            Thread.Sleep(10);
        }
        //finally setting Opacity of the form to 100%
        this.Opacity = 1.0;
    }
}</pre>
<ul>
<li>Run(F5) the application and there you have a smooth and graceful pop-up notification bar coming out over the taskbar.</li>
<li><a href="http://www.dottostring.com/wp-content/uploads/2008/11/dottostringnotificationbar.zip">Download</a> Source Code up till here</li>
</ul>
<p>moving on to the second problem</p>
<h5>how to implement message ticker (i.e. smooth scrolling of the messages on the pop-up/Notification bar)?</h5>
<p>This is a bit tricky, but the idea is pretty simple. if you think you can implement smooth scrolling / ticker with two labels potentially. All you need to do is move one label out of the view and simultaneously moving the other one into view, assign a new message to the label that went out of the view and start to move it into the view again while the other one goes out of the view so on and so forth.</p>
<p>to accomplish this we need to drop in a panel and two labels, NOTE: Labels should be placed such that, they are inside the panel.</p>
<div id="attachment_114" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.dottostring.com/wp-content/uploads/2008/11/notification-tickernotification-ticker-with-two-labels1.jpg"><img class="size-full wp-image-114" title="Demonstration of Notification Ticker with Two labels" src="http://www.dottostring.com/wp-content/uploads/2008/11/notification-tickernotification-ticker-with-two-labels1.jpg" alt="Demonstration of Notification Ticker with Two labels" width="500" height="352" /></a><p class="wp-caption-text">Demonstration of Notification Ticker with Two labels</p></div>
<p>here we need to understand that scrolling will be a continuous event and therefore we cannot have this method in the same application thread. we need to create a separate thread to handle the scrolling.</p>
<p>all of this is handled by the following two functions</p>
<pre class="c#" name="code">private void InitiateScrollingThread()
{
    //Bottom of the Panel, will be used for hiding one of the two labels
    int PanelBottom = panel1.Height;
    //Setting the second label below the panel (effectively taking it out of view)
    lblMessageTwo.Location = new Point(lblMessageTwo.Location.X, PanelBottom);
    // we have to set this to false, otherwise we wont able to update the location
    // of the labels from the scrolling thread.
    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
    //Initializing the scrollerThread(defined globally, Thread scrollerThread)
    scrollerThread = new Thread(BeginScroll);
    //Starting the BeginScroll Function at a separate thread
    scrollerThread.Start();
}
private void BeginScroll()
{
    //This variable will take label out of view
    int LabelHeight = -lblMessageOne.Height;
    //This variable will be used to assign out of view variable to bottom of the panel, to re-enter
    int PanelBottom = panel1.Height;
    while (true)
    {
        if (lblMessageOne.Location.Y == LabelHeight)
        {
            //when label one goes out of view, it is configured to re enter again
            lblMessageOne.Location = new Point(lblMessageOne.Location.X, PanelBottom);
        }
        else
        {
            //Scrolling label by decrementing it y-axis location
            int MessageOneY = lblMessageOne.Location.Y - 1;
            lblMessageOne.Location = new Point(lblMessageOne.Location.X, MessageOneY);
        }
        if (lblMessageTwo.Location.Y == LabelHeight)
        {
            //when label one goes out of view, it is configured to re enter again
            lblMessageTwo.Location = new Point(lblMessageTwo.Location.X, PanelBottom);
        }
        else
        {
            //Scrolling label by decrementing it y-axis location
            int MessageTwoY = lblMessageTwo.Location.Y - 1;
            lblMessageTwo.Location = new Point(lblMessageTwo.Location.X, MessageTwoY);
        }
        this.Refresh();
        Thread.Sleep(50);

    }
}</pre>
<ul>
<li>make a call to InitiateScrollingThread() funcation in the Activated Event defined above (in the if condition after setting the Location of the form)</li>
<li>and that&#8217;s it, we are done.</li>
<li>Run (F5) the application and the see smooth pop-up notification bar over the taskbar and then the smoothly scrolling messages.</li>
<li><a href="http://www.dottostring.com/wp-content/uploads/2008/11/dottostringnotificationbarticker.zip">Download</a> Source Code Here</li>
<li>Cheers (don&#8217;t forget to leave your feedback/concerns)</li>
</ul>
<p>Related Articles:</p>
<ul>
<li><a href="http://www.dottostring.com/2008/12/how-to-create-make-a-windows-form-act-like-a-docking-window-without-a-container-in-c-sharp-net/" target="_blank">How to create (make a Windows Form act like) a Docking Window without a container in C#.NET</a></li>
<li><a href="http://www.dottostring.com/2008/12/how-to-make-pop-up-notification-bar-with-message-ticker-smooth-message-scroller-over-the-task-bar-in-c-sharp-net-how-to-stopstart-the-ticker-on-mouse-hoverleave/" target="_blank">How to make pop-up / Notification bar with Message Ticker (smooth message scroller) over the Task Bar in C#.NET &#8211; How to Stop/Start the Ticker on Mouse Hover/Leave</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.dottostring.com%2F2008%2F11%2Fhow-to-make-pop-up-notification-bar-with-message-ticker-smooth-message-scroller-over-the-task-bar-in-cnet%2F';
  addthis_title  = 'How+to+make+pop-up+%2F+Notification+bar+with+Message+Ticker+%28smooth+message+scroller%29+over+the+Task+Bar+in+C%23.NET';
  addthis_pub    = 'erfaan';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.dottostring.com/2008/11/how-to-make-pop-up-notification-bar-with-message-ticker-smooth-message-scroller-over-the-task-bar-in-cnet/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
