Get plain text from SunAwtCanvas

1k Views Asked by At

Since it's impossible to capture text from a java canvas by using standard Windows API functions like GetMessage(), I wondered if there is any way to hook the drawText() method inside such a java canvas and to capture the text before it's actually have been drawn on the canvas. Does anyone have an idea how to solve this? A solution written in java would also be accepted. Any suggestion would be nice, thank you.

Edit: Is it possible to steal the foreign canvas handle and set it to my own canvas? So the foreign application wouldn't draw anymore to its canvas but to my spoofed one. By this way I could override the drawText() method easily in my application.

2

There are 2 best solutions below

3
On BEST ANSWER

Use Java Instrumentation to inject code into drawString(), for example with Javassist.

You can combine code from here to inject your code with insertBefore and there to activate the transformation in the premain method.

1
On

It would be easy enough to write your own subclass of Graphics2D and have the drawing done on that; your subclass could override drawString() to do whatever you'd like. I would probably write the implementation as a wrapper which forwarded all method calls to another instance specified as a constructor argument, as that way your subclass could be used to directly write to the screen. Be sure to implement create() to return a new instance of your subclass, of course.