实例介绍
【实例简介】桌面GUI时钟
【实例截图】
【核心代码】
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 System.IO; using Microsoft.Win32; namespace clock { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Bitmap b = new Bitmap(global::clock.Properties.Resources.bkg2); Bitmap h = new Bitmap(global::clock.Properties.Resources.met20002); Bitmap m = new Bitmap(global::clock.Properties.Resources.met20022); Bitmap s = new Bitmap(global::clock.Properties.Resources.met20042); string file; public static Bitmap Rotate(Bitmap b, int angle)//图片旋转 { angle =360 - angle % 360; double radian = angle * Math.PI / 180.0; double cos = Math.Cos(radian); double sin = Math.Sin(radian); Bitmap dsImage = new Bitmap(b.Width, b.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Rectangle rect = new Rectangle(0, 0, b.Width, b.Height); Point center = new Point(rect.X rect.Width / 2, rect.Y rect.Height / 2); g.TranslateTransform(center.X, center.Y); g.RotateTransform(360 - angle); g.TranslateTransform(-center.X, -center.Y); g.DrawImage(b, rect); g.ResetTransform(); g.Dispose(); return dsImage; } #region 窗体移动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; #endregion #region 调用UpdateLayeredWindow函数 protected override CreateParams CreateParams { get { const int WS_MINIMIZEBOX = 0x00020000; CreateParams cp = base.CreateParams; cp.Style = cp.Style | WS_MINIMIZEBOX; cp.ExStyle |= 0x00080000; return cp; } } public void SetBits(Bitmap bitmap) { if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))throw new ApplicationException("图片必须是32位带Alhpa通道的图片。"); IntPtr oldBits = IntPtr.Zero; IntPtr screenDC = Win32.GetDC(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; IntPtr memDc = Win32.CreateCompatibleDC(screenDC); try { Win32.Point topLoc = new Win32.Point(Left, Top); Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height); Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION(); Win32.Point srcLoc = new Win32.Point(0, 0); hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBits = Win32.SelectObject(memDc, hBitmap); blendFunc.BlendOp = Win32.AC_SRC_OVER; blendFunc.SourceConstantAlpha = 255; blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA; blendFunc.BlendFlags = 0; Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA); } finally { if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBits); Win32.DeleteObject(hBitmap); } Win32.ReleaseDC(IntPtr.Zero, screenDC); Win32.DeleteDC(memDc); } } #endregion private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) return; ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE HTCAPTION, 0);//窗体移动 SystemConfig.WriteConfigData("XY", this.Location.X.ToString() "," this.Location.Y.ToString()); } private void timer1_Tick(object sender, EventArgs e) { Bitmap bmp = new Bitmap(b); Graphics g = Graphics.FromImage(bmp); g.DrawImage(Rotate(h, DateTime.Now.Hour % 12 * 30 DateTime.Now.Minute / 2),27,25);//时 g.DrawImage(Rotate(m, DateTime.Now.Minute * 6),27,25);//分 g.DrawImage(Rotate(s, DateTime.Now.Second * 6),27,25);//秒针 SetBits(bmp); g.Dispose(); bmp.Dispose(); GC.Collect(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e) { Green(); SystemConfig.WriteConfigData("Color", "Green"); } public void Green() { b = new Bitmap(global::clock.Properties.Resources.bkg); h = new Bitmap(global::clock.Properties.Resources.met2000); m = new Bitmap(global::clock.Properties.Resources.met2002); s = new Bitmap(global::clock.Properties.Resources.met2004); 蓝色ToolStripMenuItem.Checked = false; 金属ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; } private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e) { Blue(); SystemConfig.WriteConfigData("Color", "Blue"); } public void Blue() { b = new Bitmap(global::clock.Properties.Resources.bkg1); h = new Bitmap(global::clock.Properties.Resources.met20001); m = new Bitmap(global::clock.Properties.Resources.met20021); s = new Bitmap(global::clock.Properties.Resources.met20041); 绿色ToolStripMenuItem.Checked = false; 金属ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; } private void 金属ToolStripMenuItem_Click(object sender, EventArgs e) { Gray(); SystemConfig.WriteConfigData("Color", "Gray"); } public void Gray() { b = new Bitmap(global::clock.Properties.Resources.bkg2); h = new Bitmap(global::clock.Properties.Resources.met20002); m = new Bitmap(global::clock.Properties.Resources.met20022); s = new Bitmap(global::clock.Properties.Resources.met20042); 绿色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 黄色ToolStripMenuItem.Checked = false; } private void 黄色ToolStripMenuItem_Click(object sender, EventArgs e) { Yellow(); SystemConfig.WriteConfigData("Color", "Yellow"); } public void Yellow() { b = new Bitmap(global::clock.Properties.Resources.bkg3); h = new Bitmap(global::clock.Properties.Resources.met20003); m = new Bitmap(global::clock.Properties.Resources.met20023); s = new Bitmap(global::clock.Properties.Resources.met20043); 绿色ToolStripMenuItem.Checked = false; 蓝色ToolStripMenuItem.Checked = false; 金属ToolStripMenuItem.Checked = false; } private void Form1_Load(object sender, EventArgs e) { string[] xy = SystemConfig.GetConfigData("XY", string.Empty).Split(','); file = Application.ExecutablePath; if (File.Exists(@"D:\SystemConfig.xml") == true) this.Location = new Point(int.Parse(xy[0]), int.Parse(xy[1])); string a = SystemConfig.GetConfigData("Boot", string.Empty); if (a == "true") 开机启动ToolStripMenuItem.Checked = true; else 开机启动ToolStripMenuItem.Checked = false; string b = SystemConfig.GetConfigData("Mouse", string.Empty); if (b == "true"){鼠标穿透ToolStripMenuItem.Checked = true;CanPenetrate();} else 鼠标穿透ToolStripMenuItem.Checked = false; string c = SystemConfig.GetConfigData("TopMost", string.Empty); if (c == "true"){this.TopMost = true;置于顶层ToolStripMenuItem.Checked = true;} else{this.TopMost = false;置于顶层ToolStripMenuItem.Checked = false;} string d = SystemConfig.GetConfigData("Color", string.Empty); if (d == "Green"){Green();绿色ToolStripMenuItem.Checked = true;} else if (d == "Blue"){Blue();蓝色ToolStripMenuItem.Checked = true;} else if (d == "Gray"){Gray();金属ToolStripMenuItem.Checked = true;} else{Yellow();黄色ToolStripMenuItem.Checked = true;} string ee = SystemConfig.GetConfigData("YIN", string.Empty); if (ee == "true") { notifyIcon1.Visible = false; 隐藏图标ToolStripMenuItem.Checked = true; } } private void 开机启动ToolStripMenuItem_Click(object sender, EventArgs e) { if (开机启动ToolStripMenuItem.Checked == true) { try { RegistryKey key1 = Registry.LocalMachine; RegistryKey key2 = key1.CreateSubKey("software"); RegistryKey key3 = key2.CreateSubKey("microsoft"); RegistryKey key4 = key3.CreateSubKey("windows"); RegistryKey key5 = key4.CreateSubKey("currentversion"); RegistryKey key6 = key5.CreateSubKey("run"); key6.SetValue(file, file); SystemConfig.WriteConfigData("Boot", "true"); MessageBox.Show("设置成功"); } catch { MessageBox.Show("设置失败"); } } else { try { RegistryKey key1 = Registry.LocalMachine; RegistryKey key2 = key1.CreateSubKey("software"); RegistryKey key3 = key2.CreateSubKey("microsoft"); RegistryKey key4 = key3.CreateSubKey("windows"); RegistryKey key5 = key4.CreateSubKey("currentversion"); RegistryKey key6 = key5.CreateSubKey("run"); key6.DeleteValue(file, false);//撤消开机启动 SystemConfig.WriteConfigData("Boot", "false"); MessageBox.Show("取消成功"); } catch { MessageBox.Show("取消失败"); } } } private void 鼠标穿透ToolStripMenuItem_Click(object sender, EventArgs e) { if (鼠标穿透ToolStripMenuItem.Checked == true){CanPenetrate();SystemConfig.WriteConfigData("Mouse", "true");} else{this.FormBorderStyle = FormBorderStyle.None; SystemConfig.WriteConfigData("Mouse", "false");} } public void CanPenetrate() { uint intExTemp = Win32.GetWindowLong(this.Handle, Win32.GWL_EXSTYLE); uint oldGWLEx = Win32.SetWindowLong(this.Handle, Win32.GWL_EXSTYLE, Win32.WS_EX_TRANSPARENT | Win32.WS_EX_LAYERED); } private void 置于顶层ToolStripMenuItem_Click(object sender, EventArgs e) { if (置于顶层ToolStripMenuItem.Checked == true){this.TopMost = true;SystemConfig.WriteConfigData("TopMost", "true"); } else { this.TopMost = false;SystemConfig.WriteConfigData("TopMost", "false");} } private void 隐藏图标ToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("是否隐藏托盘?", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes) { notifyIcon1.Visible = false; SystemConfig.WriteConfigData("YIN", "true"); } else 隐藏图标ToolStripMenuItem.Checked = false; } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)