Mahlzeit zusammen,
bin die letzten Tage dabei mir mal ein wenig C# anzuschauen und mein erstes "Programm" zu entwickeln.
Allerdings stehe ich vor dem Problem, dass ich - egal was ich versuche - keine Tasten abfragen kann, wenn sich die Windows Form z.b. im Hintergrund oder in der Taskleiste befindet.
Sämtliche Code und Snippets die ich im Web finde, funktionieren nicht oder nur teilweise in z.b. Konsolenanwendungen.
Eventuell kann mir hier ja jemand helfen und erklären, wie ich die Tasten abfangen kann.
Mein bisheriger Code:
C
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace YetAnotherColorPicker
{
public partial class Form1 : Form
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
int x, y;
Color color;
Thread t;
public static Thread timer;
private bool pickerStartet = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = pickColorPanel;
//comboBox1.SelectedIndex = 0;
//this.t.Start();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void AppendTextLabel()
{
if(InvokeRequired)
{
this.Invoke(new Action(AppendTextLabel));
return;
}
coordsLabel.Text = "X: " + x.ToString() + " Y: " + y.ToString(); ;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.pickerStartet == true)
{
this.t.Abort();
}
}
private void schließenToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void neustartenToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
if (this.pickerStartet == true)
{
this.t.Abort();
this.pickerStartet = false;
}
else
{
this.t = new Thread(update);
this.t.Start();
this.pickerStartet = true;
}
}
private void update()
{
Bitmap screenCopy = new Bitmap(1, 1);
using (Graphics gdest = Graphics.FromImage(screenCopy))
{
while (true)
{
x = Cursor.Position.X;
y = Cursor.Position.Y;
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
color = Color.FromArgb(screenCopy.GetPixel(0, 0).ToArgb());
pickColorPanel.BackColor = color;
AppendTextLabel();
Thread.Sleep(50);
//MessageBox.Show(c.ToString());
}
}
}
}
}
Alles anzeigen
LG & Danke