Может ли моя горячая клавиша нажимать кнопку, не фокусируя окно. Спасибо и это мой проект. Ключом для моего определения цвета является V, а Ctrl-J - это ключ, который мне нужен для окна фокусировки, пожалуйста, если кто-нибудь может мне помочь с этим.
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V)
{
button1.PerformClick();
}
}
private bool _dragging = false;
private Point _offset;
private Point _start_point = new Point(0, 0);
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_dragging = true; // _dragging is your variable flag
_start_point = new Point(e.X, e.Y);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_dragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
}
}
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInf);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int x, int y);
private void OnButtonSearchPixelClick(object sender, EventArgs e)
{
// 01. get hex value from input field
string inputHexColorCode = textBox1.Text;
SearchPixel(inputHexColorCode);
}
private bool SearchPixel(string hexcode)
{
// Take an image from the screen
// Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // Create an empty bitmap with the size of the current screen
Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); // Create an empty bitmap with the size of all connected screen
Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image); // Create a new graphics objects that can capture the screen
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); // Screenshot moment → screen content to graphics object
Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);
// Go one to the right and then check from top to bottom every pixel (next round -> go one to right and go down again)
for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++)
{
for (int y = 0; y < SystemInformation.VirtualScreen.Height; y++)
{
// Get the current pixels color
Color currentPixelColor = bitmap.GetPixel(1200, 670);
// Finally compare the pixels hex color and the desired hex color (if they match, we found a pixel)
if (desiredPixelColor == currentPixelColor)
{
DoubleClickAtPosition(813, 680);
return true;
}
}
}
return false;
}
private void DoubleClickAtPosition(int posX, int posY)
{
SetCursorPos(posX, posY);
Click();
System.Threading.Thread.Sleep(250);
Click();
}
private new void Click()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
}
Код должен был быть похож на определение цвета приложение для игры.
Кроме того, этот конкретный код c - это то, что я хотел сделать глобальным.
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V)
{
button1.PerformClick();
}
}
```