Skip to content

Latest commit

 

History

History
133 lines (105 loc) · 5.11 KB

sample_091.md

File metadata and controls

133 lines (105 loc) · 5.11 KB

主页

如何使用 Bitmap API 函数将表单的图像复制到剪贴板上

翻译:xinjie 2021.01.20

开始之前:

此示例代码显示了如何以CF_BITMAP格式将活动表单的图像复制到剪贴板。

有一个类似的例子,使用 Bitmap API 函数复制 CF_ENHMETAFILE 格式的数据:
使用增强的 Metafile API 函数将活动表单的图片复制到剪贴板

查看其它 Bitmap API 示例:


代码:

#DEFINE CF_BITMAP 2	&& clipboard format
#DEFINE SRCCOPY 0xCC0020  && BitBlt raster operation code
DO declare

PRIVATE hWindow, lnLeft, lnTop, lnRight, lnBottom, lnWidth, lnHeight
*hWindow = GetActiveWindow()  && active window
hWindow = GetFocus()  && a window with keyboard focus
*hWindow = Application.hWnd
*hWindow = _screen.HWnd

* 检索窗口的几何参数 
STORE 0 TO lnLeft, lnTop, lnRight, lnBottom, lnWidth, lnHeight
= GetRect(@lnLeft, @lnTop, @lnRight, @lnBottom, @lnWidth, @lnHeight)

* 和它的设备上下文
hdc = GetWindowDC(m.hWindow)

* 创建兼容的 DC 和 BITMAP
hVdc = CreateCompatibleDC(hdc)
hBitmap = CreateCompatibleBitmap(hdc, lnWidth, lnHeight)
= SelectObject(hVdc, hBitmap)  && 选择创建的BITMAP到hVdc

* 将一个矩形区域从HDC复制到hVdc
= BitBlt(hVdc, 0,0, lnWidth,lnHeight, hdc, 0,0, SRCCOPY)

* 打开剪贴板并将位图数据放置在它上面
= OpenClipboard(m.hWindow)
= EmptyClipboard()
= SetClipboardData(CF_BITMAP, m.hBitmap)
= CloseClipboard()  && clipboard not closed may affect other apps

* 释放 GDI 对象
= DeleteObject(m.hBitmap)
= DeleteDC(m.hVdc)
= ReleaseDC(m.hWindow, m.hdc)
* 主程序结束

PROCEDURE declare
	DECLARE INTEGER GetWindowRect IN user32 INTEGER hWindow, STRING @lpRect
	DECLARE INTEGER SelectObject IN gdi32 INTEGER hdc, INTEGER hObject
	DECLARE INTEGER ReleaseDC IN user32 INTEGER hWindow, INTEGER hdc
	DECLARE INTEGER CreateCompatibleDC IN gdi32 INTEGER hdc
	DECLARE INTEGER DeleteObject IN gdi32 INTEGER hObject
	DECLARE INTEGER DeleteDC IN gdi32 INTEGER hdc
	DECLARE INTEGER CloseClipboard IN user32
	DECLARE INTEGER GetFocus IN user32
	DECLARE INTEGER GetActiveWindow IN user32
	DECLARE INTEGER EmptyClipboard  IN user32
	DECLARE INTEGER GetWindowDC IN user32 INTEGER hWindow
	DECLARE INTEGER OpenClipboard IN user32 INTEGER hWindow
	DECLARE INTEGER SetClipboardData IN user32 INTEGER wFormat, INTEGER hMem

	DECLARE INTEGER CreateCompatibleBitmap IN gdi32;
		INTEGER hdc, INTEGER nWidth, INTEGER nHeight

	DECLARE INTEGER BitBlt IN gdi32;
		INTEGER hDestDC, INTEGER x, INTEGER y,;
		INTEGER nWidth, INTEGER nHeight, INTEGER hSrcDC,;
		INTEGER xSrc, INTEGER ySrc, INTEGER dwRop

PROCEDURE GetRect(lnLeft, lnTop, lnRight, lnBottom, lnWidth, lnHeight)
    LOCAL lpRect
    lpRect = REPLICATE(CHR(0), 16)
    = GetWindowRect(m.hWindow, @lpRect)

    lnLeft = buf2dword(SUBSTR(lpRect,  1,4))
    lnTop = buf2dword(SUBSTR(lpRect,  5,4))
    lnRight = buf2dword(SUBSTR(lpRect,  9,4))
    lnBottom = buf2dword(SUBSTR(lpRect, 13,4))
	lnWidth = lnRight - lnLeft
	lnHeight = lnBottom - lnTop

FUNCTION buf2dword(lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
	Asc(SUBSTR(lcBuffer, 2,1)) * 0x100 +;
	Asc(SUBSTR(lcBuffer, 3,1)) * 0x10000 +;
	Asc(SUBSTR(lcBuffer, 4,1)) * 0x1000000  

函数列表:

BitBlt
CloseClipboard
CreateCompatibleBitmap
CreateCompatibleDC
DeleteDC
DeleteObject
EmptyClipboard
GetActiveWindow
GetFocus
GetWindowDC
GetWindowRect
OpenClipboard
ReleaseDC
SelectObject
SetClipboardData

备注:

GetFocus函数检索拥有键盘焦点的窗口的句柄。

如果你从一个窗体中调用这个过程,你将在剪贴板上得到这个窗体的图像。

在没有其他窗体运行的情况下,从VFP主窗口调用这个过程,可以将整个主窗口本身复制到剪贴板上。


微软帮助和支持页面上发表的一篇文章 How To Copy the Screen or Active Window to the Clipboard from Visual Basic 描述了一种不同的方法--通过虚拟按下(通过keybd_event调用)PrtScr键将整个屏幕的图片发送到剪贴板。