透明桌面日历的制作

原创 2006年05月21日 03:20:00

[愚翁专栏]中有一个 如何用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上显示也是一种选择。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C# WINFORM 制作真正的 透明窗体 桌面画图 的完美解决方案

通过长时间研究终于找到关于在C# WINFORM下制作真正的透明窗体在系统桌面上画图的完美解决方案; 我的解决方案为两层透明窗体重叠,一层透明窗体showForm显示图像(TransparencyKe...

Android手机桌面客户端日历项同步方案

问题 手机是Android系统的,PC机是Windows7系统的,希望能使之同步日历信息、备忘录等。后来找到这个解决方案。除此之外还有别的方案,这只是其中之一。 需要的东西:Google的帐户、M...

桌面日历任务管理

今天利用点时间来写写,有描述不清的地方,还请各位谅解。      最近刚好有些空的时间,于是呢,做了个小桌面日历软件,先首先简单介绍下它的作用:      本程序采用.net平台开发,结合hook、图...

电信3G天翼宽带与SP1冲突导致WIN7桌面AERO半透明效果无法开启

解决了个天翼宽带与WIN7 SP1冲突的问题,记录一下供参考。 我常在笔记本上用电信3G卡上网,原先一直用得挺好的。最近嫌笔记本太慢,换了个SSD固态硬盘。然后用U盘刻了WIN7装上,挺顺利地装完。...
  • huzgd
  • huzgd
  • 2011-12-25 23:36
  • 5249

透明桌面相框

描述介绍:   该软件简单易操作、反应速度快;可以自由设置图片的边框,以达到更好的显示效果;对桌面美化和照片显示来说都是一款很实用的软件。 软件特点: 1、可以在桌面上显示你想要显示的照...

activity主题透明显示系统桌面的问题

转载请标明出处:http://blog.csdn.net/EdisonChang/article/details/50733730前段时间,优化android应用的启动速度时被activity主题是否...

怎样去掉桌面图标阴影(文字背景透明)

如果有一天你的桌面图标 由这样 变成了这样   可以尝试采用如下方法恢复 (适用于winXP、win2003,vista未测试) 1、桌面--右击--属性-外观-效果-在菜单下显示阴影" ...
  • wqjsir
  • wqjsir
  • 2011-10-22 01:00
  • 1093

JAVA模拟实现Windows屏保/实现JFrame窗口透明到桌面

在这个程序的实现中最主要有以下几点: 实现JFrame窗口透明到桌面启用多线程实现屏保重画设置鼠标监听,可以实现鼠标一动就退出屏保 实现了以上两点,代码如下: 窗口类 i...

C# WINFORM 使用鼠标钩子实现透明窗体桌面画图

接上次关于在C# WINFORM下制作透明窗体在系统桌面上画图方案是通过两个透明窗体实现的,现在讨论一下使用API底层鼠标钩子+透明窗体实现方法,这种实现方法比效麻烦,需要重新监听实现鼠标操作,在这儿...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)