透明桌面日历的制作

[愚翁专栏]中有一个 如何用C#做一个类似于桌面插件的程序 的例子,拿来实际演练了一下,发现只是实现一个界面而已,只有没有日历的内容。但是这勾起了我的兴趣,决定自己做一个。

在http://www.codeproject.com/cs/miscctrl/MonthCalendar.asp上有一个MonthCalendar控件,既有源代码又有demo,我发扬了鲁迅先生的拿来主义,统统接收。对于这个控件功能还是比较丰富的,暂时没有深入发掘,只是用其实现了简单的桌面日历。

做的很简单,效果如图:

日历下面是瑞星,瑞星下面是MM。

下面写一下过程吧。

建窗体,就像 “[愚翁专栏]中如何用C#做一个类似于桌面插件”一样,设置属性。

l         设置FormBorderStyleNone

l         设置TopMostfalse

l         设置ShowInTaskbarfalse;

l         为了能穿透桌面,要把BackColor设为White,在把TransparentKey设为White

 

然后就是添加控件MonthCalendar,具体设置就不写了,为了达到透明的效果,设置与form差不多,其他就是美观了。

这样生成的程序在桌面上是不能移动的,为了能实现移动,花了些心思(刚刚学c#不久)。思路是这样的:当鼠标移动进入form的区域时,将光标设置为hand,在mousedown中取得位置,然后用户的操作就是拖动窗体了,一般鼠标是不放开的,所以在mouseup中设置了form的新位置,这就实现了窗体的移动。

中间遇到的问题是,我的控件fill了窗体,当鼠标进入form区域时,控件就捕获了鼠标,使你不能移动窗体。为了解决这个问题,找遍了csdn的帖子,发现有两中方法,一是使用capture,二是使用鼠标钩子。capture自己放的地方不合适,费了一些周折。鼠标钩子兄弟我笨阿,不懂。后来想了办法,变通了一下,在NotifyIcon的关联ContextMenuStrip中加了个按钮,用来标志是否要移动窗体。再后来,突然想到,当鼠标进入窗体时,让上面的控件enabled = false;对日历没有什么影响,实现也方便。再后来,又加入了一个时间(小时,分钟)的lable,为了刷新时间,加了一个timer。这就把用到的东西基本说完了,下面看代码。

 

      1。       // monthCalendar1的属性还是比较重要的,决定了界面
            //
            this.monthCalendar1.ActiveMonth.Month = 5;
            this.monthCalendar1.ActiveMonth.Year = 2006;
            this.monthCalendar1.BorderColor = System.Drawing.Color.Transparent;
            this.monthCalendar1.BorderStyle = System.Windows.Forms.ButtonBorderStyle.None;
            this.monthCalendar1.Culture = new System.Globalization.CultureInfo("zh-CN");
            this.monthCalendar1.Cursor = System.Windows.Forms.Cursors.Hand;
            this.monthCalendar1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.monthCalendar1.Footer.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
            this.monthCalendar1.Footer.Format = Pabo.Calendar.mcTodayFormat.Long;
            this.monthCalendar1.Footer.TextColor = System.Drawing.Color.Red;
            this.monthCalendar1.Header.BackColor = System.Drawing.Color.Transparent;
            this.monthCalendar1.Header.Font = new System.Drawing.Font("华文新魏", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.monthCalendar1.Header.TextColor = System.Drawing.Color.Blue;
            this.monthCalendar1.Header.YearSelectors = true;
            this.monthCalendar1.ImageList = null;
            this.monthCalendar1.Location = new System.Drawing.Point(0, 0);
            this.monthCalendar1.MaxDate = new System.DateTime(2016, 5, 20, 17, 43, 14, 203);
            this.monthCalendar1.MinDate = new System.DateTime(1996, 5, 20, 17, 43, 14, 203);
            this.monthCalendar1.Month.BackgroundImage = null;
            this.monthCalendar1.Month.BorderStyles.Normal = System.Windows.Forms.ButtonBorderStyle.Dotted;
            this.monthCalendar1.Month.BorderStyles.Selected = System.Windows.Forms.ButtonBorderStyle.Outset;
            this.monthCalendar1.Month.Colors.FocusDate = System.Drawing.Color.Black;
            this.monthCalendar1.Month.Colors.FocusText = System.Drawing.Color.Black;
            this.monthCalendar1.Month.Colors.SelectedDate = System.Drawing.Color.Black;
            this.monthCalendar1.Month.Colors.SelectedText = System.Drawing.Color.Black;
            this.monthCalendar1.Month.Colors.WeekendBackground = System.Drawing.Color.Transparent;
            this.monthCalendar1.Month.Colors.WeekendDate = System.Drawing.Color.Red;
            this.monthCalendar1.Month.DateFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            this.monthCalendar1.Month.ShowMonthInDay = true;
            this.monthCalendar1.Month.TextAlign = Pabo.Calendar.mcItemAlign.TopLeft;
            this.monthCalendar1.Month.TextFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            this.monthCalendar1.Name = "monthCalendar1";
            this.monthCalendar1.ShowWeeknumbers = true;
            this.monthCalendar1.Size = new System.Drawing.Size(449, 346);
            this.monthCalendar1.TabIndex = 1;
            this.monthCalendar1.Weekdays.BorderColor = System.Drawing.Color.Transparent;
            this.monthCalendar1.Weekdays.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            this.monthCalendar1.Weekdays.Format = Pabo.Calendar.mcDayFormat.Long;
            this.monthCalendar1.Weekdays.TextColor = System.Drawing.Color.Fuchsia;
            this.monthCalendar1.Weeknumbers.Align = Pabo.Calendar.mcWeeknumberAlign.Center;
            this.monthCalendar1.Weeknumbers.BorderColor = System.Drawing.Color.Transparent;
            this.monthCalendar1.Weeknumbers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
            this.monthCalendar1.Weeknumbers.TextColor = System.Drawing.Color.Lime;
            this.monthCalendar1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.monthCalendar1_MouseMove);
            this.monthCalendar1.MouseEnter += new System.EventHandler(this.monthCalendar1_MouseEnter);
            this.monthCalendar1.HeaderMouseEnter += new System.EventHandler(this.monthCalendar1_HeaderMouseEnter);
            this.monthCalendar1.FooterMouseEnter += new System.EventHandler(this.monthCalendar1_FooterMouseEnter);

2。

 

//记录位置的

        private Rectangle canmoveRect = new Rectangle();
        private bool canMove = false ;
        private int form1Left;
        private int form1Top;
        private int mouseLeft;
        private int mouseTop;

 

 

//点击move
        private void moveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (moveToolStripMenuItem.Checked == true)
            {
                moveToolStripMenuItem.Checked = false;
                this.TopMost = false;
                canMove = false;
                this.monthCalendar1.Enabled = true ;
            }
            else
            {
                moveToolStripMenuItem.Checked = true;
                this.TopMost = true;
                canMove = true;
                canmoveRect = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
                this.monthCalendar1.Enabled = false;
            }
  
        }

 

