轉自: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(); } } }