The first thing to note is that that is bad code: bitmaps and graphics contexts are scarce resources, and you are responsible for disposing of them when you are finished with them. Particularly in the case of Graphics objects.
I'd strongly suggest that you use a
using
block if you are going to call
CreateGraphics
:
using (Graphics g = this.CreateGraphics())
{
... use g here ...
}
... g is automatically discarded here
In fact, you can speed the process up, and get exactly the same results without any bitmaps:
private void T_Tick(object sender, EventArgs e)
{
using (Graphics g = myPictureBox.CreateGraphics())
{
g.CopyFromScreen(MousePosition.X - 100, MousePosition.Y - 10, 0, 0, new Size(300, 300));
}
}
But ... the "keyboard cursor" doesn't exist; the caret that shows when the user can type isn't the same as the mouse cursor - and the most important difference is that there isn't just one for the whole system, every user input control has it's own caret, but only the focussed one is actually displayed.
So to find out when it moved you'd basically have to hook into every single input control in the system, and that's extremely difficult in a managed language like C# (because you don't have direct access to the OS)
I'd suggest that you will have to invest a lot of time into getting that working, and even then ... there will be apps where your code doesn't work because they don't use "standard controls" of any form. So it won;t necessarily be reliable...