用C#实现屏幕吸色功能,附逻辑思维讲解图,功能代码不超过50行即可实现

转自:http://www.cnblogs.com/waw/archive/2011/11/03/2234195.html

 

主要实现思路是:

一、创建一个画布(即为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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值