Lets say that you wanted to implement this feature on a usercontrol that you were making. At first you would need to handle the mouse down event to register where the user clicked, and after this you should be able to handle the key down event to register which keys the user pressed.
To draw the actual text at the target location you have several options. You could use GDI+ to render the strings on the control or you could insert labels which would benefit you due to the amount of functionality that the labels come with.
Here's a prototype example of how it could be implemented using GDI:
private Point? lastSelected;
private Dictionary<Point, string> renderedText = new Dictionary<Point, string>();
private Point LastSelected
{
get
{
return (Point)lastSelected;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.MouseDown += Form1_MouseDown;
this.KeyDown += Form1_KeyDown;
this.Paint += Form1_Paint;
}
void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (KeyValuePair<Point, string> pair in renderedText)
{
e.Graphics.DrawString(pair.Value, new Font("Arial", 12), Brushes.Black,
pair.Key);
}
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (lastSelected != null)
{
if (!renderedText.ContainsKey(LastSelected))
{
renderedText.Add(LastSelected, "");
}
renderedText[LastSelected] = renderedText[LastSelected] + e.KeyCode;
this.Invalidate();
}
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastSelected = e.Location;
}
Reply to comment:
The above code captures the location of the mouse when the user clicks on the form and saves it in the lastSelected variable. At each subsequent key press on the form the key pressed is appended to the string representing that location. Furthermore the strings are being rendered on the screen at the captured location in the paint. The strings are rendered using GDI+, which means that you don't need to worry about the length of the individual characters.
Note that for a real application storing the locations and strings via a hashtabel (dictionary in C#) is not a great idea. You should rather create a custom class or structure that will hold the information such as location, text ect. This will allow you to further improve the functionality by adding options for stuff like bolding, italic, font sizes and so on.