110,533
社区成员
发帖
与我相关
我的任务
分享
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
public partial class Form1 : Form
{
#region API
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool DestroyWindow(int hndw);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("vfw32.dll")]
public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
[DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capSetCallbackOnFrame(int hwnd, string s);
#endregion
public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved;
}
IntPtr vhandle;
private const int WM_CAP_EDIT_COPY = 0x41E;
private const int WM_CAP_SET_SCALE = 0x435;
private const int WM_CAP_SET_PREVIEWRATE = 0x434;
private const int WM_CAP_SET_PREVIEW = 0x432;
private void OpenCapture()
{
int intWidth = 160;
int intHeight = 120;
int intDevice = 0;
string refDevice = intDevice.ToString();
hHwnd = capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 160, 120, vhandle.ToInt32(), 0);
//this.LbSysMsg.Text = "";
//this.LbSysMsg.Text += "驱动:" + refDevice;
if (SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
{
SendMessage(this.hHwnd, WM_CAP_SET_SCALE, -1, 0);
SendMessage(this.hHwnd, WM_CAP_SET_PREVIEWRATE, 0x42, 0);
SendMessage(this.hHwnd, WM_CAP_SET_PREVIEW, -1, 0);
SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
}
else
{
DestroyWindow(this.hHwnd);
}
}
private int hHwnd;
public Form1()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
vhandle = this.Handle;
OpenCapture();
}
}