利用PrintWindow打印后台窗口后遇到的疑问

取昵称麻烦 2010-08-20 03:45:00
PrintWindow只能用于窗口,不能打印控件。
如果我先把整个窗口打印到一个Bitmap中,再从中复制控件区域,或者指定区域的图像,请问怎么做?
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
取昵称麻烦 2010-08-22
  • 打赏
  • 举报
回复
唉,没落了,没人知道
取昵称麻烦 2010-08-22
  • 打赏
  • 举报
回复
谢谢你的回复
kye_jufei 2010-08-22
  • 打赏
  • 举报
回复
這是C#中實現的結果,你可以使用DELPHI,用它的API聲明這個DLL並調用即可。。。

只要窗体的visable为true,即使它在屏幕的外面也可以抓到图。如果为false,就是一张黑图了,赫赫。

public   static   Bitmap   GetWindow(IntPtr   hWnd) 
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}

API声明

[DllImport( "gdi32.dll ")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name驱动名
string lpszDevice, // device name设备名
string lpszOutput, // not used; should be NULL
IntPtr lpInitData // optional printer data
);
[DllImport( "gdi32.dll ")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC目标设备的句柄
int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, // width of destination rectangle目标对象的矩形宽度
int nHeight, // height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, // handle to source DC源设备的句柄
int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop // raster operation code光栅的操作值
);

[DllImport( "gdi32.dll ")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);

[DllImport( "gdi32.dll ")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);

[DllImport( "gdi32.dll ")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);

[DllImport( "gdi32.dll ")]
public static extern int DeleteDC(
IntPtr hdc // handle to DC
);

[DllImport( "user32.dll ")]
public static extern bool PrintWindow(
IntPtr hwnd, // Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, // HDC to print into,Handle to the device context.
UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values.
);

[DllImport( "user32.dll ")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