C#获取鼠标当前颜色RGB、获取鼠标当前HEX值、获取鼠标当前坐标(源码)

Lete 乐特自制实用工具(C# - .NET Framework4.5)


由于上次使用 Java 编写的实用工具https://lete114.github.io/article/986f7d9c.html

Java 写窗体程序的优缺点

优点:

  1. 可跨平台

缺点:

  1. Java 程序依赖 JVM(Java 虚拟机)
  2. 由于 JVM 是存储在 jre 下的(而 Java 程序又依赖JVM(Java 虚拟机))jre 占用内存高达 200MB

C#写窗体程序的优缺点

由于 Java 程序不便于携带,这次我使用 C#编写

优点:

  1. 体积小(小于 1MB)
  2. 便于携带
  3. 便于编写(随意拖动按钮图标等)

缺点:

  1. C#依赖 .NET Framework
    (这个不要紧,大多数电脑都会装有 .NET Framework,玩游戏的应该需要装游戏运行库,运行库里就包含有 .NET Framework)

FormMain.cs

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delete
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
[DllImport("user32.dll")]//取设备场景
private static extern IntPtr GetDC(IntPtr hwnd);//返回设备场景句柄
[DllImport("gdi32.dll")]//取指定点颜色
private static extern int GetPixel(IntPtr hdc, Point p);



// 单击乐特
private void lete_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://lete114.github.io/");
}
// 窗体加载事件
private void LeteTools_Load(object sender, EventArgs e)
{
this.tm.Enabled = true;
this.tm.Interval = 1;//timer控件的执行频率
}

//计时器
private void tm_Tick(object sender, EventArgs e)
{
// 计时器
Timer tim = new Timer();
tim.Interval = 1;
tim.Tick += delegate
{
//获取RGB鼠标当前位置颜色
Point p = new Point(MousePosition.X, MousePosition.Y);//取置顶点坐标
IntPtr hdc = GetDC(new IntPtr(0));//取到设备场景(0就是全屏的设备场景)
int c = GetPixel(hdc, p);//取指定点颜色
int r = (c & 0xFF);//转换R
int g = (c & 0xFF00) / 256;//转换G
int b = (c & 0xFF0000) / 65536;//转换B
//RGB.BackColor = Color.FromArgb(r, g, b);
lblR255.Text = r.ToString();
lblG255.Text = g.ToString();
lblB255.Text = b.ToString();


// 用主类实例化,调用convertRGBToHex方法,传入RGB
FormMain lt = new FormMain();
lblVHEX.Text = lt.convertRGBToHex(r, g, b);

// 鼠标坐标
//方法1:
//lblVX.Text = Cursor.Position.X.ToString();
//lblVY.Text = Cursor.Position.Y.ToString();
//方法2:
lblVX.Text = Control.MousePosition.X.ToString();
lblVY.Text = Control.MousePosition.Y.ToString();

};
tim.Start();

}


// 计算HEX值
public String convertRGBToHex(int r, int g, int b)
{
String rFString, rSString, gFString, gSString, bFString, bSString, result;
int red, green, blue;
int rred, rgreen, rblue;
red = r / 16;
rred = r % 16;
if (red == 10) rFString = "A";
else if (red == 11) rFString = "B";
else if (red == 12) rFString = "C";
else if (red == 13) rFString = "D";
else if (red == 14) rFString = "E";
else if (red == 15) rFString = "F";
else rFString = red.ToString();

if (rred == 10) rSString = "A";
else if (rred == 11) rSString = "B";
else if (rred == 12) rSString = "C";
else if (rred == 13) rSString = "D";
else if (rred == 14) rSString = "E";
else if (rred == 15) rSString = "F";
else rSString = rred.ToString();

rFString = rFString + rSString;

green = g / 16;
rgreen = g % 16;

if (green == 10) gFString = "A";
else if (green == 11) gFString = "B";
else if (green == 12) gFString = "C";
else if (green == 13) gFString = "D";
else if (green == 14) gFString = "E";
else if (green == 15) gFString = "F";
else gFString = green.ToString();

if (rgreen == 10) gSString = "A";
else if (rgreen == 11) gSString = "B";
else if (rgreen == 12) gSString = "C";
else if (rgreen == 13) gSString = "D";
else if (rgreen == 14) gSString = "E";
else if (rgreen == 15) gSString = "F";
else gSString = rgreen.ToString();

gFString = gFString + gSString;

blue = b / 16;
rblue = b % 16;

if (blue == 10) bFString = "A";
else if (blue == 11) bFString = "B";
else if (blue == 12) bFString = "C";
else if (blue == 13) bFString = "D";
else if (blue == 14) bFString = "E";
else if (blue == 15) bFString = "F";
else bFString = blue.ToString();

if (rblue == 10) bSString = "A";
else if (rblue == 11) bSString = "B";
else if (rblue == 12) bSString = "C";
else if (rblue == 13) bSString = "D";
else if (rblue == 14) bSString = "E";
else if (rblue == 15) bSString = "F";
else bSString = rblue.ToString();
bFString = bFString + bSString;
result = "#" + rFString + gFString + bFString;
return result;

}

