日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Winform中实现实时颜色拾取器显示RGB和16进制颜色(附代码下载)

發(fā)布時間:2025/3/19 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform中实现实时颜色拾取器显示RGB和16进制颜色(附代码下载) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景

效果

?

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。

實現(xiàn)

新建一個窗體頁面,設(shè)計布局如下

?

頁面上添加一個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 定義快捷鍵//如果函數(shù)執(zhí)行成功,返回值不為0。??????//如果函數(shù)執(zhí)行失敗,返回值為0。要得到擴(kuò)展錯誤信息,調(diào)用GetLastError。???????[DllImport("user32.dll", SetLastError = true)]public static extern bool RegisterHotKey(IntPtr hWnd,??????????????? //要定義熱鍵的窗口的句柄???????????int id,???????????????????? //定義熱鍵ID(不能與其它ID重復(fù))??????????????????????KeyModifiers fsModifiers,?? //標(biāo)識熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時才會生效????????Keys vk???????????????????? //定義熱鍵的內(nèi)容???????????);[DllImport("user32.dll", SetLastError = true)]public static extern bool UnregisterHotKey(IntPtr hWnd,??????????????? //要取消熱鍵的窗口的句柄???????????int id????????????????????? //要取消熱鍵的ID???????????);//定義了輔助鍵的名稱(將數(shù)字轉(zhuǎn)變?yōu)樽址员阌谟洃?#xff0c;也可去除此枚舉而直接使用數(shù)值)???????[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的熱鍵設(shè)定???UnregisterHotKey(Handle, 81);}} }

代碼下載

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12243099

總結(jié)

以上是生活随笔為你收集整理的Winform中实现实时颜色拾取器显示RGB和16进制颜色(附代码下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。