<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">

<channel>
<title><![CDATA[www.taymaz.ir]]></title>
<link><![CDATA[http://www.taymaz.ir]]></link>
<description><![CDATA[دانلود]]></description>
<generator>taymaz farshi</generator>
<category><![CDATA[تاريخ و تقويم]]></category>
<item>
<title><![CDATA[Jalali Calendar]]></title>
<link><![CDATA[http://www.taymaz.ir/download_Jalali+Calendar.html]]></link>
<guid isPermaLink="true"><![CDATA[http://www.taymaz.ir/download_Jalali+Calendar.html]]></guid>
<description><![CDATA[persian date convertor<br />
<br />
<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" class="Apple-style-span"><span style="font-family: Verdana; font-size: 13px;" class="Apple-style-span">
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><img src="http://www.codeproject.com/KB/cs/JalaliCalendar/JalaliCalendar.gif" alt="" /></p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Introduction</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Jalali (Persian) calendar is the official calendar in Iran and some neighbor territories. Although Jalali is a solar calendar with strict rules, it has always been a challenge for programmers to provide some means of conversions for it and the Gregorian calendar. If you search CodeProject for terms like �<i>jalali</i>�, �<i>persian</i>� or �<i>shamsi</i>�, you�ll find some good codes that try to present a method for converting dates in Gregorian to dates in Persian calendar and vice versa. The Microsoft .NET Framework 2 contains the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>class in the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">System.Globalization</code><span class="Apple-converted-space">&nbsp;</span>namespace. It should have solved all the problems but the truth is that the Persian calendar is not a simple periodic one as the algorithm used in the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>class supposes.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The main problem is in leap years. Jalali calendar is based on the actual observation of the sun, other than established mathematical principals. The<code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>uses an algorithm sometimes called<span class="Apple-converted-space">&nbsp;</span><i>Khayam</i>. It assumes that a given Jalali year is a leap year if the remainder of it divided by 33 be 1, 5, 9, 13, 17, 22, or 30. This simple pattern fails for 1634, just 250 years by now. In fact the algorithm is correct for the years 1178 to 1634 Jalali (1799 to 2256 Gregorian). Here I will describe another method that covers a much wider range.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The codes and the algorithm that would be described in this article are originally written by<span class="Apple-converted-space">&nbsp;</span><i>Bill J. Gray</i>. He kindly allowed me to port his C codes to this .NET class.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Jalali Calendar in Details</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The Jalali calendar is named after king<span class="Apple-converted-space">&nbsp;</span><i>Jalal-al-Din Malek-Shah Saljouqi</i>. The Iranian mathematician and poet<span class="Apple-converted-space">&nbsp;</span><i>Omar Khayam</i><span class="Apple-converted-space">&nbsp;</span>has done much of the works to designate the rules of the calendar.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The new year of Jalali begins at the astronomical moment of the beginning of spring. Since not all programmers are astronomers, I will explain some astronomical terms in this article. Everybody knows that the sun moves east to west in the sky, because the earth is spinning. But if the sun was too dim that we could see other stars around it, we would have noticed another motion: the sun slowly moving from west to east across the sky. This motion is caused by the earth orbiting the sun, and it has a period of nearly 365.24 days. The fraction (0.24) is why we must have leap years in calendars; and because 0.24 is not exactly 0.25 (a quarter), the periods of leap years in the Jalali calendar are so complex. Anyway, a new year in Jalali calendar begins just exactly when the sun is located in a point in the sky we call<span class="Apple-converted-space">&nbsp;</span><i>Vernal Equinox</i>. For simplicity, I call this moment<span class="Apple-converted-space">&nbsp;</span><i>Norooz</i><span class="Apple-converted-space">&nbsp;</span>(Norooz is the name of the new year celebrations in Iran).</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Jalali calendar rules say that if Norooz happens before local midday, that day is the first day of the new year, else the following day is. Each Jalali year has 12 months. The first 6 months are all 31-days long, the remaining 5 months have 30 days, and the last month is also a 30-day month in leap years, or has 29 days otherwise.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">This pattern is so interesting; at the first half of the year, the earth is farther from the sun, so it moves slower than the second half of the year when the months are 30-days long. This resembles<span class="Apple-converted-space">&nbsp;</span><a style="text-decoration: none;" target="_blank" href="http://www-spof.gsfc.nasa.gov/stargaze/Skeplaws.htm">Kepler�s third law</a>.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">How Does It Work</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">All we need to do is to find the instance of vernal equinox (Norooz). If we know this moment for a given year (and its following year), then we can make that year�s calendar. There is a term in astronomy called<span class="Apple-converted-space">&nbsp;</span><i>Julian day</i><span class="Apple-converted-space">&nbsp;</span>(it has nothing to do with the Julian calendar). A Julian day is a means to express dates in real numbers other than the common y-m-d format. It would be more useful for mathematical purposes if we express dates by numbers like 2453823.92452 instead of March 29, 2006 10:11:18 UT. The .NET framework supports<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">TimeSpan</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Ticks</code><span class="Apple-converted-space">&nbsp;</span>but a Julian day is widely used in astronomical calculations. There is another advantage, the concept of a Julian day provides us a means to deal with BCE dates (see the<span class="Apple-converted-space">&nbsp;</span><i>Points of interest</i><span class="Apple-converted-space">&nbsp;</span>section). A Julian day is the days passed from midday (GMT) of January 1, -4712 Gregorian. Theoretically, the dates in any calendar can be converted to Julian day and back (I am not sure about �any�, but at least for Persian calendar, this can be done).</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Although the motion of the earth is not completely periodic, there are algorithms for finding the instance of vernal equinox (of course, they are only accurate for a limited time but thousands of years). If we have a table of Julian days of vernal equinoxes over thousands of years, then we can have the Jalali calendar in that range. There are some books published in Persian that provide this method for conversion. Bill J. Gray had a better way; he introduced an algorithm that matches the previous method for Persian years between -1096 and +2327 (Gregorian years -457 to +2948).</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">It starts by finding the Julian day number of Norooz for a given year:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain0"><img height="9" width="9" preid="0" src="http://www.codeproject.com/images/minus.gif" id="preimg0" alt="" /><span style="margin-bottom: 0px;" preid="0" id="precollapse0"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre0"><span style="color: blue;" class="code-keyword">private</span> <span style="color: blue;" class="code-keyword">static</span> <span style="color: blue;" class="code-keyword">int</span> jalali_jd0(<span style="color: blue;" class="code-keyword">int</span> jalali_year)
{
   <span style="color: blue;" class="code-keyword">int</span> rval;
   <span style="color: blue;" class="code-keyword">if</span> (jalali_year &lt; LOWER_PERSIAN_YEAR)
       <span style="color: blue;" class="code-keyword">throw</span> <span style="color: blue;" class="code-keyword">new</span> ArgumentOutOfRangeException(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">jalali_year&quot;</span>, 
             <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">the value must be bigger than &quot;</span> + 
             LOWER_PERSIAN_YEAR.ToString());
   <span style="color: blue;" class="code-keyword">for</span> (<span style="color: blue;" class="code-keyword">int</span> i = <span style="color: navy;" class="code-digit">0</span>; i &lt; <span style="color: navy;" class="code-digit">12</span>; i++)
       <span style="color: blue;" class="code-keyword">if</span> (jalali_year &lt; breaks[i])
       {
           rval = JALALI_ZERO + jalali_year * <span style="color: navy;" class="code-digit">365</span> + 
                    (deltas[i] + jalali_year * <span style="color: navy;" class="code-digit">303</span>) / <span style="color: navy;" class="code-digit">1250</span>;
           <span style="color: blue;" class="code-keyword">if</span> (i &lt; <span style="color: navy;" class="code-digit">3</span>)  <span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">zero point drops one day in first three blocks <br /></span>
               rval--;
           <span style="color: blue;" class="code-keyword">return</span> (rval);
       }
   <span style="color: blue;" class="code-keyword">throw</span> <span style="color: blue;" class="code-keyword">new</span> ArgumentOutOfRangeException(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">jalali_year&quot;</span>, 
             <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">the value must be bigger than &quot;</span> + 
             LOWER_PERSIAN_YEAR.ToString() + 
             <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string"> and less than &quot;</span> + 
             UPPER_PERSIAN_YEAR.ToString());
}</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">It is quite straightforward,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JALALI_ZERO</code><span class="Apple-converted-space">&nbsp;</span>is the Julian day of the first day of the Jalali year zero (which is equal to 1947954).<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">breaks</code><span class="Apple-converted-space">&nbsp;</span>is an array of Jalali years while<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">deltas</code><span class="Apple-converted-space">&nbsp;</span>is an array of constant numbers for the years in breaks.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">If the year is outside the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">LOWER_PERSIAN_YEAR</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">UPPER_PERSIAN_YEAR</code><span class="Apple-converted-space">&nbsp;</span>values (which are -1096 and +2327), then we use another calendar system called the<span class="Apple-converted-space">&nbsp;</span><i>Modern Persian Calendar</i>. This is completely algorithmic (and much simpler), and while it does not match Jalali, it closely follows it. The method that returns the Julian date of the first day of the year in the Modern Persian calendar is called<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">persian_modern_jd0()</code><span class="Apple-converted-space">&nbsp;</span>and is available in the source code.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">There is a method named<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">day_to_dmy()</code><span class="Apple-converted-space">&nbsp;</span>that converts any Julian day to its corresponding date in Persian (or Gregorian) calendar. It starts by guessing the approximate calendar year for the given Julian day:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain1"><img height="9" width="9" preid="1" src="http://www.codeproject.com/images/minus.gif" id="preimg1" alt="" /><span style="margin-bottom: 0px;" preid="1" id="precollapse1"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre1">
year = (jd - JALALI_ZERO) / <span style="color: navy;" class="code-digit">365</span>;</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Then in a loop, it finds the exact year and calculates the Julian days of the first days of that year and its following year (using the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">jalali_jd0()</code><span class="Apple-converted-space">&nbsp;</span>method), and determines if the current year is a leap year:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain2"><img height="9" width="9" preid="2" src="http://www.codeproject.com/images/minus.gif" id="preimg2" alt="" /><span style="margin-bottom: 0px;" preid="2" id="precollapse2"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre2"><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment"> +1 to make sure the value is calculated<br /></span>
<span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment"> for the given year not its previous year<br /></span>
<span style="color: blue;" class="code-keyword">int</span> currentYearJD0 = jalali_jd0(year) + <span style="color: navy;" class="code-digit">1</span>;
<span style="color: blue;" class="code-keyword">int</span> nextYearJD0 = jalali_jd0(year + <span style="color: navy;" class="code-digit">1</span>) + <span style="color: navy;" class="code-digit">1</span>;
<span style="color: blue;" class="code-keyword">if</span>(nextYearJD0 � currentYearJD0 == <span style="color: navy;" class="code-digit">366</span>) {
    <span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">current year is a leap year<br /></span>
} <span style="color: blue;" class="code-keyword">else</span> {
    <span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">it is a normal year<br /></span>
}</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The same methods exist for the Gregorian calendar. We can convert any Gregorian date to its corresponding Julian day using a simple formula provided by<span class="Apple-converted-space">&nbsp;</span><i>Jean Meeus</i><span class="Apple-converted-space">&nbsp;</span>in<span class="Apple-converted-space">&nbsp;</span><i>Astronomical Algorithms</i>:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain3"><img height="9" width="9" preid="3" src="http://www.codeproject.com/images/minus.gif" id="preimg3" alt="" /><span style="margin-bottom: 0px;" preid="3" id="precollapse3"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre3"><span style="color: blue;" class="code-keyword">double</span> JulianDate(<span style="color: blue;" class="code-keyword">double</span> y, <span style="color: blue;" class="code-keyword">double</span> m, <span style="color: blue;" class="code-keyword">double</span> d, <span style="color: blue;" class="code-keyword">double</span> h)
{
  <span style="color: blue;" class="code-keyword">double</span> a,b;
  h = h / <span style="color: navy;" class="code-digit">24</span>; d = d + h;
  <span style="color: blue;" class="code-keyword">if</span>(m==1 || m==2) 
  {
    m += <span style="color: navy;" class="code-digit">12</span>; y-=1;
  }
  a = (<span style="color: blue;" class="code-keyword">int</span>)(y / <span style="color: navy;" class="code-digit">100</span>); b = <span style="color: navy;" class="code-digit">2</span> - a + (<span style="color: blue;" class="code-keyword">int</span>)(a / <span style="color: navy;" class="code-digit">4</span>);
  <span style="color: blue;" class="code-keyword">return</span> (<span style="color: blue;" class="code-keyword">int</span>)(<span style="color: navy;" class="code-digit">365</span>.<span style="color: navy;" class="code-digit">25</span> * y) + (<span style="color: blue;" class="code-keyword">int</span>)(<span style="color: navy;" class="code-digit">30</span>.<span style="color: navy;" class="code-digit">6001</span> * (m + <span style="color: navy;" class="code-digit">1</span>)) + d + <span style="color: navy;" class="code-digit">1720994</span>.<span style="color: navy;" class="code-digit">5</span> + b;
}</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">There is a reverse method that converts Julian days back to Gregorian dates. The algorithm could be found in the book. I have kept Gray�s implementation in the source code.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">And now we�ve done! We can conclude the method described above as:</p>
<ol>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Convert Jalali date to its corresponding Julian day</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Convert the calculated Julian day to Gregorian date</li>
</ol>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">And vice versa.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">What If You Want a Jalali Calendar Out of This Range</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The Jalali year range -1096 to 2327 fits all my historical needs. The Persian year of 2327 is equal to the Gregorian year 2949 and is the maximum year that the .NET Framework�s<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">DateTime</code><span class="Apple-converted-space">&nbsp;</span>supports. But it may happen for you that you want date conversations out of this range. The code provided in this article simply uses the Modern Persian Calendar when it gets a date out of this range. But it is possible to provide exact conversions for dates over thousands years, actually as long as the astronomical formulae that calculate the position of the sun in the sky permits, and also as long as the variable we call Delta-T is accurate enough. I will not go farther, just know that Delta-T varies by time and its value is not known for the future years.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Again, all we need to do is to populate a table of Julian days of spring equinoxes, but instead of using the simple<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">jalali_jd0()</code><span class="Apple-converted-space">&nbsp;</span>method, we need to find the moment by calculating the position of the sun in the sky. Meeus has provided some nice algorithms in his book. You can also take a look at the nice method Gray posted in his website. Dealing with Jalali calendar over a wide range of years is complex enough to be the subject of another article. If I get time, I�ll write it for CodeProject, otherwise you can find all the methods in Gray�s pages (see the Resources section).</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Using The Code</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">To make it all work with the .NET Framework, I made the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaliCalendar</code><span class="Apple-converted-space">&nbsp;</span>class that inherits<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">System.Globalization.Calendar</code>. I also included a structure type in the source code, named<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianDate</code>, which mimics the functionality of<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">System.DateTime</code>. I had written the structure about a year ago to use with the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>class, and although the code is quite funny, it was a handy tool for me. I changed it a little to work with the Jalali calendar. Using the type is straightforward:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain4"><img height="9" width="9" preid="4" src="http://www.codeproject.com/images/minus.gif" id="preimg4" alt="" /><span style="margin-bottom: 0px;" preid="4" id="precollapse4"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre4">
PersianDate pd1 = <span style="color: blue;" class="code-keyword">new</span> PersianDate(DateTime.Today);
PersianDate pd2 = <span style="color: blue;" class="code-keyword">new</span> PersianDate(<span style="color: navy;" class="code-digit">1385</span>, <span style="color: navy;" class="code-digit">1</span>, <span style="color: navy;" class="code-digit">13</span>);</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianDate</code><span class="Apple-converted-space">&nbsp;</span>type provides many properties and methods such as:<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">AddDays()</code>�,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">ToString()</code>,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">WeekOfYear</code>,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">DaysInMonth()</code>,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">IsLeapYear()</code>,<code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">GetMonthName()</code>,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">StaringDateOfWeek()</code>, and so on.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The following code demonstrates how to use<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaliCalendar</code>:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain5"><img height="9" width="9" preid="5" src="http://www.codeproject.com/images/minus.gif" id="preimg5" alt="" /><span style="margin-bottom: 0px;" preid="5" id="precollapse5"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre5">
JalaliCalendar jcal = <span style="color: blue;" class="code-keyword">new</span> JalaliCalendar();
DateTime dt = jcal.ToDateTime(<span style="color: navy;" class="code-digit">1385</span>, <span style="color: navy;" class="code-digit">1</span>, <span style="color: navy;" class="code-digit">13</span>, <span style="color: navy;" class="code-digit">18</span>, <span style="color: navy;" class="code-digit">15</span>, <span style="color: navy;" class="code-digit">20</span>, <span style="color: navy;" class="code-digit">0</span>);
Console.WriteLine(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">Gregorian date correspoding &quot;</span> + 
                  <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">to 13/1/1385 18:15:20 == &quot;</span> + dt);
Console.WriteLine(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">The length of the last month in 1387 is == &quot;</span> 
                  + jcal.GetDaysInMonth(<span style="color: navy;" class="code-digit">1387</span>, <span style="color: navy;" class="code-digit">12</span>));
Console.WriteLine(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">So 1387 is a leap year == &quot;</span> + 
                  jcal.IsLeapYear(<span style="color: navy;" class="code-digit">1387</span>));</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Prints:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain6"><img height="9" width="9" preid="6" src="http://www.codeproject.com/images/minus.gif" id="preimg6" alt="" /><span style="margin-bottom: 0px;" preid="6" id="precollapse6"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="text" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre6">
Gregorian date correspoding 
          to 13/1/1385 18:15:20 == 4/2/2006 6:15:20 PM
The length of the last month in 1387 is == 30
So 1387 is a leap year == True</pre>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">The Demo Project</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">I had originally written the program for my Pocket PC. Here I have slightly modified it to become a test program for Persian date conversation for .NET Framework�s<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>and the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaliCalendar</code><span class="Apple-converted-space">&nbsp;</span>introduced here. Its source code demonstrates the usage of my<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianDate</code><span class="Apple-converted-space">&nbsp;</span>structure type.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Points of Interest</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The main purpose for using this<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaiCalendar</code><span class="Apple-converted-space">&nbsp;</span>in place of the .NET Framework�s<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>must be the need of date conversation for historical events. If you just want to display the current date in your website,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>is sufficient. But the .NET Framework has limitations for dates before the common era (dates with a negative year),<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">System.DateTime</code><span class="Apple-converted-space">&nbsp;</span>just supports dates after January 1, 1 AC. To overcome this problem, I made a structure type named<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime</code><span class="Apple-converted-space">&nbsp;</span>that supports dates in both common and before common eras. This type uses Julian days at the heart of the algorithm instead of<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Ticks</code>. Using this method,<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime</code><span class="Apple-converted-space">&nbsp;</span>can deal with any date after January 1, 4712 BC.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The type initializes by some overridden constructors, but all of them calls one of these two methods: one that constructs the type using a calendar date (year, month �) values, and one that makes the type using a Julian day value. The methods ask you to specify if the date you are working on is expressed (or should be expressed) in Gregorian or Julian calendar. Note that the Julian calendar does not relate to a Julian day at all. For common historical uses, you must consider that the days before October 15, 1582 are in Julian calendar. There�s another thing to be mentioned, there is no year zero in history! The year before year 1 is considered to be year -1. This is why when you call the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime.ToString()</code><span class="Apple-converted-space">&nbsp;</span>method, you�ll get something like: 4/18/53 BC, while<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime.Year</code>property returns -52. The algorithms used here take year zero into effect, thus<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime(-<span style="color: navy;" class="code-digit">52</span>,<span style="color: navy;" class="code-digit">4</span>,<span style="color: navy;" class="code-digit">18</span>)</code><span class="Apple-converted-space">&nbsp;</span>is the constructor for 4/18/53 BC. Here are some examples:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain7"><img height="9" width="9" preid="7" src="http://www.codeproject.com/images/minus.gif" id="preimg7" alt="" /><span style="margin-bottom: 0px;" preid="7" id="precollapse7"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre7">
XDateTime xdt = <span style="color: blue;" class="code-keyword">new</span> XDateTime(-400, <span style="color: navy;" class="code-digit">5</span>, <span style="color: navy;" class="code-digit">8</span>, <span style="color: navy;" class="code-digit">2</span>, <span style="color: navy;" class="code-digit">25</span>, <span style="color: navy;" class="code-digit">30</span>, <span style="color: blue;" class="code-keyword">false</span>);
Console.WriteLine(xdt);
Console.WriteLine(xdt.DayOfWeek);
xdt = xdt.AddDays(<span style="color: navy;" class="code-digit">3</span>.<span style="color: navy;" class="code-digit">5</span>);
Console.WriteLine(xdt);
Console.WriteLine(xdt.DayOfWeek);</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Prints:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain8"><img height="9" width="9" preid="8" src="http://www.codeproject.com/images/minus.gif" id="preimg8" alt="" /><span style="margin-bottom: 0px;" preid="8" id="precollapse8"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="text" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre8">
5/8/401 BC 2:25:30
Wednesday
5/11/401 BC 14:25:30
Saturday</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">The last argument of the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XDateTime</code><span class="Apple-converted-space">&nbsp;</span>constructor is set to<span class="Apple-converted-space">&nbsp;</span><code lang="cs" style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;"><span style="color: blue;" class="code-keyword">false</span></code><span class="Apple-converted-space">&nbsp;</span>to indicate that the date is expressed in Julian calendar.</p>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">I have coded an<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">XPersianDate</code><span class="Apple-converted-space">&nbsp;</span>structure in the same manner. This type uses the same algorithm that<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaliCalendar</code><span class="Apple-converted-space">&nbsp;</span>uses, to convert any Jalali date to its corresponding Julian day and Gregorian date. Here is a sample:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain9"><img height="9" width="9" preid="9" src="http://www.codeproject.com/images/minus.gif" id="preimg9" alt="" /><span style="margin-bottom: 0px;" preid="9" id="precollapse9"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre9">
XDateTime xdt = <span style="color: blue;" class="code-keyword">new</span> XDateTime(-400, <span style="color: navy;" class="code-digit">5</span>, <span style="color: navy;" class="code-digit">8</span>, <span style="color: navy;" class="code-digit">2</span>, <span style="color: navy;" class="code-digit">25</span>, <span style="color: navy;" class="code-digit">30</span>, <span style="color: blue;" class="code-keyword">false</span>);
Console.WriteLine(xdt);
XPersianDate xpd = <span style="color: blue;" class="code-keyword">new</span> XPersianDate(xdt.JulianDay);
Console.WriteLine(xpd);
Console.WriteLine(xpd.ToXDateTime(<span style="color: blue;" class="code-keyword">false</span>)); <span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">//</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">false: is Julian</span></pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Prints:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain10"><img height="9" width="9" preid="10" src="http://www.codeproject.com/images/minus.gif" id="preimg10" alt="" /><span style="margin-bottom: 0px;" preid="10" id="precollapse10"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="text" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre10">
5/8/401 BC 2:25:30
13/2/1022 BH 2:25:30
5/8/401 BC 2:25:30</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">I used the term �BH� to express �Before Hijrat�.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Conclusion</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Here, an accurate date conversation method for Jalali and Gregorian calendars was explained. This method is useful for special purposes such as date conversation for historical events. I also provided two structure types to deal with dates in Before Common Era.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Resources</h2>
<ul>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Here is<span class="Apple-converted-space">&nbsp;</span><a style="text-decoration: none;" href="http://www.projectpluto.com/calendar.htm#jalali">Bill J. Gray's algorithm</a>[<a style="text-decoration: none;" target="_blank" href="http://www.projectpluto.com/calendar.htm#jalali">^</a>]. I just ported his C code into C#.</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><a style="text-decoration: none;" href="http://www.willbell.com/math/mc1.htm">Jean Meeus� Astronomical Algorithms</a>[<a style="text-decoration: none;" target="_blank" href="http://www.willbell.com/math/mc1.htm">^</a>]. The first edition of this book has been translated in Persian and published under the title of �فرمول&zwnj;های ستاره&zwnj;شناسی برای محاسب&zwnj;ها�.</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">I believe<span class="Apple-converted-space">&nbsp;</span><a style="text-decoration: none;" href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=844E1A97-0E12-48D7-B2F9-D5884A9B1523">this</a>[<a style="text-decoration: none;" target="_blank" href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=844E1A97-0E12-48D7-B2F9-D5884A9B1523">^</a>] is the origin of the .NET Framework�s<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">PersianCalendar</code><span class="Apple-converted-space">&nbsp;</span>class. This is a nice project written by<span class="Apple-converted-space">&nbsp;</span><i>Omid K. Rad</i>, I have used his idea and his routines in the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">JalaliCalendar</code><span class="Apple-converted-space">&nbsp;</span>class.</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Here is another article titled<span class="Apple-converted-space">&nbsp;</span><a style="text-decoration: none;" href="http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm">The Persian Calendar for 3000 years</a>[<a style="text-decoration: none;" target="_blank" href="http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm">^</a>] that explains a similar method to Gray�s algorithm.</li>
</ul>
</span></span>]]></description>
<pubDate><![CDATA[Wed, 08 Jul 2009 11:13:29 GMT]]></pubDate>
</item>
<item>
<title><![CDATA[تبديل تاريخ ميلادي به شمسي نمايش آن در Taskbar]]></title>
<link><![CDATA[http://www.taymaz.ir/download_%D8%AA%D8%A8%D8%AF%D9%8A%D9%84+%D8%AA%D8%A7%D8%B1%D9%8A%D8%AE+%D9%85%D9%8A%D9%84%D8%A7%D8%AF%D9%8A+%D8%A8%D9%87+%D8%B4%D9%85%D8%B3%D9%8A+%D9%86%D9%85%D8%A7%D9%8A%D8%B4+%D8%A2%D9%86+%D8%AF%D8%B1+Taskbar.html]]></link>
<guid isPermaLink="true"><![CDATA[http://www.taymaz.ir/download_%D8%AA%D8%A8%D8%AF%D9%8A%D9%84+%D8%AA%D8%A7%D8%B1%D9%8A%D8%AE+%D9%85%D9%8A%D9%84%D8%A7%D8%AF%D9%8A+%D8%A8%D9%87+%D8%B4%D9%85%D8%B3%D9%8A+%D9%86%D9%85%D8%A7%D9%8A%D8%B4+%D8%A2%D9%86+%D8%AF%D8%B1+Taskbar.html]]></guid>
<description><![CDATA[<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" class="Apple-style-span"><span style="font-family: Verdana; font-size: 13px;" class="Apple-style-span"><img height="103" width="239" src="TrayIconPersianCalendar/traycalande1.JPG" alt="traycalande1.JPG" /><br />
<br />
<img height="85" width="322" src="TrayIconPersianCalendar/traycalande2.JPG" alt="traycalande2.JPG" />
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Introduction</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">This is my first article that is written with the C# language. In this article, I will teach you how to build a simple application that creates a tray icon and shows a simple menu. Also in this article, you'll learn how to hide the main form of your application. If you are Persian, you can find codes that convert the date of a system to Hejri Shamsi and show the date in a tray icon.</p>
<h2 style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 13pt; color: rgb(255, 153, 0); font-weight: bold;">Using the Code</h2>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">To create a program that shows a tray icon, you should add a notify icon control to your form from the toolbox. Notify icons have some properties:</p>
<img height="298" width="244" src="TrayIconPersianCalendar/traycalande3.JPG" alt="traycalande3.JPG" />
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Some important properties are:</p>
<ol>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Icon</code>: changes the icon that shows in a tray icon</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Visible</code>: sets if the icon is shown or not</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">ContextMenu</code>: sets a menu for your tray icon</li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;"><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Text</code>: sets the tool tip of a tray icon</li>
</ol>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">I set all of these properties in a constructor of form:</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain0"><img height="9" width="9" preid="0" src="http://www.codeproject.com/images/minus.gif" id="preimg0" alt="" /><span style="margin-bottom: 0px;" preid="0" id="precollapse0"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre0"><span style="color: blue;" class="code-keyword">public</span> Form1()
        {
            InitializeComponent();
            <span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment">/*</span><span style="color: rgb(0, 128, 0); font-style: italic;" class="code-comment"> some code there<br />                    */</span>

            <span style="color: blue;" class="code-keyword">string</span> s = strDay + <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string"> &quot;</span> + Convert.ToString(dd) + <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string"> &quot;</span> + 
                strMonth + <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string"> &quot;</span> + Convert.ToString(yy);
            s += <span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">\n&quot;</span> + d1.ToLongDateString();
            notifyIcon1.Text = s;

            m_menu = <span style="color: blue;" class="code-keyword">new</span> ContextMenu();
            m_menu.MenuItems.Add(<span style="color: navy;" class="code-digit">0</span>,
                <span style="color: blue;" class="code-keyword">new</span> MenuItem(<span style="color: purple;" class="code-string">&quot;</span><span style="color: purple;" class="code-string">Exit&quot;</span>, <span style="color: blue;" class="code-keyword">new</span> System.EventHandler(Exit_Click)));
            notifyIcon1.ContextMenu = m_menu;
            notifyIcon1.Visible = <span style="color: blue;" class="code-keyword">true</span>;
        }</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">In this project, I want a to hide my main form. For this, I did the following:</p>
<ol>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Set the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">ormWindowState</code><span class="Apple-converted-space">&nbsp;</span>property of the form to<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Minimized</code></li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Set the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Opacity</code><span class="Apple-converted-space">&nbsp;</span>property of the form to<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;"><span style="color: navy;" class="code-digit">0</span></code></li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Set the<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">ShowInTaskbar</code><span class="Apple-converted-space">&nbsp;</span>property to<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;"><span style="color: blue;" class="code-keyword">false</span></code></li>
    <li style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">Add an event handler for<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">VisibleChanged</code><span class="Apple-converted-space">&nbsp;</span>and add the below code:</li>
</ol>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain1"><img height="9" width="9" preid="1" src="http://www.codeproject.com/images/minus.gif" id="preimg1" alt="" /><span style="margin-bottom: 0px;" preid="1" id="precollapse1"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre1">
      <span style="color: blue;" class="code-keyword">private</span> <span style="color: blue;" class="code-keyword">void</span> Form1_VisibleChanged(<span style="color: blue;" class="code-keyword">object</span> sender, EventArgs e)
        {
            <span style="color: blue;" class="code-keyword">this</span>.Visible = <span style="color: blue;" class="code-keyword">false</span>;
        }</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">So, this program just shows a tray icon and the main form is not displayed. Therefore, for exiting from the program, I create a menu for the tray icon with an exit item. When the user clicks on the exit item, the program ends.</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain2"><img height="9" width="9" preid="2" src="http://www.codeproject.com/images/minus.gif" id="preimg2" alt="" /><span style="margin-bottom: 0px;" preid="2" id="precollapse2"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre2">
      <span style="color: blue;" class="code-keyword">protected</span> <span style="color: blue;" class="code-keyword">void</span> Exit_Click(<span style="color: rgb(51, 153, 153);" class="code-SDKkeyword">Object</span> sender, System.EventArgs e)
        {
            Close();
        }     </pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">My program changes the tray icon related the date in Hejri Shamsi. For doing this, I created 31 icons and added them to the resource of my project. I created an array of form icons.</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain3"><img height="9" width="9" preid="3" src="http://www.codeproject.com/images/minus.gif" id="preimg3" alt="" /><span style="margin-bottom: 0px;" preid="3" id="precollapse3"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre3">
            Icon[] Icon_ar = <span style="color: blue;" class="code-keyword">new</span> Icon[<span style="color: navy;" class="code-digit">32</span>];
            Icon_ar[<span style="color: navy;" class="code-digit">1</span>] = global::mytray.Properties.Resources.icon1;
            Icon_ar[<span style="color: navy;" class="code-digit">2</span>] = global::mytray.Properties.Resources.icon2;
            Icon_ar[<span style="color: navy;" class="code-digit">3</span>] = global::mytray.Properties.Resources.icon3;
            Icon_ar[<span style="color: navy;" class="code-digit">4</span>] = global::mytray.Properties.Resources.icon4;
            Icon_ar[<span style="color: navy;" class="code-digit">5</span>] = global::mytray.Properties.Resources.icon5;
            Icon_ar[<span style="color: navy;" class="code-digit">6</span>] = global::mytray.Properties.Resources.icon6;
            Icon_ar[<span style="color: navy;" class="code-digit">7</span>] = global::mytray.Properties.Resources.icon7;
            Icon_ar[<span style="color: navy;" class="code-digit">8</span>] = global::mytray.Properties.Resources.icon8;
            Icon_ar[<span style="color: navy;" class="code-digit">9</span>] = global::mytray.Properties.Resources.icon9;
            Icon_ar[<span style="color: navy;" class="code-digit">10</span>] = global::mytray.Properties.Resources.icon10;
            Icon_ar[<span style="color: navy;" class="code-digit">11</span>] = global::mytray.Properties.Resources.icon11;
            Icon_ar[<span style="color: navy;" class="code-digit">12</span>] = global::mytray.Properties.Resources.icon12;
            Icon_ar[<span style="color: navy;" class="code-digit">13</span>] = global::mytray.Properties.Resources.icon13;
            Icon_ar[<span style="color: navy;" class="code-digit">14</span>] = global::mytray.Properties.Resources.icon14;
            Icon_ar[<span style="color: navy;" class="code-digit">15</span>] = global::mytray.Properties.Resources.icon15;
            Icon_ar[<span style="color: navy;" class="code-digit">16</span>] = global::mytray.Properties.Resources.icon16;
            Icon_ar[<span style="color: navy;" class="code-digit">17</span>] = global::mytray.Properties.Resources.icon17;
            Icon_ar[<span style="color: navy;" class="code-digit">18</span>] = global::mytray.Properties.Resources.icon18;
            Icon_ar[<span style="color: navy;" class="code-digit">19</span>] = global::mytray.Properties.Resources.icon19;
            Icon_ar[<span style="color: navy;" class="code-digit">20</span>] = global::mytray.Properties.Resources.icon20;
            Icon_ar[<span style="color: navy;" class="code-digit">21</span>] = global::mytray.Properties.Resources.icon21;
            Icon_ar[<span style="color: navy;" class="code-digit">22</span>] = global::mytray.Properties.Resources.icon22;
            Icon_ar[<span style="color: navy;" class="code-digit">23</span>] = global::mytray.Properties.Resources.icon23;
            Icon_ar[<span style="color: navy;" class="code-digit">24</span>] = global::mytray.Properties.Resources.icon24;
            Icon_ar[<span style="color: navy;" class="code-digit">25</span>] = global::mytray.Properties.Resources.icon25;
            Icon_ar[<span style="color: navy;" class="code-digit">26</span>] = global::mytray.Properties.Resources.icon26;
            Icon_ar[<span style="color: navy;" class="code-digit">27</span>] = global::mytray.Properties.Resources.icon27;
            Icon_ar[<span style="color: navy;" class="code-digit">28</span>] = global::mytray.Properties.Resources.icon28;
            Icon_ar[<span style="color: navy;" class="code-digit">29</span>] = global::mytray.Properties.Resources.icon29;
            Icon_ar[<span style="color: navy;" class="code-digit">30</span>] = global::mytray.Properties.Resources.icon30;
            Icon_ar[<span style="color: navy;" class="code-digit">31</span>] = global::mytray.Properties.Resources.icon31;</pre>
<p style="font-family: Verdana,Helvetica,Arial,sans-serif; font-size: 10pt; color: black;">In the above code, I retrieve<span class="Apple-converted-space">&nbsp;</span><code style="color: rgb(153, 0, 0); font-family: 'Courier New',Courier,mono; font-size: 11pt;">Icon</code>s from the resource of the assembly. Retrieving has the advantage that, at run time, your EXE files have icons inside and don't need external files.</p>
<div style="font-size: 8pt; width: 1081px; cursor: pointer;" class="SmallText" id="premain4"><img height="9" width="9" preid="4" src="http://www.codeproject.com/images/minus.gif" id="preimg4" alt="" /><span style="margin-bottom: 0px;" preid="4" id="precollapse4"><span class="Apple-converted-space">&nbsp;</span>Collapse</span></div>
<pre lang="cs" style="padding: 7pt; overflow: auto ! important; background-color: rgb(251, 237, 187); font-family: 'Courier New',Courier,mono; font-style: normal; font-variant: normal; font-weight: normal; font-size: 9pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; white-space: pre; margin-top: 0px;" id="pre4">
notifyIcon1.Icon = Icon_ar[dd];</pre>
</span></span>]]></description>
<pubDate><![CDATA[Wed, 08 Jul 2009 07:34:47 GMT]]></pubDate>
</item>
</channel>
</rss>
