views:

345

answers:

3

I need to draw a textbox in a Compact Framework app, but using directly a Graphics object.

I've found the Control.DrawToBitmap method, which I could use for drawing with GDI, but it's not available on Compact Framework.

Any hints?

A: 

Well typically you just inherit from : Control, override OnPaint and use the graphics object provided by the arguments. Have you tried this yet?

Quibblesome
With this I can draw text, lines, polygons, bitmaps, etc. But how I can get the "image" from a textbox that is not in any control?
penyaskito
Eh? You want to get the image that is drawn by the control? You cannot get this, unless you implement the control yourself and expose it via a property. You could do this by drawing the control on a graphics object created by Graphics.FromImage() then expose that image via a property before finally drawing the control by using Graphics.DrawImage on the "main" graphics object in the event args. You would have to re-implement every single control this way though so it would be a lot of effort.
Quibblesome
+1  A: 

Control.DrawToBitmap() is implemented by sending WM_PRINT to the control to let it draw itself in a memory device context. If you are using non-standard controls, the odds are great that the programmer did not implement this message. I don't think it is implemented by the standard Windows Mobile controls either.

Your only other recourse then is to copy the pixels off the screen. Graphics.CopyFromScreen() is a no-go, you'll have to P/Invoke the BitBlt() API function. Possibly useful sample code is available in this thread. and at pinvoke.net

Hans Passant
I did the BitBit p/Invoke, but got a black image (I think that's because of the controls not being visible in any form). If I use visible controls it works. Thanks anyway :(
penyaskito
Well you always make them visible but "off screen". Again not pretty....
Quibblesome
The problem is that in Windows Mobile I can only have an active screen, so puting the StartLocation out of the bounds of it it's not an option.
penyaskito
A: 

I have no idea if this would work.... but technically you could use reflection to call into the control's OnPaint method and pass in your own graphics object (Graphics.FromImage) in a new set of PaintEventArgs.

It's ugly as hell..... but it might work.

Quibblesome