我目前正在使用一个WM_PRINT调用将一个控件呈现到一个图形对象中:
GraphicsState backup = graphics.Save();
graphics.TranslateTransform(50, 50);
IntPtr destHdc = graphics.GetHdc();
const int flags = (int)(DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCLIENT);
NativeMethods.SendMessage(srcControl.Handle, (Int32)WM.WM_PRINT, (IntPtr)destHdc, (IntPtr)flags);
graphics.ReleaseHdc(destHdc);
graphics.DrawLine(Pens.Blue, new Point(), new Point(srcControl.Width, srcControl.Height));
graphics.Restore(backup);我需要使用WM_PRINT命令而不是control.DrawToBitmap(),因为DrawToBitmap方法不处理屏幕外的控件。
代码将正确地将蓝色线条的绘制转换50,50,但控件呈现在左上角(0,0)。有没有什么方法可以使用WM_PRINT命令打印到特定的位置(50,50)?
谢谢
发布于 2013-08-18 18:54:27
原因是WM_PRINT使用Device context,而不是通过graphics,因此translate transform不受影响。它只受graphics上调用的绘图方法的影响。以下是一种解决方法:
GraphicsState backup = graphics.Save();
Bitmap bm = new Bitmap(srcControl.Width, srcControl.Height);
Graphics g = Graphics.FromImage(bm);
IntPtr destHdc = g.GetHdc();
const int flags = (int)(DrawingOptions.PRF_CHILDREN | DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCLIENT);
NativeMethods.SendMessage(srcControl.Handle, (Int32)WM.WM_PRINT, destHdc, (IntPtr)flags);
g.ReleaseHdc(destHdc);
graphics.DrawImage(bm, new Point(50,50));
graphics.DrawLine(Pens.Blue, new Point(), new Point(srcControl.Width, srcControl.Height));
graphics.Restore(backup);https://stackoverflow.com/questions/14617487
复制相似问题