视线语音鼠标 2 C#视频捕获

要引用system32中的avicap32.dll和user32.dll

四个函数的意思分别为设置捕获窗口,关闭窗口,发送消息,和设置窗口位置。

我用panel显示视频,button和1button2分别控制视频的开关,这里有个小bug,有时会蹦出视频源那个窗口,要快速的多点几次才行。不知是怎么一回事,知道的一定要告诉我一声!

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace AutMouse { public partial class Form1 : Form { public Form1() { InitializeComponent(); } 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; } [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("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); private int hHwnd; private void OpenCapture() { int intWidth = this.panel1.Width; int intHeight = this.panel1.Height; int intDevice = 0; string refDevice = intDevice.ToString(); //创建视频窗口并得到句柄 hHwnd = Form1.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.panel1.Handle.ToInt32(), 0); if (Form1.SendMessage(hHwnd, 0x40a, intDevice, 0) > 0) { Form1.SendMessage(this.hHwnd, 0x435, -1, 0); Form1.SendMessage(this.hHwnd, 0x434, 0x42, 0); Form1.SendMessage(this.hHwnd, 0x432, -1, 0); Form1.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6); } else { Form1.DestroyWindow(this.hHwnd); } } private void button1_Click(object sender, EventArgs e) { //运行视频开始 this.OpenCapture(); button1.Enabled = false; button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { //停止视频注销视频句柄 Form1.SendMessage(this.hHwnd, 0x40b, 0, 0); Form1.DestroyWindow(this.hHwnd); button2.Enabled = false; button1.Enabled = true; } } }

你可能感兴趣的:(String,object,struct,user,C#,button)