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");
}
}
}
}