|
2#
楼主 |
发表于 2011-9-5 08:48:50
|
只看该作者
模块2
Public Function CaptureRect(ByVal hwnd As Long, ptOrigin As POINTAPI) As RECT
Dim hdc As Long
Dim msg As msg
Dim hPal, hBitmap As Long
Dim rcClip As RECT
Dim rcClient As RECT
' 获得屏幕设备句柄
hdc = CreateDCNull("DISPLAY ", 0, 0, 0&)
' 转换屏幕坐标
ClientToScreen hwnd, ptOrigin
' rcClip 是用户当前选择的方框
' 单用户刚刚按下左键时,将该方框初始化最小
rcClip.Left = ptOrigin.x
rcClip.Right = ptOrigin.x
rcClip.Top = ptOrigin.y
rcClip.Bottom = ptOrigin.y
' 确保该方框为一个正常的方框
NormalizeRect rcClip
' 开始画抓取方框
DrawSelect hdc, rcClip
Do
' 等待鼠标消息
WaitMessage
If (PeekMessage(msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE)) Then
' 擦除前一个方框
DrawSelect hdc, rcClip
' 获得新坐标
rcClip.Left = ptOrigin.x
rcClip.Top = ptOrigin.y
rcClip.Right = msg.pt.x
rcClip.Bottom = msg.pt.y
' 正常化方框
NormalizeRect rcClip
' 画新的方框
DrawSelect hdc, rcClip
'如果是左键弹起消息则退出循环
If (msg.message = WM_LBUTTONUP) Then Exit Do
End If
' 下一个消息
Loop
' 清除最后一个方框
DrawSelect hdc, rcClip
' 清除设备
DeleteDC hdc
' 释放捕捉
ReleaseCapture
' 设置方框
CaptureRect = rcClip
' 向源窗体发送ButtonUp消息,以便让程序知道处理已经完成
PostMessage hwnd, WM_LBUTTONUP, msg.wParam, msg.lParam
End Function
' DrawSelect
' 按照剪切方框的尺寸绘制剪切方框
Public Function DrawSelect(hdc As Long, rcClip As RECT)
Dim hdcBits As Long ' DWORD
Dim x, y As Long
Dim dx, dy As Long
Dim hbm As Long ' HBITMAP
Dim sExtent As Size ' SIZE
Dim sz As String
Dim psz As Long ' Size sz
' 如果一个剪切方框已经被选择则绘制它
If Not IsRectEmpty(rcClip) Then
' PatBlt 函数用选择的画笔向指定设备绘制给定的方框
' 画笔的颜色和表面颜色由给定的光栅操作决定
' BOOL PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop)
PatBlt hdc, rcClip.Left, rcClip.Top, _
rcClip.Right - rcClip.Left, 1, DSTINVERT
PatBlt hdc, rcClip.Left, rcClip.Bottom, _
1, -(rcClip.Bottom - rcClip.Top), DSTINVERT
PatBlt hdc, rcClip.Right - 1, rcClip.Top, _
1, rcClip.Bottom - rcClip.Top, DSTINVERT
PatBlt hdc, rcClip.Right, rcClip.Bottom - 1, _
-(rcClip.Right - rcClip.Left), 1, DSTINVERT
sz = CStr(rcClip.Right - rcClip.Left) & "x " & _
CStr(rcClip.Bottom - rcClip.Top)
GetTextExtentPoint32 hdc, sz, Len(sz), sExtent
dx = sExtent.cx
dy = sExtent.cy
x = (rcClip.Right + rcClip.Left - dx) / 2
y = (rcClip.Bottom + rcClip.Top - dy) / 2
hdcBits = CreateCompatibleDC(hdc)
SetTextColor hdcBits, &HFFFFFF
SetBkColor hdcBits, &H0
' 向设备输出文本
hbm = CreateBitmap(dx, dy, 1, 1, 0&)
If (hbm) Then
hbm = SelectObject(hdcBits, hbm)
TextOut hdcBits, 0, 0, sz, Len(sz)
BitBlt hdc, x, y, dx, dy, hdcBits, 0, 0, SRCINVERT
' 恢复原有句柄
hbm = SelectObject(hdcBits, hbm)
DeleteObject hbm
End If
DeleteDC hdcBits
End If
End Function
' 确保该方框为一个正常的方框
' 目的: 如果方框坐标被颠倒了就交换它们
' 这样作的目的是保证第一点为方框的左上角点
' 第二点为方框的右下角点
Public Sub NormalizeRect(ByRef Rct As RECT)
If (Rct.Right < Rct.Left) Then Swap Rct.Right, Rct.Left
If (Rct.Bottom < Rct.Top) Then Swap Rct.Bottom, Rct.Top
End Sub
' 交换
Public Sub Swap(ByRef x As Long, ByRef y As Long)
x = x Xor y
y = x Xor y
x = x Xor y
End Sub
' 拷贝选定方框区域的屏幕图像到剪贴板
Public Sub ScrnCap(Left As Long, Top As Long, Right As Long, Bottom As Long)
Dim rWidth As Long
Dim rHeight As Long
Dim SourceDC As Long
Dim DestDC As Long
Dim BHandle As Long
Dim Wnd As Long
Dim DHandle As Long
rWidth = Right - Left
rHeight = Bottom - Top
SourceDC = CreateDC("DISPLAY ", 0, 0, 0)
DestDC = CreateCompatibleDC(SourceDC)
BHandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight)
SelectObject DestDC, BHandle
BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Left, Top, &HCC0020
Wnd = Screen.ActiveForm.hwnd
OpenClipboard Wnd
EmptyClipboard
SetClipboardData 2, BHandle
CloseClipboard
DeleteDC DestDC
ReleaseDC DHandle, SourceDC
End Sub
|
|