图形对象的转换不会影响WM_PRINT调用

本文关键字:WM PRINT 调用 影响 对象 转换 图形 | 更新日期: 2023-09-27 17:56:50

我目前正在使用 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 命令而不是控件。DrawToBitmap() 作为 DrawToBitmap 方法不处理屏幕外的控件。

代码将正确地将蓝线的绘制转换为 50,50,但控件呈现在左上角 (0,0)。有什么方法可以使用WM_PRINT命令打印到特定位置(50,50)?

谢谢

图形对象的转换不会影响WM_PRINT调用

原因是

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);