VB BitBlt函数抓取屏幕配合滚动条设计问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 19:25:14
我想用Picture控件显示抓取的屏幕,然后多出Picture控件的部分利用滚动条观看,我这个程序的问题就出在无法用滚动条实现,我明明将P2.AutoSize = True,但抓取屏幕时图片框根本和p2.AutoSize =False 效果一样答!请高手帮忙解答! Picture1=P1,Picture2=P2
API函数声明:
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

代码:
Private Sub Form_Load()
P2.AutoSize = True
End Sub

Private Sub HS1_Change()
P2.Left = -HS1.Value
End Sub

Private Sub VS1_Change()
P2.Top = -VS1.Value
End Sub

Private Sub z_Click()
Const SRCCOPY = &HCC0020
Dim hdc As Long, r As Long
nWidth = Screen.Width \ Screen.TwipsPe

当您对一个图象使用 BitBlt() 时,PictureBox 控件不知道象您使用 LoadPicture方法那样去调整大小。所以你必须自己编写代码来控制。另外,必须设置picturebox的autoredraw属性为TRUE,否则在移动滚动条的时候,会擦除移出视线外的图像,等你移回来就会是一片空白。
调整大小的代码:
Private Sub z_Click()

Const SRCCOPY = &HCC0020
Dim hdc As Long, r As Long
nWidth = Screen.Width \ Screen.TwipsPerPixelX
nHeight = Screen.Height \ Screen.TwipsPerPixelY

P2.Move 0, 0, nWidth * Screen.TwipsPerPixelX, nHeight * Screen.TwipsPerPixelY '新添加

hdc = GetDC(0)
r = BitBlt(P2.hdc, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY)

P2.Refresh '新添加

r = ReleaseDC(0, hdc)
h = P2.Height - P1.Height
v = P2.Width - P1.Width
HS1.Max = IIf(v > 0, v, 0)
VS1.Max = IIf(h > 0, h, 0)
End Sub