// 进入删除窗体
private void Zhu_Click(object sender, EventArgs e)
{
Delete m = new Delete();
m.Show();
}
}
}

Delete.cs

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delete
{

public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}

// 按钮
private void btnDelete_Click(object sender, EventArgs e)
{
// 获取路径
string str = txtPath.Text;
str.Replace("\\", "/");
if (str.Equals(""))
{
MessageBox.Show("路径不能为空!", "提示");
}
else if (str.Equals("Q:\\") || str.Equals("W:\\") || str.Equals("E:\\") || str.Equals("R:\\") || str.Equals("T:\\") || str.Equals("Y:\\") || str.Equals("U:\\") || str.Equals("I:\\") || str.Equals("O:\\") || str.Equals("P:\\")|| str.Equals("A:\\") || str.Equals("S:\\") || str.Equals("D:\\") || str.Equals("G:\\") || str.Equals("H:\\")|| str.Equals("J:\\") || str.Equals("K:\\") || str.Equals("L:\\") || str.Equals("Z:\\") || str.Equals("X:\\")|| str.Equals("C:\\") || str.Equals("C:\\") || str.Equals("V:\\") || str.Equals("B:\\") || str.Equals("N:\\")|| str.Equals("M:\\"))
{
MessageBox.Show("路径不能为盘符!", "提示");
}
else
{
getPath(str);
}
}

// 窗体加载事件
private void Main_Load(object sender, EventArgs e)
{

}
// 浏览
private void btnLL_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == DialogResult.OK)
{
String DirPath = f.SelectedPath;
this.txtPath.Text = DirPath;//G:\新建文件夹
}
}

static List<string> list = new List<string>();//定义list变量
public List<string> getPath(string path)
{
// 获取子目录
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] fil = dir.GetFiles();
DirectoryInfo[] dii = dir.GetDirectories();
foreach (FileInfo f in fil)
{
list.Add(f.FullName);//添加文件的路径到列表
}
//获取子文件夹内的文件列表,递归遍历
foreach (DirectoryInfo d in dii)
{
getPath(d.FullName);
list.Add(d.FullName);//添加文件夹的路径到列表
}

// 删除空目录
/// 删除掉空文件夹
/// 所有没有子“文件系统”的都将被删除
DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);
foreach (DirectoryInfo subdir in subdirs)
{
FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
if (subFiles.Count() == 0)
{
subdir.Delete();
}
}
return list;
}
}
}

Program.cs

COPY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delete
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}


下载地址

这里顺便推荐个云盘曲奇云盘https://quqi.com用了有几个月了还不错

同样和百度云一样送2T空间,晚了可能就没有了,关键是不限速

曲奇云盘:https://quqi.gblhgk.com/s/47889/mAxdzF3Auy0rAN0t

Github:https://github.com/lete114/C.Tools

Gitee:https://github.com/lete114/C.Tools


【Lete 乐特个人博客】:https://lete114.now.sh

Authorship: Lete乐特
Article Link: https://blog.imlete.cn/article/2edfe4f5.html
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite Lete乐特 's Blog !