透明桌面日历的制作

news/2025/3/4 17:03:51/

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


http://www.ppmy.cn/news/570941.html

相关文章

MySQL全表扫描一定存在性能问题吗?

查找表中的一行数据,数据库有几种实现方法? 答案是两种,全表扫描或者索引查找。 全表扫描就是通过读取整个表中的数据获取查询结果,这种方式最大的问题就是随着数据量的增长,扫描磁盘数据的性能急剧下降。对于 MySQL…

Spring Cloud - Nacos 注册发现、分级模型、配置集群、环境隔离、原理

目录 一、Nacos 安装和配置 二、Nacos 服务注册发现 2.1、将服务注册到 nacos 中 2.2、执行效果 三、Nacos 的服务分级模型及配置 3.1、分级模型 3.2、配置集群 3.3、配置 Nacos 负载均衡策略 3.4、Nacos 服务实例的权重设置 3.5、环境隔离——namespace 四、Nacos注…

JavaScript的基础语法

JavaScript的基础语法 🔎编写第一个HelloWorld行内式内嵌式外部式 🔎注释🔎consoloe.log()利用consoloe.log()打印HelloWorld 🔎基础语法变量变量的使用动态类型 🔎基础数据类型🔎运算符算术运算符复合赋值…

甘特图神器大比拼——国内外7款经典工具评测

甘特图是一种重要的项目管理工具,它能够帮助我们规划、安排和跟踪项目的进度和任务。然而,市面上的甘特图工具众多,选择恰当的工具并不容易。在本文中,我们将为您介绍国内外7款经典的甘特图神器,并进行详细评测和比较。…

乔布斯-遗失的访谈中英双文版-尘封十余年的伟大遇见!

史蒂夫乔布斯遗失的访谈 视频可以访问网易公开课:http://v.163.com/movie/2013/5/N/R/M8TBJIK7D_M8TBLIINR.html 英文版:(中文版在下方) I’m Bob Cringley, 16 years ago when Iwas making my television series Triumph of t…

五人制棒球的发展·棒球1号位

五人制棒球是一种小型棒球运动,它与传统的九人制棒球相比,球场更小、人数更少,但比赛节奏更快、更刺激。在近年来,五人制棒球在全球范围内逐渐受到了越来越多的关注和喜爱,因此对其发展进行课题研究具有重要意义。 一、…

基于工业智能网关的设备运维管理平台有何功能?

工业物联网平台作为监控工业设备和工业环境的智能应用,整合边缘和云端的数据优势,在制造业领域得到越来越丰富的应用。 在工业制造生产过程中,常常分为人、机、料、法、环等五大要素,其中机器设备的安全稳定运行时保证工厂生产效…

BurpSuite安装教程以及环境配置(附安装包)

前言 Burp Suite 是用于攻击web 应用程序的集成平台。它包含了许多Burp工具,这些不同的burp工具通过协同工作,有效的分享信息,支持以某种工具中的信息为基础供另一种工具使用的方式发起攻击。 它主要用来做安全性渗透测试,可以实…