用C#实现屏幕吸色功能,附逻辑思维讲解图,功能代码不超过50行即可实现
2011-11-03 10:14 狼人:-) 阅读(3169) 评论(10) 编辑 收藏 举报此程序是我上学的时候写的,好几年前的事了,前几天整理硬盘文件时发现自已其实还写过很多东西,当时还没有在园子里面混,故没怎么分享,现在有时间那就给需要的朋友分享分享,我的主要实现思路是:
一、创建一个画布(即为Form),大小和当前屏幕大小一样
二、在这快画布上建立一个绘图对象,截取复制当前屏幕内容
三、用Image对象的GetThumbnailImage方法获取鼠标坐标点的方圆20像素的图像,然后以缩略图的形式将其放大,实现放大镜效果
四、利用API获取当前鼠标坐标点的像素色
五、吸色显示信息窗体实时跟踪
六、方向键微调功能,直接调用WIN的API设置鼠标坐标即可
先来看下吸引效果:
控件布局:
实时跟踪窗体显示模式的逻辑思维图:
始终保持吸色信息窗体保持上图所示状态(左上,右上,左下,右下),我的实现代码是这样写的:
Point p = new Point();
p.X = MousePosition.X+10;
p.Y = MousePosition.Y+10;
Size s = Screen.PrimaryScreen.Bounds.Size;
if (p.X > s.Width - this.Width)
p.X -= this.Width + 20;
if (p.Y > s.Height - this.Height)
p.Y -= this.Height + 20;
this.Location = p;
好了,下面附上我的全部代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LR.Tools
{
///<summary>
/// LR.Tools 的摘要说明
/// 程序: LR.Tools V1.0版
/// Developer: 狼人
/// QQ:459094521 博客: http://www.cnblogs.com/waw/
/// 编写时间: 2009-01-15
///<summary>
public partial class WinEatColor : LR.Tools.MasterForm
{
Form f = new Form();
public WinEatColor()
{
f.FormBorderStyle = FormBorderStyle.None;//无边框
f.Cursor = System.Windows.Forms.Cursors.Cross;
f.WindowState = FormWindowState.Maximized;
f.Opacity = 0.01;
f.ShowInTaskbar = false;
f.Show();
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//鼠标的坐标
this.label1.Text = "鼠标坐标:"+MousePosition.X+","+MousePosition.Y;
//显示句柄
this.label3.Text = "句柄:" + LR.Win32API.WindowAPI.WindowFromPoint(MousePosition);
//当前窗体自动跟随鼠标
this.MoveForm();
//利用API获取当前鼠标坐标点的像素色
Color c = LR.Win32API.WindowAPI.GetColorOfScreen(MousePosition);
//显示在网页中显示的编码
this.textBox1.Text = "#"+c.Name.ToUpper().Substring(2);
//显示RGB三位色
this.txt_RGB.Text = c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString();
//设置label的颜色
this.label6.BackColor = c;
//显示放大镜
this.ShowPictureBox(MousePosition);
}
void ShowPictureBox(Point p)
{
//创建一个画布大小和当前屏幕大小一样
Bitmap bmp = new Bitmap(20,20);
//在这快画布上建立一个绘图对象
Graphics g = Graphics.FromImage(bmp);
//截取复制当前屏幕内容
g.CopyFromScreen(p.X-10, p.Y-10, 0,0, bmp.Size);
//以缩略图的形式就放大镜
Image pThumbnail = bmp.GetThumbnailImage(this.pictureBox1.Width, this.pictureBox1.Height, null, new IntPtr());
//画放大图
g.DrawImage(bmp, 10, 10, pThumbnail.Width, pThumbnail.Height);
g.Dispose();
this.pictureBox1.Image = pThumbnail;
g = Graphics.FromImage(this.pictureBox1.Image);
g.DrawRectangle(Pens.Black, this.pictureBox1.Width / 2 - 5, this.pictureBox1.Height / 2 - 5, 10, 10);
g.Dispose();
}
void MoveForm()
{
Point p = new Point();
p.X = MousePosition.X+10;
p.Y = MousePosition.Y+10;
Size s = Screen.PrimaryScreen.Bounds.Size;
if (p.X > s.Width - this.Width)
p.X -= this.Width + 20;
if (p.Y > s.Height - this.Height)
p.Y -= this.Height + 20;
this.Location = p;
}
private void WinEatColor_Load(object sender, EventArgs e)
{
}
private void WinEatColor_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
this.timer1.Stop();
Point pCur = MousePosition;
//方向键微调
if (e.KeyCode == Keys.Up)
LR.Win32API.WindowAPI.SetCursorPos(pCur.X, pCur.Y - 1);
if (e.KeyCode == Keys.Left)
LR.Win32API.WindowAPI.SetCursorPos(pCur.X - 1, pCur.Y);
if (e.KeyCode == Keys.Right)
LR.Win32API.WindowAPI.SetCursorPos(pCur.X + 1, pCur.Y);
if (e.KeyCode == Keys.Down)
LR.Win32API.WindowAPI.SetCursorPos(pCur.X, pCur.Y + 1);
}
private void WinEatColor_Deactivate(object sender, EventArgs e)
{
this.timer1.Stop();
}
//重拾
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
private void textBox1_MouseEnter(object sender, EventArgs e)
{
this.textBox1.Focus();
this.textBox1.SelectAll();
this.textBox1.Copy();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
LR.Win32API.WindowAPI.MouseMoveWindow(this.Handle);
}
private void WinEatColor_FormClosed(object sender, FormClosedEventArgs e)
{
f.Close();
}
}
}
倘若还有不明白的朋友可以在评论中留言
声明:此博有部分内容为转载,版权归原作者所有~