The client already provided the design and layout of the keyboard for the touch screen for the self service machine. I was thinking to overlay those individual keys with a transparent pictureboxes or labels and then hook up the mouse click event to get which key that the user touched.First of all, why are you using transparent picture boxes to do hit testing?
class KeyRect
{
public string Key { get; set; }
public Rectangle Rect { get; set; }
}
:
List<KeyRect> _keys = new List<KeyRect>()
{
new KeyRect() { "Delete", new Rectangle(150, 5, 40, 20) },
new KeyRect() { "A", new Rectangle(10, 75, 10, 10) },
:
}
public Dictionary<string, Tuple<Point, Point>> Key_Positions = new Dictionary<string, Tuple<Point, Point>>();
private void Form1_Shown(object sender, EventArgs e)
{
// These : new Point(50, 30), cover from the left of the form to left side of number 1 (50). Top of the form to top of the number 1 is (30).
// These : new Point(120, 80), cover from the left of the form to right side of number 1 (120). Top of the form to bottom of the number 1 is (80).
Key_Positions.Add("1", Tuple.Create(new Point(50, 30), new Point(120, 80)));
/* Add your custom MouseMove and MouseUpEvents */
pictureBox1.MouseMove += PictureBox1_MouseMove;
pictureBox1.MouseUp += PictureBox1_MouseUp;
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Location = e.Location;
label1.Text = e.Location.ToString();
/* This will move the label away from the actual position so not to prvent clicking the picture box. Otherwise the label will block your click. */
label1.Location = new Point(e.Location.X + 20, e.Location.Y + 10);
}
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
foreach (var kp in from KeyValuePair<string, Tuple<Point, Point>> kp in Key_Positions
let X_Pos1 = e.Location.X >= kp.Value.Item1.X/* is Greater than 50 */
let X_Pos2 = e.Location.X <= kp.Value.Item2.X/* is Less than 30 */
let x_Pos3 = e.Location.Y >= kp.Value.Item1.Y/* is Gtreater than 30 */
let x_Pos4 = e.Location.Y <= kp.Value.Item2.Y/* is Less than 80 */
select kp)
{
if (e.Location.X >= kp.Value.Item1.X && e.Location.X <= kp.Value.Item2.X &&
e.Location.Y >= kp.Value.Item1.Y && e.Location.Y <= kp.Value.Item2.Y)
{
Debug.WriteLine("You clicked 1");
}
else
{
Debug.WriteLine("1 was not clicked");
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Dictionary<string, Tuple<Point, Point>> Key_Positions = new Dictionary<string, Tuple<Point, Point>>();
private void Form1_Shown(object sender, EventArgs e)
{
// These : new Point(50, 30), cover from the left of the form to left side of number 1 (50). Top of the form to top of the number 1 is (30).
// These : new Point(120, 80), cover from the left of the form to right side of number 1 (120). Top of the form to bottom of the number 1 is (80).
Key_Positions.Add("1", Tuple.Create(new Point(50, 30), new Point(120, 80)));
/* Add your custom MouseMove and MouseUpEvents */
pictureBox1.MouseMove += PictureBox1_MouseMove;
pictureBox1.MouseUp += PictureBox1_MouseUp;
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Location = e.Location;
label1.Text = e.Location.ToString();
/* This will move the label away from the actual position so not to prvent clicking the picture box. Otherwise the label will block your click. */
label1.Location = new Point(e.Location.X + 20, e.Location.Y + 10);
}
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
foreach (var kp in from KeyValuePair<string, Tuple<Point, Point>> kp in Key_Positions
let X_Pos1 = e.Location.X >= kp.Value.Item1.X/* is Greater than 50 */
let X_Pos2 = e.Location.X <= kp.Value.Item2.X/* is Less than 30 */
let x_Pos3 = e.Location.Y >= kp.Value.Item1.Y/* is Gtreater than 30 */
let x_Pos4 = e.Location.Y <= kp.Value.Item2.Y/* is Less than 80 */
select kp)
{
if (e.Location.X >= kp.Value.Item1.X && e.Location.X <= kp.Value.Item2.X &&
e.Location.Y >= kp.Value.Item1.Y && e.Location.Y <= kp.Value.Item2.Y)
{
Debug.WriteLine("You clicked 1");
}
else
{
Debug.WriteLine("1 was not clicked");
}
}
}
}