大虾们:好!我最近想做一个截屏的软件,但因我是个新手,不知从何入手,现非常的
烦恼.请大家们帮忙了!

解决方案 »

  1.   

    1)获取屏幕DC得到屏幕DC,多种方法:
    CDC *pDC = GetDC(NULL);
    CDC *pDC = CDC::FromHandle(::GetDC(::GetDesktopWindow()));
    CClientDC dc(NULL);2)画图
    有了屏幕DC之后,就可以画出来了.有关代码可参考
    http://expert.csdn.net/Expert/topic/649/649984.xml?temp=.5050318
      

  2.   

    但是图象如果要在网上传输的话太大了
    用MPEG1压缩的话  具体的算法是如何实现的呢
      

  3.   

    http://www.csdn.net/Dev/Format/应该可以找到你想要的东西
      

  4.   

    void CCaptureView::OnCaptureScreen() 
    {
    // TODO: Add your command handler code here
    CDC dc;
    dc.CreateDC("DISPLAY",NULL,NULL,NULL);
    int SrcWidth=GetSystemMetrics(SM_CXSCREEN);
    int SrcHeight=GetSystemMetrics(SM_CYSCREEN);
    // int Width=384,Height=288;
    int Width=SrcWidth,Height=SrcHeight;
    /* CDC memdc;
    memdc.CreateCompatibleDC(&dc);
    memdc.StretchBlt(0,0,Width,Height,&dc,0,0,SrcWidth,SrcHeight,SRCCOPY);
    */
    CBitmap bm;
    bm.CreateCompatibleBitmap(&dc,Width,Height);
    CDC tdc;
    tdc.CreateCompatibleDC(&dc);
    CBitmap*pOld=tdc.SelectObject(&bm);
    tdc.SetStretchBltMode(STRETCH_HALFTONE);
    tdc.StretchBlt(0,0,Width,Height,&dc,0,0,SrcWidth,SrcHeight,SRCCOPY);
    // tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY);
    tdc.SelectObject(pOld);
    BITMAP btm;
    bm.GetBitmap(&btm);
    DWORD size=btm.bmWidthBytes*btm.bmHeight;
    LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
    /////////////////////////////////////////////
    BITMAPINFOHEADER bih;
    bih.biBitCount=btm.bmBitsPixel;
    bih.biClrImportant=0;
    bih.biClrUsed=0;
    bih.biCompression=0;
    bih.biHeight=btm.bmHeight;
    bih.biWidth=btm.bmWidth;
    bih.biPlanes=1;
    bih.biSize=sizeof(BITMAPINFOHEADER);
    bih.biSizeImage=size;
    bih.biXPelsPerMeter=0;
    bih.biYPelsPerMeter=0;
    ///////////////////////////////////
    GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
    // bm.GetBitmapBits(size,lpData); //此函数在处理5-5-5模式的16位色下会出现颜色混乱
    //////////////////////////////
    static int filecount=0;
    BITMAPFILEHEADER bfh;
    bfh.bfReserved1=bfh.bfReserved2=0;
    bfh.bfType=((WORD)('M'<< 8)|'B');
    bfh.bfSize=54+size;
    bfh.bfOffBits=54;
    CFile bf;
    if(bf.Open("capture.bmp",CFile::modeCreate|CFile::modeWrite)){
    bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
    bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
    bf.WriteHuge(lpData,size);
    bf.Close();
    // nCount++;
    }
    GlobalFreePtr(lpData);
    UpdateData(FALSE);
    }void CCaptureView::OnCaptureScreen1() 
    {
    // TODO: Add your command handler code here
    char filename[30]="capture1.bmp";
    HDC hdc;
    HDC hdc2;
    DWORD dwWidth, dwHeight, dwBPP, dwNumColors;
    HBITMAP bitmap;
    HGDIOBJ gdiobj;
    LPVOID pBits;
    BITMAPFILEHEADER bitmapfileheader;
    BITMAPINFOHEADER bitmapinfoheader; hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
    if(hdc==NULL) {
    AfxMessageBox("Couldn't create device context.\n");
    return ;
    }

    dwWidth = GetDeviceCaps(hdc, HORZRES);
    dwHeight = GetDeviceCaps(hdc, VERTRES);
    dwBPP = GetDeviceCaps(hdc, BITSPIXEL); if(dwBPP<=8) {
    dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
    // dwNumColors = 256;
    } else {
    dwNumColors = 0;
    }
    CString temp;
    temp.Format("%d",dwNumColors);
    AfxMessageBox(temp); hdc2=CreateCompatibleDC(hdc);
    if(hdc2==NULL) {
    DeleteDC(hdc);
    AfxMessageBox("Couldn't create compatible device context.\n");
    return ;
    }
    bitmapfileheader.bfType = 0x4D42;
    bitmapfileheader.bfSize = ((dwWidth * dwHeight * dwBPP)/8) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));
    bitmapfileheader.bfReserved1 = 0;
    bitmapfileheader.bfReserved2 = 0;
    bitmapfileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));  

    bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapinfoheader.biWidth = dwWidth;
    bitmapinfoheader.biHeight = dwHeight;
    bitmapinfoheader.biPlanes = 1;
    bitmapinfoheader.biBitCount = (WORD)dwBPP;
    bitmapinfoheader.biCompression = BI_RGB;
    bitmapinfoheader.biSizeImage = 0;
    bitmapinfoheader.biXPelsPerMeter = 0;
    bitmapinfoheader.biYPelsPerMeter = 0;
    bitmapinfoheader.biClrUsed = dwNumColors;
    bitmapinfoheader.biClrImportant = 0; bitmap = CreateDIBSection(hdc, (BITMAPINFO *)&bitmapinfoheader, DIB_PAL_COLORS, &pBits, NULL, 0);
    if(bitmap==NULL) {
    DeleteDC(hdc);
    DeleteDC(hdc2);
    AfxMessageBox("Couldn't create compatible bitmap.\n");
    return ;
    }
    gdiobj = SelectObject(hdc2, (HGDIOBJ)bitmap);
    if((gdiobj==NULL) || (gdiobj==(void *)GDI_ERROR)) {
    DeleteDC(hdc);
    DeleteDC(hdc2);
    AfxMessageBox("Couldn't select bitmap.\n");
    return ;
    }
    if (!BitBlt(hdc2, 0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY)) {
    DeleteDC(hdc);
    DeleteDC(hdc2);
    AfxMessageBox("Couldn't copy bitmap.\n");
    return ;
    }

    RGBQUAD colors[256];
    if(dwNumColors!=0) {
    dwNumColors = GetDIBColorTable(hdc2, 0, dwNumColors, colors);
    }

    HANDLE hfile;
    DWORD dwBytes;

    hfile=CreateFile(filename,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hfile==INVALID_HANDLE_VALUE) {
    DeleteObject(bitmap);
    DeleteDC(hdc2);
    DeleteDC(hdc);
    AfxMessageBox("Couldn't write file to disk.\n");
    return ;
    }
    WriteFile(hfile,&bitmapfileheader,sizeof(BITMAPFILEHEADER), &dwBytes, NULL);
    WriteFile(hfile,&bitmapinfoheader,sizeof(BITMAPINFOHEADER), &dwBytes, NULL);
    if(dwNumColors!=0)
    WriteFile(hfile,colors,sizeof(RGBQUAD)*dwNumColors,&dwBytes,NULL);
    WriteFile(hfile,pBits,(dwWidth*dwHeight*dwBPP)/8,&dwBytes,NULL);
    CloseHandle(hfile);


    DeleteObject(bitmap);
    DeleteDC(hdc2);
    DeleteDC(hdc);

    return ;
    }