0

我目前正在使用 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)?

谢谢

4

1 回答 1

0

原因是WM_PRINT使用 theDevice 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);
于 2013-08-18T10:54:26.617 回答