Option Explicit
Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" ( _
ByVal lpszWindowName As String, _
ByVal dwStyle As Long, _
ByVal x As Long, ByVal Y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hWndParent As Long, _
ByVal nID As Long) As Long
Private Declare Function DestroyWindow Lib "user32" ( _
ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Const WS_CHILD As Long = &H40000000
Private Const WS_VISIBLE As Long = &H10000000
Private Const WM_USER As Long = &H400
Private Const WM_CAP_START As Long = WM_USER
Private Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
Private Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
Private Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25
Private Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
Private Const WM_CAP_DLG_VIDEOSOURCE As Long = WM_CAP_START + 42
Private Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
Private Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
Private hCap As Long
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdSettings_Click()
Call SendMessage(hCap, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
PicWebCam.BackColor = &HE0E0E0
End Sub
Private Sub cmdGetPicture_Click()
On Error GoTo ErrHandler
Dim sFileName As String
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(False), 0&)
sFileName = App.Path & "\pic.jpg"
Call SendMessage(hCap, WM_CAP_FILE_SAVEDIB, 0&, ByVal CStr(sFileName))
DoFinally:
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
Unload Me
Exit Sub
ErrHandler:
If Err.Number <> 32755 Then 'User did not clicked on cancel.
MsgBox Err.Description
End If
Resume DoFinally
End Sub
Private Sub cmdSource_Click()
Call SendMessage(hCap, WM_CAP_DLG_VIDEOSOURCE, 0&, 0&)
End Sub
Private Sub Form_Load()
hCap = capCreateCaptureWindow("Take a Camera Shot", WS_CHILD Or WS_VISIBLE, 0, 0, PicWebCam.Width, PicWebCam.Height, PicWebCam.hWnd, 0)
If hCap <> 0 Then
Call SendMessage(hCap, WM_CAP_DRIVER_CONNECT, 0, 0)
Call SendMessage(hCap, WM_CAP_SET_PREVIEWRATE, 66, 0&)
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
Call SendMessage(hCap, WM_CAP_DRIVER_DISCONNECT, 0, 0)
If hCap <> 0 Then DestroyWindow (hCap)
Set frmCapPic = Nothing
End Sub
Private Sub Form_Load()
hCap = capCreateCaptureWindow("Take a Camera Shot", WS_CHILD Or WS_VISIBLE, 0, 0, PicWebCam.Width, PicWebCam.Height, PicWebCam.hWnd, 0)
If hCap <> 0 Then
Call SendMessage(hCap, WM_CAP_DRIVER_CONNECT, 0, 0)
Call SendMessage(hCap, WM_CAP_SET_PREVIEWRATE, 66, 0&)
Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End If
End Sub
สำคัญที่ method นี้ครับ คุณเห็น hCap มั้ย มันคือ windows handle คุณต้อง แยก method นี้ออกจาก form load แล้วสั่งสร้าง windows handle เพิ่มขึ้นมา