实例介绍
【实例简介】
【实例截图】









【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
namespace MyDesktop
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
#region 实例化公共类对象及定义公共变量
DateConvert dateconvert = new DateConvert();
DataOperate dataoperate = new DataOperate();
WinSetManage winsetmanage = new WinSetManage();
DataSet myds;
private const uint WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private const int GWL_STYLE = (-16);
private const int GWL_EXSTYLE = (-20);
private static int intX = 0;
private static int intY = 0;
static string strName = Application.ExecutablePath;//获得可执行文件路径
string strnewName = strName.Substring(strName.LastIndexOf("\\") 1);//要写入注册表的键值名称
//获取注册表中的启动位置
RegistryKey RKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
#endregion
#region 在窗口结构中为指定的窗口设置信息
/// <summary>
/// 在窗口结构中为指定的窗口设置信息
/// </summary>
/// <param name="hwnd">欲为其取得信息的窗口的句柄</param>
/// <param name="nIndex">欲取回的信息</param>
/// <param name="dwNewLong">由nIndex指定的窗口信息的新值</param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(
IntPtr hwnd,
int nIndex,
uint dwNewLong
);
#endregion
#region 从指定窗口的结构中取得信息
/// <summary>
/// 从指定窗口的结构中取得信息
/// </summary>
/// <param name="hwnd">欲为其获取信息的窗口的句柄</param>
/// <param name="nIndex">欲取回的信息</param>
/// <returns></returns>
[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(
IntPtr hwnd,
int nIndex
);
#endregion
#region 使窗口有鼠标穿透功能
/// <summary>
/// 使窗口有鼠标穿透功能
/// </summary>
private void CanPenetrate()
{
uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
}
#endregion
//窗体加载事件
public void frmMain_Load(object sender, EventArgs e)
{
timer1.Start();//启动计时器
myds = dataoperate.getds("select * from tb_WinSet");
if (myds.Tables[0].Rows[0][0].ToString() == "False")
{
普通模式ToolStripMenuItem.Text = "√普通模式";
this.TopMost = false;
}
else
{
总在最前ToolStripMenuItem.Text = "√总在最前";
this.TopMost = true;
}
if (myds.Tables[0].Rows[0][1].ToString() == "True")
允许移动ToolStripMenuItem.Text = "√允许移动";
else
允许移动ToolStripMenuItem.Text = "允许移动";
if (myds.Tables[0].Rows[0][2].ToString() == "True")
{
鼠标穿透ToolStripMenuItem.Text = "√鼠标穿透";
CanPenetrate();
}
else
鼠标穿透ToolStripMenuItem.Text = "鼠标穿透";
this.Opacity = Convert.ToDouble(myds.Tables[0].Rows[0][3].ToString());//窗体显示的透明度
RegistryKey myReg1, myReg2;
myReg1 = Registry.CurrentUser;
try
{
myReg2 = myReg1.CreateSubKey("Software\\MySoft");
//从上次关闭位置启动窗体
this.Location = new Point(Convert.ToInt16(myReg2.GetValue("1")), Convert.ToInt16(myReg2.GetValue("2")));
}
catch { }
if (RKey.GetValue(strnewName) == null)//检测注册表中是否存在指定的键值
开机启动ToolStripMenuItem.Text = "开机启动";
else
开机启动ToolStripMenuItem.Text = "√开机启动";
}
#region 是否允许移动窗体
//按下鼠标时记录鼠标位置
private void monthCalendar1_MouseDown(object sender, MouseEventArgs e)
{
intX = -e.X;
intY = -e.Y;
}
//判断是否允许移动窗体
private void monthCalendar1_MouseMove(object sender, MouseEventArgs e)
{
if (允许移动ToolStripMenuItem.Text == "√允许移动" && e.Button == MouseButtons.Left)
{
Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
myPosittion.Offset(intX, intY);//重载当前鼠标的位置
this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
}
}
#endregion
//显示窗口设置窗体
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
frmWinSet frmwinset = new frmWinSet(this);
frmwinset.Show();
}
#region 万年历显示风格的设置,设置的同时保存到数据库中
private void 允许移动ToolStripMenuItem_Click(object sender, EventArgs e)
{
dateconvert.isSelect(允许移动ToolStripMenuItem);
if (允许移动ToolStripMenuItem.Text == "√允许移动")
winsetmanage.ISMove = true;
else
winsetmanage.ISMove = false;
dataoperate.getcmd("update tb_WinSet set ISMove=" winsetmanage.ISMove "");
}
private void 鼠标穿透ToolStripMenuItem_Click(object sender, EventArgs e)
{
dateconvert.isSelect(鼠标穿透ToolStripMenuItem);
if (鼠标穿透ToolStripMenuItem.Text == "√鼠标穿透")
{
CanPenetrate();
winsetmanage.ISThrough = true;
}
else
winsetmanage.ISThrough = false;
dataoperate.getcmd("update tb_WinSet set ISThrough=" winsetmanage.ISThrough "");
}
private void 普通模式ToolStripMenuItem_Click(object sender, EventArgs e)
{
dateconvert.isSelect(普通模式ToolStripMenuItem);
if (普通模式ToolStripMenuItem.Text == "√普通模式")
{
总在最前ToolStripMenuItem.Text = "总在最前";
this.TopMost = false;
winsetmanage.ShowStyle = false;
}
else
winsetmanage.ShowStyle = true;
dataoperate.getcmd("update tb_WinSet set ShowStyle=" winsetmanage.ShowStyle "");
}
private void 总在最前ToolStripMenuItem_Click(object sender, EventArgs e)
{
dateconvert.isSelect(总在最前ToolStripMenuItem);
if (总在最前ToolStripMenuItem.Text == "√总在最前")
{
this.TopMost = true;
普通模式ToolStripMenuItem.Text = "普通模式";
winsetmanage.ShowStyle = true;
}
else
winsetmanage.ShowStyle = this.TopMost = false;
dataoperate.getcmd("update tb_WinSet set ShowStyle=" winsetmanage.ShowStyle "");
}
#endregion
//显示节日列表窗体
private void 节日ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmHoliday frmholiday = new frmHoliday();
frmholiday.Show();
}
//显示新建提醒窗体
private void 新建提醒ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmOperateRemind frmoperateremind = new frmOperateRemind();
DateConvert.rflag = 0;
frmoperateremind.Show();
}
//显示提醒列表窗体
private void 提醒列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
frmRemind frmremind = new frmRemind();
frmremind.Show();
}
#region 转到今日、上一周、下一周、上一月、下一月、上一年、下一年的实现
private void 今日ToolStripMenuItem_Click(object sender, EventArgs e)
{
monthCalendar1.SelectionStart = DateTime.Now;
}
private void 上一周ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (monthCalendar1.SelectionStart.Day - 7 <= 1)
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddDays(-7);
else
{
if (monthCalendar1.SelectionStart.AddMonths(-1).Month == 0)
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(-1).AddMonths(11).AddDays(DateTime.DaysInMonth(monthCalendar1.SelectionStart.AddYears(-1).AddMonths(11).Year, monthCalendar1.SelectionStart.AddYears(-1).AddMonths(11).Month) - 7);
}
else
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddMonths(-1).AddDays(DateTime.DaysInMonth(monthCalendar1.SelectionStart.AddMonths(-1).Year, monthCalendar1.SelectionStart.AddMonths(-1).Month) - 7);
}
}
}
private void 下一周ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (monthCalendar1.SelectionStart.Day 7 <= DateTime.DaysInMonth(monthCalendar1.SelectionStart.Year, monthCalendar1.SelectionStart.Month))
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddDays(7);
else
{
if (monthCalendar1.SelectionStart.AddMonths(1).Month == 12)
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(1).AddMonths(-11).AddDays(7 - DateTime.DaysInMonth(monthCalendar1.SelectionStart.AddMonths(-11).Year, monthCalendar1.SelectionStart.AddMonths(-11).Month));
}
else
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddMonths(1).AddDays(7 - DateTime.DaysInMonth(monthCalendar1.SelectionStart.Year, monthCalendar1.SelectionStart.Month));
}
}
}
private void 上一月ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (monthCalendar1.SelectionStart.AddMonths(-1).Month == 0)
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(-1).AddMonths(11);
else
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddMonths(-1);
}
private void 下一月ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (monthCalendar1.SelectionStart.AddMonths(1).Month == 12)
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(1).AddMonths(-11);
else
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddMonths(1);
}
private void 上一年ToolStripMenuItem_Click(object sender, EventArgs e)
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(-1);
}
private void 下一年ToolStripMenuItem_Click(object sender, EventArgs e)
{
monthCalendar1.SelectionStart = monthCalendar1.SelectionStart.AddYears(1);
}
#endregion
//退出当前应用程序
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//选择日期时,显示当日详细信息
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
myds = dataoperate.getds("select * from tb_Holiday");
string strInfo = "";//存储当日详细信息
//判断数据库中是否存在记录
if (myds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < myds.Tables[0].Rows.Count; i )
{
string strName = myds.Tables[0].Rows[i][0].ToString();
string strBDate = myds.Tables[0].Rows[i][1].ToString();
string strBYear = myds.Tables[0].Rows[i][2].ToString();
string strNL = myds.Tables[0].Rows[i][3].ToString();
string strEffective = myds.Tables[0].Rows[i][4].ToString();
//将数据库中的大写月份转换为小写
int intMonth = getDate(strBDate.Substring(0, strBDate.IndexOf("月")));
//判断节日是否有效
if (strEffective.ToLower() == "true")
{
if (monthCalendar1.SelectionStart.Year >= Convert.ToInt32(strBYear))
{
//判断公历节日
if (strNL == "")
{
if (strBDate.Length > 3 && strBDate.Length < 7)//几月几日
{
if (monthCalendar1.SelectionStart.Month == intMonth && monthCalendar1.SelectionStart.Day == Convert.ToInt32(strBDate.Substring(strBDate.IndexOf("月") 1, strBDate.IndexOf("日") - strBDate.IndexOf("月") - 1)))
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart) " " strName;
goto line;//使用goto语句执行到指定位置
}
}
else if (strBDate.Length < 4)//几月
{
if (monthCalendar1.SelectionStart.Month == intMonth)
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart) " " strName;
goto line;
}
}
else if (strBDate.Length > 7 && strBDate.Length < 10)//几月的第几周
{
if (monthCalendar1.SelectionStart.Month == intMonth && (Convert.ToInt32(monthCalendar1.SelectionStart.Day / 7)) == getDate(strBDate.Substring(strBDate.IndexOf("第") 1, strBDate.IndexOf("个") - strBDate.IndexOf("第") - 1)))
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart) " " strName;
goto line;
}
}
else if (strBDate.Length > 9 && strBDate.Length < 12)//几月的第几个星期几
{
if (monthCalendar1.SelectionStart.Month == intMonth && (Convert.ToInt32(monthCalendar1.SelectionStart.Day / 7)) == getDate(strBDate.Substring(strBDate.IndexOf("第") 1, strBDate.IndexOf("个") - strBDate.IndexOf("第") - 1)))
{
int intFlag = 0;
if (getDate(strBDate.Substring(strBDate.IndexOf("期") 1)) == 7)
intFlag = 1;
else
intFlag = getDate(strBDate.Substring(strBDate.IndexOf("期") 1)) 1;
if ((monthCalendar1.SelectionStart.Day % 7) == intFlag)
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart) " " strName;
goto line;
}
}
}
else
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart);
goto line;
}
}
//判断农历节日
else
{
string strDate = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart);
int intnewMonth = 0;
if (strBDate.IndexOf("十一") == -1)
intnewMonth = getDate(strDate.Substring(strDate.IndexOf("月") - 1, 1));
else
intnewMonth = getDate(strDate.Substring(strDate.IndexOf("月") - 2, 2));
if (intnewMonth == intMonth)
{
int intDay = 0;
if (strDate.IndexOf("初") != -1)
intDay = getDate(strDate.Substring(strDate.IndexOf("初") 1, 1));
else
intDay = getDate(strDate.Substring(strDate.IndexOf("月") 1, 2));
if (intDay == Convert.ToInt32(strBDate.Substring(strBDate.IndexOf("月") 1, strBDate.IndexOf("日") - strBDate.IndexOf("月") - 1)))
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart) " " strName;
goto line;
}
}
else
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart);
}
}
}
}
}
}
else
{
strInfo = dateconvert.GetLunarCalendar(monthCalendar1.SelectionStart) " " dateconvert.GetConstellationName(monthCalendar1.SelectionStart);
goto line;
}
line:
toolTip.Show(strInfo, monthCalendar1, monthCalendar1.Location, 5000);//显示提示信息
}
//关闭窗体时,将窗体的当前位置写入注册表中,以便下次启动时访问
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
RegistryKey myReg1, myReg2;
myReg1 = Registry.CurrentUser;
//在注册表中创建自定义项
myReg2 = myReg1.CreateSubKey("Software\\MySoft");
try
{
//将窗体当前位置写入注册表
myReg2.SetValue("1", this.Location.X.ToString());
myReg2.SetValue("2", this.Location.Y.ToString());
}
catch { }
}
//在托盘中写入程序图标
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
this.Activate();//激活窗体并给予它焦点
}
//最小化当前窗体
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Hide();
}
//在计时器中判断当前时间有没有到期的提醒
private void timer1_Tick(object sender, EventArgs e)
{
myds = dataoperate.getds("select * from tb_Remind");
for (int i = 0; i < myds.Tables[0].Rows.Count; i )
{
string strName = myds.Tables[0].Rows[i][0].ToString();
string strFrequency = myds.Tables[0].Rows[i][1].ToString();
string strTime = myds.Tables[0].Rows[i][2].ToString();
string strContent = myds.Tables[0].Rows[i][3].ToString();
string[] strTimes = strTime.Split(' ');
//判断提醒方式,并根据提醒方式弹出提醒对话框
if (strFrequency == "仅仅一次")
{
if (DateTime.Now.ToShortDateString() == strTimes[0].ToString() && DateTime.Now.ToLongTimeString() == strTimes[1].ToString())
{
MessageBox.Show(strContent, strName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
int intNum = Convert.ToInt32(strFrequency.Substring(0, 1));
string strUnit = strFrequency.Substring(1, 1);
TimeSpan TSpan = DateTime.Now - Convert.ToDateTime(strTimes[0]);
switch (strUnit)
{
case "天"://每几天提醒一次
if (TSpan.Days % intNum == 0 && DateTime.Now.ToLongTimeString() == strTimes[1].ToString())
MessageBox.Show(strContent, strName, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "周"://每几周提醒一次
if (TSpan.Days % (intNum * 7) == 0 && DateTime.Now.ToLongTimeString() == strTimes[1].ToString())
MessageBox.Show(strContent, strName, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "月"://每几月提醒一次
if (TSpan.Days % (intNum * 30) == 0 && DateTime.Now.ToLongTimeString() == strTimes[1].ToString())
MessageBox.Show(strContent, strName, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "年"://每几年提醒一次
if (TSpan.Days % (intNum * 365) == 0 && DateTime.Now.ToLongTimeString() == strTimes[1].ToString())
MessageBox.Show(strContent, strName, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
}
}
//设置程序开机自启动
private void 开机启动ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!File.Exists(strName))//判断指定的文件是否存在
return;
if (RKey == null)
RKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
if (开机启动ToolStripMenuItem.Text == "开机启动")
{
开机启动ToolStripMenuItem.Text = "√开机启动";
RKey.SetValue(strnewName, strName);//通过修改注册表,使程序在开机时自动运行
MessageBox.Show("程序设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (开机启动ToolStripMenuItem.Text == "√开机启动")
{
开机启动ToolStripMenuItem.Text = "开机启动";
RKey.DeleteValue(strnewName, false);//通过修改注册表,取消程序在开机时自动运行
MessageBox.Show("程序设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#region 大小写数字的转换
/// <summary>
/// 大小写数字的转换
/// </summary>
/// <param name="str">要转换的大写数字</param>
/// <returns>转换后的小写数字</returns>
private int getDate(string str)
{
int intDay = 0;
switch (str)
{
case "一":
case "正":
intDay = 1;
break;
case "二":
intDay = 2;
break;
case "三":
intDay = 3;
break;
case "四":
intDay = 4;
break;
case "五":
intDay = 5;
break;
case "六":
intDay = 6;
break;
case "七":
case "日":
intDay = 7;
break;
case "八":
intDay = 8;
break;
case "九":
intDay = 9;
break;
case "十":
intDay = 10;
break;
case "十一":
intDay = 11;
break;
case "十二":
case "腊":
intDay = 12;
break;
case "十三":
intDay = 13;
break;
case "十四":
intDay = 14;
break;
case "十五":
intDay = 15;
break;
case "十六":
intDay = 16;
break;
case "十七":
intDay = 17;
break;
case "十八":
intDay = 18;
break;
case "十九":
intDay = 19;
break;
case "二十":
intDay = 20;
break;
case "廿一":
intDay = 21;
break;
case "廿二":
intDay = 22;
break;
case "廿三":
intDay = 23;
break;
case "廿四":
intDay = 24;
break;
case "廿五":
intDay = 25;
break;
case "廿六":
intDay = 26;
break;
case "廿七":
intDay = 27;
break;
case "廿八":
intDay = 28;
break;
case "廿九":
intDay = 29;
break;
case "三十":
intDay = 30;
break;
}
return intDay;
}
#endregion
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论