实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 透明时钟
{
public partial class Form1 : Form
{
//鼠标拖动相关变量
Point oldPoint = new Point(0, 0);
bool mouseDown = false;
//时钟样式元素,依次为:背景、时针、分针、秒针、转轴、表盖
private Image img1;
private Image img2;
private Image img3;
private Image img4;
private Image img5;
private Image img6;
public Form1()
{
InitializeComponent();
MouseDown = new MouseEventHandler(Form1_MouseDown);
MouseUp = new MouseEventHandler(Form1_MouseUp);
MouseMove = new MouseEventHandler(Form1_MouseMove);
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Left = (e.X - oldPoint.X);
this.Top = (e.Y - oldPoint.Y);
}
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
oldPoint = e.Location;
mouseDown = true;
}
protected override void OnHandleCreated(EventArgs e)
{
InitializeStyles();
base.OnHandleCreated(e);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cParms = base.CreateParams;
cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
return cParms;
}
}
private void InitializeStyles()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
UpdateStyles();
}
public void SetBits()
{
if (BackgroundImage != null)
{
Bitmap bitmap = new Bitmap(BackgroundImage, Width, Height);
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(Width, 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);
}
}
}
public void SetStyle(int Style)
{
switch (Style)
{
case 1:
img1 = new Bitmap(透明时钟.Properties.Resources.system);
img2 = new Bitmap(透明时钟.Properties.Resources.system_h);
img3 = new Bitmap(透明时钟.Properties.Resources.system_m);
img4 = new Bitmap(透明时钟.Properties.Resources.system_s);
img5 = new Bitmap(透明时钟.Properties.Resources.system_dot);
img6 = new Bitmap(透明时钟.Properties.Resources.system_highlights);
break;
case 2:
img1 = new Bitmap(透明时钟.Properties.Resources.trad);
img2 = new Bitmap(透明时钟.Properties.Resources.trad_h);
img3 = new Bitmap(透明时钟.Properties.Resources.trad_m);
img4 = new Bitmap(透明时钟.Properties.Resources.trad_s);
img5 = new Bitmap(透明时钟.Properties.Resources.trad_dot);
img6 = new Bitmap(透明时钟.Properties.Resources.trad_highlights);
break;
case 3:
img1 = new Bitmap(透明时钟.Properties.Resources.modern);
img2 = new Bitmap(透明时钟.Properties.Resources.modern_h);
img3 = new Bitmap(透明时钟.Properties.Resources.modern_m);
img4 = new Bitmap(透明时钟.Properties.Resources.modern_s);
img5 = new Bitmap(透明时钟.Properties.Resources.modern_dot);
img6 = new Bitmap(透明时钟.Properties.Resources.modern_highlights);
break;
case 4:
img1 = new Bitmap(透明时钟.Properties.Resources.flower);
img2 = new Bitmap(透明时钟.Properties.Resources.flower_h);
img3 = new Bitmap(透明时钟.Properties.Resources.flower_m);
img4 = new Bitmap(透明时钟.Properties.Resources.flower_s);
img5 = new Bitmap(透明时钟.Properties.Resources.flower_dot);
img6 = new Bitmap(透明时钟.Properties.Resources.flower_highlights);
break;
}
}
public void Draw()
{
//当前时间
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
BackgroundImage = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(BackgroundImage);
//背景
g.DrawImage(img1, 0, 0, img1.Width, img1.Height);
//坐标中心平移
g.TranslateTransform((float)Width / 2.0f, (float)Height / 2.0f);
//时针
g.RotateTransform(30.0f * h 30.0f * (float)(m / 60.0f));
g.DrawImage(img2, -img2.Width / 2, -img2.Height / 2, img2.Width, img2.Height);
g.RotateTransform(-30.0f * h - 30.0f * (float)(m / 60.0f));
//分针
g.RotateTransform(360.0f * (float)(m / 60.0f));
g.DrawImage(img3, -img3.Width / 2, -img3.Height / 2, img3.Width, img3.Height);
g.RotateTransform(-360.0f * (float)(m / 60.0f));
//秒针
g.RotateTransform((float)s * 360 / 60.0f);
g.DrawImage(img4, -img4.Width / 2, -img4.Height / 2, img4.Width, img4.Height);
g.RotateTransform(-(float)s * 360 / 60.0f);
//转轴
g.DrawImage(img5, -img5.Width / 2, -img5.Height / 2, img5.Width, img5.Height);
//坐标中心平移还原
g.TranslateTransform(-(float)Width / 2.0f, -(float)Height / 2.0f);
//表盖
g.DrawImage(img6, 0, 0, img1.Width, img1.Height);
SetBits();
}
private void Form1_Load(object sender, EventArgs e)
{
SetStyle(1);
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Draw();
}
private void 银色ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStyle(1);
}
private void 黑色ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStyle(2);
}
private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStyle(3);
}
private void 花瓣ToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStyle(4);
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:刘典武(游龙工作室)", "呵呵", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论