{"id":72,"date":"2011-04-19T16:10:30","date_gmt":"2011-04-19T06:10:30","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=72"},"modified":"2011-04-19T16:10:30","modified_gmt":"2011-04-19T06:10:30","slug":"desktop-toy-snow","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=72","title":{"rendered":"Desktop toy &#8211; Snow"},"content":{"rendered":"<p>This article was posted on last Christmas. Now I move this to my new home.<\/p>\n<p>It&#8217;s very hard to see snow here, but we can create a desktop toy to simulate snowing on your screen. If you run this desktop toy, you will see snow falling from top of your screen. The compiled version can be downloaded <a href=\"http:\/\/www.nickdu.com\/tools\/snow.zip\" target=\"_blank\">here<\/a><\/p>\n<ul>\n<li>Simply create a WinForm application in Visual Studio 2008,and change the Form properties as following picture shows. If you don&#8217;t have Visual Studio 2008, you also can download <a href=\"http:\/\/www.microsoft.com\/express\/Downloads\/#2008-Visual-CS\" target=\"_blank\">Visual Studio Express <\/a>for free<\/li>\n<\/ul>\n<p style=\"padding-left: 60px;\"><a href=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2011\/04\/winformproperties.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-73\" title=\"winformproperties\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2011\/04\/winformproperties-145x300.jpg\" alt=\"\" width=\"145\" height=\"300\" \/><\/a><\/p>\n<ul>\n<li>Dump the following code and run it.<\/li>\n<\/ul>\n<pre lang=\"csharp\">using System;\nusing System.Collections.Generic;\nusing System.Drawing;\nusing System.Drawing.Drawing2D;\nusing System.Drawing.Imaging;\nusing System.Runtime.InteropServices;\nusing System.Windows.Forms;\n\nnamespace Snow\n{\n    public partial class Form1 : Form\n    {\n        private Bitmap m_Snow;\n        private static readonly Random rand = new Random();\n        private readonly List SnowFlakes = new List();\n        private int Tick = 0;\n        private class SnowFlake\n        {\n            public float Rotation;\n            public float RotVelocity;\n            public float Scale;\n            public float X;\n            public float XVelocity;\n            public float Y;\n            public float YVelocity;\n        }\n\n        Image screenImage;\n\n        private void Form1_Load(object sender, EventArgs e)\n        {\n            \/\/\/\/\n            screenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);\n        }\n\n        private void SetBackground(Image img)\n        {\n            try\n            {\n                Bitmap bitmap = (Bitmap)img;\n                if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)\n                {\n                    throw new ApplicationException();\n                }\n                IntPtr hObject = IntPtr.Zero;\n                IntPtr zero = IntPtr.Zero;\n                IntPtr hDC = Win32.GetDC(IntPtr.Zero);\n                IntPtr ptr2 = Win32.CreateCompatibleDC(hDC);\n                try\n                {\n                    hObject = bitmap.GetHbitmap(Color.FromArgb(0));\n                    zero = Win32.SelectObject(ptr2, hObject);\n                    Win32.Size size2 = new Win32.Size(bitmap.Width, bitmap.Height);\n                    Win32.Size psize = size2;\n                    Win32.Point point3 = new Win32.Point(0, 0);\n                    Win32.Point pprSrc = point3;\n                    point3 = new Win32.Point(base.Left, base.Top);\n                    Win32.Point pptDst = point3;\n                    Win32.BLENDFUNCTION pblend = new Win32.BLENDFUNCTION();\n                    pblend.BlendOp = 0;\n                    pblend.BlendFlags = 0;\n                    pblend.SourceConstantAlpha = 0xff;\n                    pblend.AlphaFormat = 1;\n                    Win32.UpdateLayeredWindow(this.Handle, hDC, ref pptDst, ref psize, ptr2, ref pprSrc, 0, ref pblend, 2);\n                }\n                catch (Exception exception1)\n                {\n                    Exception exception = exception1;\n                    throw exception;\n                }\n                finally\n                {\n                    Win32.ReleaseDC(IntPtr.Zero, hDC);\n                    if (hObject != IntPtr.Zero)\n                    {\n                        Win32.SelectObject(ptr2, zero);\n                        Win32.DeleteObject(hObject);\n                    }\n                    Win32.DeleteDC(ptr2);\n                }\n            }\n            catch (Exception e)\n            {\n                MessageBox.Show(e.Message);\n            }\n        }\n        protected override System.Windows.Forms.CreateParams CreateParams\n        {\n            get\n            {\n                System.Windows.Forms.CreateParams createParams = base.CreateParams;\n                createParams.ExStyle |= 0x80000;\n                return createParams;\n            }\n        }\n        private Bitmap Snow\n        {\n            get\n            {\n                if (m_Snow == null)\n                {\n                    m_Snow = new Bitmap(32, 32);\n                    using (Graphics g = Graphics.FromImage(m_Snow))\n                    {\n                        g.SmoothingMode = SmoothingMode.AntiAlias;\n                        g.Clear(Color.Transparent);\n                        g.TranslateTransform(16, 16, MatrixOrder.Append);\n                        Color black = Color.FromArgb(1, 1, 1);\n                        Color white = Color.FromArgb(255, 255, 255);\n                        DrawSnow(g, new SolidBrush(black), new Pen(black, 3f));\n                        DrawSnow(g, new SolidBrush(white), new Pen(white, 2f));\n                        g.Save();\n                    }\n                }\n                return m_Snow;\n            }\n        }\n        public Form1()\n        {\n            InitializeComponent();\n            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);\n        }\n        private void OnTick(object sender, EventArgs args)\n        {\n            Tick++;\n            \/\/new snow flake\n            if (Tick % 5 == 0 &amp;&amp; rand.NextDouble() &lt; 0.30)\n            {\n                SnowFlake s = new SnowFlake();\n                s.X = rand.Next(-20, this.Width + 20);\n                s.Y = 0f;\n                s.XVelocity = (float)(rand.NextDouble() - 0.5f) * 2f;\n                s.YVelocity = (float)(rand.NextDouble() * 3) + 1f;\n                s.Rotation = rand.Next(0, 359);\n                s.RotVelocity = rand.Next(-3, 3) * 2;\n                if (s.RotVelocity == 0)\n                {\n                    s.RotVelocity = 3;\n                }\n                s.Scale = (float)(rand.NextDouble() \/ 2) + 0.75f;\n                SnowFlakes.Add(s);\n            }\n             \/\/To draw snowflake\n            Graphics g = Graphics.FromImage(screenImage);\n            g.Clear(Color.Transparent);\n            g.SmoothingMode = SmoothingMode.HighSpeed;\n\n            for (int i = 0; i &lt; SnowFlakes.Count; i++)             {                 SnowFlake s = SnowFlakes[i];                 s.X += s.XVelocity;                 s.Y += s.YVelocity;                 s.Rotation += s.RotVelocity;                 s.XVelocity += ((float)rand.NextDouble() - 0.5f) * 0.7f;                 s.XVelocity = Math.Max(s.XVelocity, -2f);                 s.XVelocity = Math.Min(s.XVelocity, +2f);                 if (s.Y &gt; this.Height)\n                {\n                    SnowFlakes.RemoveAt(i);\n                }\n                else\n                {\n                    g.ResetTransform();\n                    g.TranslateTransform(-16, -16, MatrixOrder.Append); \/\/pan\n                    g.ScaleTransform(s.Scale, s.Scale, MatrixOrder.Append); \/\/scale\n                    g.RotateTransform(s.Rotation, MatrixOrder.Append); \/\/rotate\n                    g.TranslateTransform(s.X, s.Y, MatrixOrder.Append); \/\/pan\n                    g.DrawImage(Snow, 0, 0); \/\/draw\n                    \/\/\/\/g.Dispose();\n                }\n            }\n            g.Dispose();\n            SetBackground(screenImage);\n        }\n\n        private static void DrawSnow(Graphics g, Brush b, Pen p)\n        {\n            const int a = 6;\n            const int a2 = a + 2;\n            const int r = 2;\n            g.DrawLine(p, -a, -a, +a, +a);\n            g.DrawLine(p, -a, +a, +a, -a);\n            g.DrawLine(p, -a2, 0, +a2, 0);\n            g.DrawLine(p, 0, -a2, 0, +a2);\n            g.FillEllipse(b, -r, -r, r * 2, r * 2);\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article was posted on last Christmas. Now I move this to my new home. It&#8217;s very hard to see snow here, but we can create a desktop toy to simulate snowing on your screen. If you run this desktop toy, you will see snow falling from top of your screen. The compiled version can &hellip; <a href=\"https:\/\/nickdu.com\/?p=72\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Desktop toy &#8211; Snow&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=72"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}