各位好,小弟最近想写一个截取屏幕的小软件,但以前没有相关的经历,心中有几个疑问想烦请各位解答。
1. 是不是要用到Windows API,记得大学里学过,依稀还记得一点。
2. 现在出来了那么多花里胡哨的东西,难道我想实现这个功能一定要用Windows API么?
3. 新的Virtual Studio里面没有可以参考的类或插件可以实现么?或者可以基于插件作二次开发之类的?谢谢!

解决方案 »

  1.   

    http://blog.vckbase.com/zaboli/archive/2005/01/24/2746.html
      

  2.   

    BOOL CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
    {
        HDC         hScrDC, hMemDC;         // screen DC and memory DC
        HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
        int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
        int         nWidth, nHeight;        // DIB width and height
        int         xScrn, yScrn;           // screen resolution
    BITMAPINFO  InitHeader; memcpy(&InitHeader,pHeader,sizeof(BITMAPINFO));    // check for an empty rectangle
        if (IsRectEmpty(lpRect))
          return FALSE;    // create a DC for the screen and create
        // a memory DC compatible to screen DC   
        hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);//Intel(R) 82845G Graphics Controller
    //  hScrDC = CreateDC(TEXT("DISPLAY"),TEXT("Intel(R) 82845G Graphics Controller")/* TEXT("NetMeeting driver")*/, NULL, NULL);    hMemDC = CreateCompatibleDC(hScrDC);    // get points of rectangle to grab
        nX  = lpRect->left;
        nY  = lpRect->top;
        nX2 = lpRect->right;
        nY2 = lpRect->bottom;    // get screen resolution
        xScrn = GetDeviceCaps(hScrDC, HORZRES);
        yScrn = GetDeviceCaps(hScrDC, VERTRES);    //make sure bitmap rectangle is visible
        if (nX < 0)
            nX = 0;
        if (nY < 0)
            nY = 0;
        if (nX2 > xScrn)
            nX2 = xScrn;
        if (nY2 > yScrn)
            nY2 = yScrn;    nWidth  = nX2 - nX;
        nHeight = nY2 - nY; InitHeader.bmiHeader.biWidth=nWidth;
    InitHeader.bmiHeader.biHeight=nHeight;
    InitHeader.bmiHeader.biSizeImage=GetBitmapSize(&InitHeader.bmiHeader);    // create a bitmap compatible with the screen DC
        hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);    // select new bitmap into memory DC
        hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);    // bitblt screen DC to memory DC
        BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY); //Get curser and draw to capture bitmap
    HCURSOR hCursor=LoadCursor(NULL,IDC_ARROW);//=GetCursor();
    POINT ptCursor;
    GetCursorPos(&ptCursor);
    ICONINFO IconInfo; 
    if (GetIconInfo(hCursor, &IconInfo))
    {
    ptCursor.x -= ((int) IconInfo.xHotspot);
    ptCursor.y -= ((int) IconInfo.yHotspot);
    if (IconInfo.hbmMask != NULL)
    DeleteObject(IconInfo.hbmMask);
    if (IconInfo.hbmColor != NULL)
    DeleteObject(IconInfo.hbmColor);
    }
    DrawIconEx(hMemDC,ptCursor.x,ptCursor.y,hCursor,0,0,0,NULL,DI_NORMAL|DI_COMPAT);    // select old bitmap back into memory DC and get handle to
        // bitmap of the screen   
        hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, &InitHeader, DIB_RGB_COLORS);
    // clean up
        DeleteDC(hScrDC);
        DeleteDC(hMemDC); // return handle to the bitmap
        DeleteObject(hBitmap);
        return TRUE;}