//总要退出吧

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
           
        }

 

3。

//移动阿

        private void HeliosDeskCalendar_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.Cursor == Cursors.Hand && canMove)
            {
                form1Left = this.Left;
                form1Top = this.Top;
                mouseLeft = this.Left + e.X;
                mouseTop = this.Top + e.Y;
            }

        }

        private void HeliosDeskCalendar_MouseMove(object sender, MouseEventArgs e)
        {
            if (canmoveRect.Contains(e.X, e.Y) && canMove)
            {
                this.Cursor = Cursors.Hand;
            }
        }

        private void HeliosDeskCalendar_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.Cursor == Cursors.Hand && canMove)
            {
                this.Left = form1Left + (this.Left + e.X) - mouseLeft;
                this.Top = form1Top + (this.Top + e.Y) - mouseTop;
            }
        }

4。

//最后加的lable要显示时间,总不能enabled = false吧

 

        private void labelTime_MouseEnter(object sender, EventArgs e)
        {
            if (canMove)
            {
                this.Capture = true;
                this.labelTime.Capture = false;
            }
        }

        private void labelTime_MouseMove(object sender, MouseEventArgs e)
        {
            if (canMove)
            {
                this.Capture = true;
                this.labelTime.Capture = false;
            }
        }

5。借助timer刷新时间

        private void timer1_Tick(object sender, EventArgs e)
        {
         // labelTime.Text = System.DateTime.Now.ToString();
         // labelTime.Text = System.DateTime.Now.ToShortTimeString();
            labelTime.Text = System.DateTime.Now.ToLongTimeString();

        }

6。

 MonthCalendar可以利用的地方很多,可以用来丰富日历功能。如事件就有:

Events

The most useful events are:

Month

  • MonthChanged: Indicates that the active month was changed.
  • DayDragDrop: Indicates that data was dropped on a day, AllowDrop must be true for this event to be raised.
  • ImageClick: Indicates that an image was clicked.
  • DayClick: Indicates that a day was clicked.
  • DayRender: Occurs before a date is updated.
  • DayDoubleClick: Indicates that a day was double clicked.
  • DaySelected: Indicates that one or more days were selected.
  • DayDeselected: Indicates that one or more days were deselected.
  • DayGotFocus: Indicates that a day received focus.
  • DayLostFocus: Indicates that a day lost focus.
  • DayMouseMove: Indicates that the mouse is moved inside a day.

Header

  • HeaderClick: Indicates that the header was clicked.
  • HeaderDoubleClick: Indicates that the header was double clicked.
  • HeaderMouseEnter: Indicates that the mouse entered the header region.
  • HeaderMouseLeave: Indicates that the mouse left the header region.

Footer

  • FooterClick: Indicates that the footer was clicked.
  • FooterDoubleClick: Indicates that the footer was double clicked.
  • FooterMouseEnter: Indicates that the mouse entered the footer region.
  • FooterMouseLeave: Indicates that the mouse left the footer region.

Weekday

  • WeekdayClick: Indicates that a weekday was clicked.
  • WeekdayDoubleClick: Indicates that a weekday was double clicked.
  • WeekdayMouseEnter: Indicates that the mouse entered the weekday region.
  • WeekdayMouseLeave: Indicates that the mouse left the weekday region.

Weeknumber

  • WeeknumberClick: Indicates that a week was clicked.
  • WeeknumberDoubleClick: Indicates that a week was double clicked.
  • WeeknumberMouseEnter: Indicates that the mouse entered the week number region.
  • WeeknumberMouseLeave: Indicates that the mouse left the week number region.

7。含控件的透明窗体移动与鼠标的捕获是重点,日历的功能就要再发掘MonthCalendar控件或者自己改造了,好在有源码。另外,也可以按照[愚翁]先生的方法,直接按照一定的算法在form上显示也是一种选择。

你可能感兴趣的:(Maybe...,日历,microsoft,object,header,timer,events)