Winform中实现实时颜色拾取器显示RGB和16进制颜色(附代码下载)
程序员文章站
2023-02-21 13:58:04
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 新建一个窗体页面,设计布局如下 页面上添加一个Timer 然后修改此页面代码 using System; usi ......
场景
效果
注:
博客主页:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
新建一个窗体页面,设计布局如下
页面上添加一个timer
然后修改此页面代码
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.runtime.interopservices; namespace getcolor { public partial class frmgetcolor : form { public frmgetcolor() { initializecomponent(); } #region 定义快捷键 //如果函数执行成功,返回值不为0。 //如果函数执行失败,返回值为0。要得到扩展错误信息,调用getlasterror。 [dllimport("user32.dll", setlasterror = true)] public static extern bool registerhotkey( intptr hwnd, //要定义热键的窗口的句柄 int id, //定义热键id(不能与其它id重复) keymodifiers fsmodifiers, //标识热键是否在按alt、ctrl、shift、windows等键时才会生效 keys vk //定义热键的内容 ); [dllimport("user32.dll", setlasterror = true)] public static extern bool unregisterhotkey( intptr hwnd, //要取消热键的窗口的句柄 int id //要取消热键的id ); //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) [flags()] public enum keymodifiers { none = 0, alt = 1, ctrl = 2, shift = 4, windowskey = 8 } #endregion [dllimport("gdi32.dll")] static public extern uint getpixel(intptr hdc,int xpos,int ypos); [dllimport("gdi32.dll")] static public extern intptr createdc(string drivername,string devicename,string output,intptr lpinitdata); [dllimport("gdi32.dll")] static public extern bool deletedc(intptr dc); static public byte getrvalue(uint color) { return (byte)color; } static public byte getgvalue(uint color) { return ((byte)(((short)(color))>>8)); } static public byte getbvalue(uint color) { return ((byte)((color)>>16)); } static public byte getavalue(uint color) { return ((byte)((color)>>24)); } public color getcolor(point screenpoint) { intptr displaydc = createdc("display",null,null,intptr.zero); uint colorref = getpixel(displaydc,screenpoint.x,screenpoint.y); deletedc(displaydc); byte red = getrvalue(colorref); byte green = getgvalue(colorref); byte blue = getbvalue(colorref); return color.fromargb(red, green, blue); } private void frmgetcolor_load(object sender, eventargs e) { this.topmost = true; } private void checkbox1_checkedchanged(object sender, eventargs e) { if (checkbox1.checked == true) { this.topmost = true; } else { this.topmost = false; } } private void timer1_tick(object sender, eventargs e) { txtpoint.text=control.mouseposition.x.tostring()+","+control.mouseposition.y.tostring(); point pt = new point(control.mouseposition.x, control.mouseposition.y); color cl = getcolor(pt); panel1.backcolor = cl; txtrgb.text = cl.r + "," + cl.g + "," + cl.b; txtcolor.text = colortranslator.tohtml(cl).tostring(); registerhotkey(handle, 81, keymodifiers.ctrl, keys.f); } private void button1_click(object sender, eventargs e) { application.exit(); } private void button2_click(object sender, eventargs e) { aboutbox1 ab = new aboutbox1(); ab.showdialog(); } private void label3_click(object sender, eventargs e) { system.diagnostics.process.start("http://www.mrbccd.com"); } protected override void wndproc(ref message m) { const int wm_hotkey = 0x0312; //按快捷键 switch (m.msg) { case wm_hotkey: switch (m.wparam.toint32()) { case 81: //按下的是ctrl+f clipboard.settext(txtcolor.text.trim()); break; } break; } base.wndproc(ref m); } private void frmgetcolor_leave(object sender, eventargs e) { } private void frmgetcolor_formclosed(object sender, formclosedeventargs e) { //注销id号为81的热键设定 unregisterhotkey(handle, 81); } } }
代码下载
https://download.csdn.net/download/badao_liumang_qizhi/12243099