public partial class MainForm : Form { private MouseKeyHook mousekeyHook = new MouseKeyHook(); private Timer time = new Timer(); private Point point; private int width = 40; private int height = 40; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { try { this.mousekeyHook.MouseMoveEvent += new MouseKeyHook.MouseMoveHandler(this.Mouse_Move); this.mousekeyHook.MouseClickEvent += new MouseKeyHook.MouseClickHandler(this.Mouse_Click); this.mousekeyHook.DirectionKeyDown += new KeyEventHandler(this.Direction_KeyDown); this.time.Tick += new EventHandler(this.time_Tick); this.time.Interval = 100; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 开始 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void startbtn_Click(object sender, EventArgs e) { try { this.mousekeyHook.Hook_Start(); this.time.Start(); this.stopbtn.Focus(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 停止 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void stopbtn_Click(object sender, EventArgs e) { try { this.time.Stop(); this.mousekeyHook.Hook_Clear(); this.startbtn.Focus(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 窗口置顶 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void topmostcb_CheckedChanged(object sender, EventArgs e) { try { this.TopMost = this.topmostcb.Checked; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 鼠标移动事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Mouse_Move(object sender, MouseEventArgs e) { try { this.point.X = e.Location.X; this.point.Y = e.Location.Y; this.xvalue.Text = this.point.X.ToString(); this.yvalue.Text = this.point.Y.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 鼠标右击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Mouse_Click(object sender, MouseEventArgs e) { this.stopbtn_Click(null, null); } /// <summary> /// 键盘方向键 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Direction_KeyDown(object sender, KeyEventArgs e) { try { WinAPI.PointStruct pt = new WinAPI.PointStruct(); WinAPI.GetCursorPos(ref pt); if (e.KeyCode == (Keys.RButton | Keys.MButton | Keys.Space)) { pt.Y--; } else if (e.KeyCode == (Keys.Back | Keys.Space)) { pt.Y++; } else if (e.KeyCode == (Keys.LButton | Keys.MButton | Keys.Space)) { pt.X--; } else if (e.KeyCode == (Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space)) { pt.X++; } WinAPI.SetCursorPos(pt.X, pt.Y); this.point.X = pt.X; this.point.Y = pt.Y; this.xvalue.Text = this.point.X.ToString(); this.yvalue.Text = this.point.Y.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误"); } } /// <summary> /// 定时刷新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void time_Tick(object sender, EventArgs e) { Image img = this.regionColor.BackgroundImage; using (Bitmap bmp = new Bitmap(width + 1, height + 1)) { using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(new Point(this.point.X - this.width / 2, this.point.Y - this.height / 2), new Point(0, 0), new Size(bmp.Width, bmp.Height)); this.regionColor.BackgroundImage = this.ZoomImage(bmp, this.regionColor.Size); this.selectColor.BackColor = bmp.GetPixel(this.width / 2, this.height / 2); this.htmltxt.Text = System.Drawing.ColorTranslator.ToHtml(this.selectColor.BackColor); this.argbtxt.Text = this.selectColor.BackColor.A.ToString().ToLower() + "," + this.selectColor.BackColor.R.ToString().ToLower() + "," + this.selectColor.BackColor.G.ToString().ToLower() + "," + this.selectColor.BackColor.B.ToString().ToLower(); this.rgbtxt.Text = this.selectColor.BackColor.R.ToString().ToLower() + "," + this.selectColor.BackColor.G.ToString().ToLower() + "," + this.selectColor.BackColor.B.ToString().ToLower(); } } if (img != null) img.Dispose(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { this.stopbtn_Click(null, null); } /// <summary> /// 缩放图片 /// </summary> /// <param name="bmp">图片源</param> /// <param name="newiwdth">新图片大小</param> /// <returns></returns> private Bitmap ZoomImage(Bitmap bmp, Size size) { Bitmap img = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(img)) { g.InterpolationMode = InterpolationMode.NearestNeighbor; g.SmoothingMode = SmoothingMode.None; g.DrawImage(bmp, new Rectangle(0, 0, size.Width, size.Height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); } return img; } }
源码下载地址:拾色器.zip