Source code: http://download.csdn.net/detail/nuptboyzhb/4136686
Add header files and global variables:
# Include <math. h> // header file of the mathematical function library
# Include <VFW. h> // Add the header file of the AVI video processing function.
# Pragma comment (Lib, "vfw32.lib") // Connection Library vfw32.lib
# Pragma comment (Lib, "winmm. lib") // link library
// Define the struct variable required by the AVI Video Processing Function
Avistreaminfo strhdr;
Pavifile pfile;
Pavistream pS;
Pavistream pcomstream;
Avicompressoptions pcompressoption;
Avicompressoptions far * opts [1] ={& pcompressoption };
Hresult hr;
Int nframes = 0; // defines the number of frames in the video.
Bool m_timer = true; // indicates the running status of the timer.
Uint timer_num = 0; // mark the timer name
Bool m_ispause = false; // whether to suspend the flag
Added the ontimer timer message.
// Todo: add your message handler code here and/or call default
If (m_timer) // if the last Timer Program has been run, the following code is executed:
{
M_timer = false; // This timer is running. You cannot run it again before it is finished.
CDC * pdeskdc = getdesktopwindow ()-> getdc (); // obtain the desktop canvas object
Crect RC;
Getclienttopwindow ()-> getclientrect (RC); // obtain the client region of the screen.
CDC memdc; // defines a memory canvas
Memdc. createcompatibledc (p1_dc); // create a compatible canvas
Cbitmap BMP;
BMP. createcompatiblebitmap (p1_dc, RC. Width (), RC. Height (); // create a compatible bitmap
Memdc. SelectObject (& BMP); // select a bitmap object
Bitmap bitmap;
BMP. getbitmap (& Bitmap );
Memdc. bitblt (, bitmap. bmwidth, bitmap. bmheight, p1_dc, srccopy );
DWORD size = bitmap. bmwidthbytes * bitmap. bmheight; // The total number of pixels of the screen image
Byte * lpdata = new byte [size]; // apply for a byte array of size for storing desktop image data
Int panelsize = 0; // record the color palette size
If (bitmap. bmbitspixel <16) // determines whether the color bitmap is true.
Panelsize = (INT) Pow (2, bitmap. bmbitspixel * sizeof (rgbquad ));
Bitmapinfoheader * pbinfo = new bitmapinfoheader; // defines the Bitmap header structure.
// Initialize the bitmap information Header
Pbinfo-> bibitcount = bitmap. bmbitspixel;
Pbinfo-> biclrimportant = 0;
Pbinfo-> bicompression = 0;
Pbinfo-> biheight = bitmap. bmheight;
Pbinfo-> biplanes = bitmap. bmplanes;
Pbinfo-> bisize = sizeof (bitmapinfoheader );
Pbinfo-> bisizeimage = bitmap. bmwidthbytes * bitmap. bmheight;
Pbinfo-> biwidth = bitmap. bmwidth;
Pbinfo-> bixpelspermeter = 0;
Pbinfo-> biypelspermeter = 0;
Bitmapinfo binfo; // defines the bitmap information structure.
Binfo. bmiheader = * pbinfo; // Initialization
Getdibits (memdc. m_hdc, BMP, 0, pbinfo-> biheight, lpdata, & binfo, dib_rgb_colors );
// Save image data to pdata
If (nframes = 0) // if it is the first frame
{
Cstring rate = "3"; // 3 frames
Cstring filename = "111.avi"; // file name
Avifileopen (& pfile, filename, of_write | of_create, null); // open or create an AVI file
Memset (& strhdr, 0, sizeof (strhdr); // The initialization information header is 0.
// Initialize the AVI Video Stream Information Structure
Strhdr. fcctype = streamtypevideo;
Strhdr. fcchandler = 0;
Strhdr. dwscale = 1;
Strhdr. dwrate = atoi (rate );
Strhdr. dwsuggestedbuffersize = pbinfo-> bisizeimage;
Setrect (& strhdr. rcframe, 0, pbinfo-> biwidth, pbinfo-> biheight );
HR = avifilecreatestream (pfile, & PS, & strhdr); // create an AVI file stream
Opts [0]-> fcctype = streamtypevideo;
Opts [0]-> fcchandler = mmiostringtofourcc ("msvc", 0 );
Opts [0]-> dwquality = 7500;
Opts [0]-> dwbytespersecond = 0;
Opts [0]-> dwflags = avicompressf_valid | avicompressf_keyframes;
Opts [0]-> lpformat = 0;
Opts [0]-> cbformat = 0;
Opts [0]-> dwinterleaveevery = 0;
Avimakecompressedstream (& pcomstream, PS, & pcompressoption, null );
Avistreamsetformat (pcomstream, 0, pbinfo, sizeof (bitmapinfoheader ));
}
HR = avistreamwrite (pcomstream, nframes, 1, (lpbyte) lpdata,
Pbinfo-> bisizeimage, aviif_keyframe, null, null); // write desktop image data to a file
Nframes ++; // Add a frame
Delete [] lpdata; // release memory
Delete pbinfo; // release memory
M_timer = true; // The timer program can continue to respond.
}
Add recording button
Timer_num = settimer (1,100, 0); // start the timer
M_timer = true; // set the timer to be available
Avifileinit (); // initialize the AVI file
Add stop button
If (timer_num)
{
Killtimer (timer_num );
}
M_timer = false;
If (pcomstream)
{
Avistreamclose (pcomstream );
}
If (PS)
{
Avistreamclose (PS );
}
If (pfile! = NULL)
Avifilerelease (pfile );
Avifileexit ();
Nframes = 0;
Add pause button
If (m_ispause = false)
{
M_ispause =! M_ispause;
If (timer_num)
{
Killtimer (timer_num );
}
}
Else
{
M_ispause =! M_ispause;
Timer_num = settimer (1,100, null );
}