由于只是为了实现功能,所以界面上没有任何的button之类,所有的代码都完成在构造函数中.
#include
#include
#include
const static int WM_CAP_START=WM_USER;
const static int WM_CAP_STOP = WM_CAP_START + 68;
const static int WM_CAP_DRIVER_CONNECT=WM_CAP_START+10;
const static int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
const static int WM_CAP_SAVEDIB = WM_CAP_START + 25;
const static int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
const static int WM_CAP_SEQUENCE = WM_CAP_START + 62;
const static int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
const static int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;
const static int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51 ;
const static int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50 ;
const static int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;
const static int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;
const static int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;
const static int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;
const static int WM_CAP_SET_SCALE=WM_CAP_START+ 53;
const static int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;
CamViewer::CamViewer(QWidget *parent)
: QWidget(parent)
{
resize(400,300);
QLibrary mylib("avicap32");
if(mylib.load()){
// QMessageBox::information(this,tr("info"),tr("load dll OK!"));
typedef HWND(* capCreateFunc)(char *lpszWindowName,long int swStyle,
int x,int y,int nWidth,int nHeight,HWND parentWin,int nID);
capCreateFunc capCreate=(capCreateFunc)mylib.resolve("capCreateCaptureWindowA");
if(capCreate){
HWND h=capCreate("my own capture window",WS_CHILD|WS_VISIBLE,0,0,width(),height(),winId(),0);
if(h){
SendMessage(h,WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage(h,WM_CAP_SET_CALLBACK_ERROR,0,0);
SendMessage(h,WM_CAP_SET_CALLBACK_STATUSA,0,0);
SendMessage(h,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(h,WM_CAP_SET_SCALE,1,0);
SendMessage(h,WM_CAP_SET_PREVIEWRATE,66,0);
SendMessage(h,WM_CAP_SET_OVERLAY,1,0);
SendMessage(h,WM_CAP_SET_PREVIEW,1,0);
}
}
}
}
说起来也是很简单,载入avicap.dll之后,就调动里面的capCreateCaptureWindowA()函数,然后是一系列的SendMessage
运行效果图如下
其它的功能比如截图或是保存到avi文件之类的,也就是再多加几个SendMessage而已,或是再设计一个漂亮的UI
加上PushButton,没有想再继续写下去,我的目的已经达到了