gpt4 book ai didi

c# - 掌上电脑 : Draw control to bitmap

转载 作者:太空宇宙 更新时间:2023-11-03 20:41:53 31 4
gpt4 key购买 nike

我试图使用 C# 将控件实例(比如面板或按钮)绘制到我的 Pocket PC 应用程序中的位图中。 .NET 控件具有漂亮的 DrawToBitmap 函数,但它在 .NET Compact Framework 中不存在。

我将如何在 Pocket PC 应用程序中为图像绘制控件?

最佳答案

DrawToBitmap 在完整框架中通过发送 WM_PRINT 来工作到控件的消息,以及要打印到的位图的设备上下文。 Windows CE 不包含 WM_PRINT,因此该技术不起作用。

如果正在显示您的控件,您可以从屏幕上复制控件的图像。以下代码使用此方法将兼容的 DrawToBitmap 方法添加到 Control:

public static class ControlExtensions
{
[DllImport("coredll.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("coredll.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll.dll")]
private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hdcSrc,
int nXSrc, int nYSrc, uint dwRop);

private const uint SRCCOPY = 0xCC0020;

public static void DrawToBitmap(this Control control, Bitmap bitmap,
Rectangle targetBounds)
{
var width = Math.Min(control.Width, targetBounds.Width);
var height = Math.Min(control.Height, targetBounds.Height);

var hdcControl = GetWindowDC(control.Handle);

if (hdcControl == IntPtr.Zero)
{
throw new InvalidOperationException(
"Could not get a device context for the control.");
}

try
{
using (var graphics = Graphics.FromImage(bitmap))
{
var hdc = graphics.GetHdc();
try
{
BitBlt(hdc, targetBounds.Left, targetBounds.Top,
width, height, hdcControl, 0, 0, SRCCOPY);
}
finally
{
graphics.ReleaseHdc(hdc);
}
}
}
finally
{
ReleaseDC(control.Handle, hdcControl);
}
}
}

关于c# - 掌上电脑 : Draw control to bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2292391/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com