【问题标题】:MFC C++ ScreenshotMFC C++ 截图
【发布时间】:2012-08-08 08:00:19
【问题描述】:

我有一个使用 CDC 绘制网格的应用程序(它有文本、矩形和位图)。我想在保存完成的网格时对其进行截图,并将该截图用作文件的“预览”。

如何截取我的应用程序的屏幕截图并保存?

谢谢,

【问题讨论】:

    标签: c++ mfc screenshot


    【解决方案1】:

    回复is here

    void CScreenShotDlg::OnPaint()
    {
        // device context for painting
        CPaintDC dc(this);
    
        // Get the window handle of calculator application.
        HWND hWnd = ::FindWindow( 0, _T( "Calculator" ));
    
        // Take screenshot.
        PrintWindow( hWnd,
                     dc.GetSafeHdc(),
                     0 );
    }
    

    【讨论】:

    • 虽然我最终使用了不同的方法来截屏。我什至需要捕获窗口的隐藏部分,而 PrintWindow 没有捕获它。
    【解决方案2】:

    最终我这样做是因为我想捕捉窗口的隐藏部分(因为内容超出了屏幕并需要滚动):

    CDC* WindowToCaptureDC = AfxGetMainWnd()->GetWindowDC();
    CDC CaptureDC;
    CDC MemDC;
    MemDC.CreateCompatibleDC(WindowToCaptureDC);
    CaptureDC.CreateCompatibleDC(WindowToCaptureDC);
    
    CBitmap CaptureBmp;
    CBitmap ResizeBmp;
    int pWidth = grid.tableWidth + grid.marginLeft*2;
    int pHeight = grid.tableHeight + grid.marginBottom; 
    
    CaptureBmp.CreateCompatibleBitmap( WindowToCaptureDC, pWidth, pHeight);
    CaptureDC.SelectObject(&CaptureBmp);
    
    CBrush brush(RGB(255, 255, 255));
    CaptureDC.SelectObject(&brush);
    CaptureDC.Rectangle(0, 0, pWidth, pHeight);
    

    ///像我在此处为 OnDraw 所做的那样将项目绘制到 CaptureDC///

    double width = //desired width;
    double height = //desired width;
    
        //maintain aspect ratio
    if(pWidth!=width || pHeight!=height)
    {
        double w = width/pWidth;
        double h = height/pHeight;
        if(w < h)
            height = height*w;
        else
            width = width*h;
    }
    
    ResizeBmp.CreateCompatibleBitmap(WindowToCaptureDC, width, height);
    MemDC.SelectObject(&ResizeBmp);
    
    MemDC.StretchBlt(0, 0, width, height, &CaptureDC, 0, 0, pWidth, pHeight, SRCCOPY);
    
    CImage TempImageObj;
    TempImageObj.Attach((HBITMAP)ResizeBmp.Detach());
    CString filePath = _T("LOCATION\\image.bmp");
    TempImageObj.Save(filePath);
    

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2018-02-19
      • 2022-11-11
      • 2011-03-28
      相关资源
      最近更新 更多