-
Notifications
You must be signed in to change notification settings - Fork 1
/
PixelColor.cs
207 lines (146 loc) · 5.99 KB
/
PixelColor.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace CLICKER2
{
public partial class PixelColor : Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
private static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
[DllImport("gdi32.dll")]
public static extern int BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, int dwRop);
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);
// public string c;
Form1 frm1;
bool state;
int xPos;
int yPos;
Color c;
string colorHTML;
public PixelColor(Form1 F, string cHTML)
{
InitializeComponent();
frm1 = F;
state = false;
pictureBox1.BackColor = c;
colorHTML = cHTML;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
}
protected override void OnMouseMove(MouseEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (xPos != 0 && yPos != 0)
{
string colorHTML = ColorTranslator.ToHtml(c);
frm1.listView1.SelectedItems[0].SubItems[2].Text = xPos.ToString();
frm1.listView1.SelectedItems[0].SubItems[3].Text = yPos.ToString();
frm1.listView1.SelectedItems[0].SubItems[7].Text = colorHTML;
frm1.additionalDelay = Int32.Parse(delayBox.Text);
}
if (delayBox.Text == "") delayBox.Text = "0";
frm1.additionalDelay = Int32.Parse(delayBox.Text);
this.Close();
}
Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
public Color GetColorAt(Point location)
{
using (Graphics gdest = Graphics.FromImage(screenPixel))
{
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
return screenPixel.GetPixel(0, 0);
}
private void PixelColor_KeyDown(object sender, KeyEventArgs e)
{
if (state)
{
Point p1 = new Point();
p1.X = Cursor.Position.X;
p1.Y = Cursor.Position.Y;
xPos = Cursor.Position.X;
yPos = Cursor.Position.Y;
xPosLabel.Text = xPos.ToString();
yPosLabel.Text = yPos.ToString();
c = GetColorAt(p1);
pictureBox1.BackColor = c;
state = false;
this.TopMost = false;
}
this.Cursor = Cursors.Arrow;
}
public static Cursor ActuallyLoadCursor(String path)
{
return new Cursor(LoadCursorFromFile(path));
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string fileName);
private void pickColorButton_Click(object sender, EventArgs e)
{
state = !state;
this.TopMost = true;
// string filename = "C:\\Windows\\Cursors\\Eyedropper.Cur";
// Cursor cur = ActuallyLoadCursor(filename);
// this.Cursor = cur;
}
private void PixelColor_MouseClick(object sender, MouseEventArgs e)
{
}
private void PixelColor_Shown(object sender, EventArgs e)
{
//Fill relevant values
xPos = 0;
yPos = 0;
xPosLabel.Text = frm1.listView1.SelectedItems[0].SubItems[2].Text.ToString();
yPosLabel.Text = frm1.listView1.SelectedItems[0].SubItems[3].Text.ToString();
delayBox.Text = frm1.additionalDelay.ToString();
if (colorHTML != "Color [Empty]")
{
//color HTML value passed in from the list view
pictureBox1.BackColor = ColorTranslator.FromHtml(colorHTML);
}
}
private void PixelColor_Load(object sender, EventArgs e)
{
}
private void delayBox_KeyPress(object sender, KeyPressEventArgs e)
{
//Restrit user input to numbers only in a given textbox
if (Char.IsDigit(e.KeyChar))
return;
else if (e.KeyChar < ' ') // <- I suggest that you allow user to press ESC, TAB etc.
return;
// Other keys are forbidden
e.Handled = true;
}
}
}