My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.

How to get the mouse-click and mouseover coordinates from an image This example shows how easy it is to respond to what the mouse is doing on top of an image: in the event handlers you get the coordinates of the mouse when clicked or moved. Note that this works in Silverlight as well, you just have to use ContentControl_MouseLeftButtonDown. ![]() XAML:
<Window x:Class="TestClick828374.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <StackPanel Margin="10"> <ContentControl Content="{Binding TheImage}" MouseDown="ContentControl_MouseDown" MouseMove="ContentControl_MouseMove" Margin="0 0 0 10"/> <StackPanel Orientation="Horizontal"> <TextBlock Text="mouse-click X = "/> <TextBlock Text="{Binding HorizontalClickPoint}"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0 0 0 5"> <TextBlock Text="mouse-click Y = "/> <TextBlock Text="{Binding VerticalClickPoint}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="mouseover X = "/> <TextBlock Text="{Binding HorizontalMouseOverPoint}"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0 0 0 5"> <TextBlock Text="mouseover Y = "/> <TextBlock Text="{Binding VerticalMouseOverPoint}"/> </StackPanel> </StackPanel> </Window> Code-Behind:
using System;
using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.ComponentModel; namespace TestClick828374 { public partial class Window1 : Window, INotifyPropertyChanged { #region ViewModelProperty: TheImage private Image _theImage; public Image TheImage { get { return _theImage; } set { _theImage = value; OnPropertyChanged("TheImage"); } } #endregion test #region ViewModelProperty: HorizontalClickPoint private double _horizontalClickPoint; public double HorizontalClickPoint { get { return _horizontalClickPoint; } set { _horizontalClickPoint = value; OnPropertyChanged("HorizontalClickPoint"); } } #endregion test #region ViewModelProperty: VerticalClickPoint private double _verticalClickPoint; public double VerticalClickPoint { get { return _verticalClickPoint; } set { _verticalClickPoint = value; OnPropertyChanged("VerticalClickPoint"); } } #endregion test #region ViewModelProperty: HorizontalMouseOverPoint private double _horizontalMouseOverPoint; public double HorizontalMouseOverPoint { get { return _horizontalMouseOverPoint; } set { _horizontalMouseOverPoint = value; OnPropertyChanged("HorizontalMouseOverPoint"); } } #endregion test #region ViewModelProperty: VerticalMouseOverPoint private double _verticalMouseOverPoint; public double VerticalMouseOverPoint { get { return _verticalMouseOverPoint; } set { _verticalMouseOverPoint = value; OnPropertyChanged("VerticalMouseOverPoint"); } } #endregion test public Window1() { InitializeComponent(); DataContext = this; TheImage = new Image(); TheImage.Source = new BitmapImage(new Uri(@"c:testrectangle.png")); TheImage.Stretch = Stretch.None; TheImage.HorizontalAlignment = HorizontalAlignment.Left; } #region INotifiedProperty Block public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion test private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { Point clickPoint = e.GetPosition(TheImage); HorizontalClickPoint = clickPoint.X; VerticalClickPoint = clickPoint.Y; } private void ContentControl_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { Point clickPoint = e.GetPosition(TheImage); HorizontalMouseOverPoint = clickPoint.X; VerticalMouseOverPoint = clickPoint.Y; } } } |
Most Recently Added Items:
- Extension method for checking regex in one line - c# code example - added 4 days ago
- How to use a Dictionary<> with struct key to save a dynamic matrix of objects - c# code example - added on Sunday, August 22, 2010
- A simple jquery search machine for a web page - jquery code example - added on Sunday, August 22, 2010
- Wrapper class to simplify the creation of Excel files in C# 4.0 - wpf code example - added on Tuesday, July 20, 2010
- How to make clickable flashcards in plain javascript for your mobile phone - javascript code example - added on Wednesday, July 07, 2010
- Simple example of javascript which loads jquery locally - jquery code example - added on Wednesday, July 07, 2010
- How to stop regular expression greediness - c# code example - added on Tuesday, July 06, 2010
- How to use a generic dictionary to total enum values - c# code example - added on Friday, July 02, 2010
- Generic method to case-insensitively convert a string to any enum - c# code example - added on Friday, July 02, 2010
- How to create a TextBlock that has various font formatting in code behind - silverlight code example - added on Wednesday, June 30, 2010
- How to encode binary files to text files and back to binary again - c# code example - added on Wednesday, June 30, 2010
- How to use a custom parameter struct to pass any number of variables to constructors of similar classes. - c# code example - added on Saturday, June 26, 2010
- How to make a class that renders an interactive FrameworkElement and interacts with the View - silverlight code example - added on Tuesday, June 15, 2010
- How to set a nullable type to null in a ternary operator - c# code example - added on Tuesday, June 15, 2010
- How to override events in inherited classes - c# code example - added on Friday, June 11, 2010
- How to strip off e.g. "note:" and "firstName: " from the left of a string using regex - c# code example - added on Tuesday, June 01, 2010
- How to create and subscribe to custom events using EventHandler - c# code example - added on Wednesday, May 12, 2010
- Code base for asynchronously loading and caching dependent data in a Silverlight app - silverlight code example - added on Wednesday, May 05, 2010
- Function to trim the preceding and trailing blank lines off an array - php code example - added on Sunday, May 02, 2010
- How to load and display the contents of a text file with AJAX/Jquery - jquery code example - added on Sunday, May 02, 2010
- How to use fopen() to create a proxy site to read any website content into AJAX - php code example - added on Sunday, May 02, 2010
- Code base for loading and caching external data into a silverlight app - silverlight code example - added on Friday, April 30, 2010
- An UpdateSourceTrigger workaround for Silverlight - wpf code example - added on Sunday, April 18, 2010
- Silverlight layout with click-in info panel - silverlight code example - added on Thursday, April 01, 2010
- How to get querystring variables and change HTML elements from Silverlight. - silverlight code example - added on Thursday, April 01, 2010
- XAML and code for a basic chat window in WPF - wpf code example - added on Saturday, March 27, 2010
- How to consume text from any Google Document, RSS feed, or Twitter feed in your Silverlight application - wpf code example - added on Sunday, March 21, 2010
- How to overlay one image on top of another in code behind - wpf code example - added on Saturday, March 20, 2010
- How to get the mouse-click and mouseover coordinates from an image - wpf code example - added on Tuesday, March 16, 2010
- How to use OrderedDictionary to lookup items by string key or integer index - c# code example - added on Tuesday, March 16, 2010
