Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / WTL
Tip/Trick

Capture Program using GDI

Rate me:
Please Sign up or sign in to vote.
4.85/5 (24 votes)
29 Mar 2017CPOL1 min read 35.9K   6.2K   34   5
Capture program using GDI

Source files were corrupted. I uploaded sources again.

Introduction

I have been interested in WTL (Windows Template Library) for a very long time. So, I decided to make a small program using WTL last September. I really like the fact that the size of the executable files that WTL makes is much smaller than the ones that MFC (Microsoft Foundation Classes) makes and the source code is open for everyone to see. Some people might think that there are no advantages between WTL and MFC. But I believe that these tools are both powerful for anyone who wants to build small, fast programs.

If some errors show up related to libraries, you might have to install the run-time components below.

Visual C++ Redistributable for Visual Studio 2015

I have tested this program on Windows 7 and Windows XP. I don’t know if it will work on other Windows platforms.

Features

  1. Capture Full Screen - You can take a snapshot of the whole screen.
  2. Capture Window - You can capture one window on your screen.
  3. Capture Region - You can specify what you want to capture.
  4. Zoom In
  5. Zoom Out
  6. Copy the captured Image to MS Paint
  7. Favorites
  8. Copy the captured image to the Clipboard
  9. Environment (Hotkeys)
  10. Image viewer - includes panning function

Implementation

Sending the Captured Image to Microsoft Paint

C++
//Copy an image to the clipboard.
if (OpenClipboard())
{
    EmptyClipboard();
    CBitmap bmpCopy;

    BITMAP bm;
    if (GetObject(pImageData->m_bmpOri.m_hBitmap, sizeof(BITMAP), &bm))
        bmpCopy = (HBITMAP)CopyImage(pImageData->m_bmpOri.m_hBitmap, IMAGE_BITMAP, 0, 0, 0);
    ATLASSERT(::GetObjectType(bmpCopy.m_hBitmap) == OBJ_BITMAP);
    SetClipboardData(CF_BITMAP, bmpCopy.m_hBitmap);
    CloseClipboard();
}

if (IsClipboardFormatAvailable(CF_BITMAP))
{
    HWND                    hPaint;
    STARTUPINFO                si;
    PROCESS_INFORMATION        pi;

    memset(&si, 0, sizeof(STARTUPINFO));
    memset(&pi, 0, sizeof(PROCESS_INFORMATION));
    si.cb = sizeof(STARTUPINFO);

    wchar_t    szWindowDirector[MAX_PATH] = { 0 };
    CString    strMspaintLoc;

    GetSystemDirectory(szWindowDirector, MAX_PATH);
    strMspaintLoc.Format(L"%s\\%s", szWindowDirector, L"mspaint.exe");
    if (CreateProcess(strMspaintLoc,
        NULL, NULL, NULL, FALSE,
        NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
    {
        //HWINEVENTHOOK hook =
        //    SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL,
        //        WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
        /*ResumeThread(pi.hThread);*/
        //THIS PART NEEDS TO BE FIXED.
        while (TRUE)
        {
            Sleep(100);
            hPaint = FindWindow(L"MsPaintApp", NULL);
            ATLTRACE(L"findwindow Handle %X\n", hPaint);
            if (hPaint) break;
        }
    }
    else
    {
        MessageBox(L"Couldn't find mspaint program");
        return;
    }

    if (OpenClipboard())
    {
        CloseClipboard();

        if (SetForegroundWindow(hPaint))
        {
            //Ctrl + V (paste)
            INPUT ip;
            ip.type = INPUT_KEYBOARD;
            ip.ki.wScan = 0;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;

            // Press the "Ctrl" key
            ip.ki.wVk = VK_CONTROL;
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));

            // Press the "V" key
            ip.ki.wVk = 'V';
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));

            // Release the "V" key
            ip.ki.wVk = 'V';
            ip.ki.dwFlags = KEYEVENTF_KEYUP;
            SendInput(1, &ip, sizeof(INPUT));

            // Release the "Ctrl" key
            ip.ki.wVk = VK_CONTROL;
            ip.ki.dwFlags = KEYEVENTF_KEYUP;
            SendInput(1, &ip, sizeof(INPUT));
        }
    }
}

Capturing Screen

C++
CRect        rc;
ShowWindow(SW_MINIMIZE);
Sleep(100);

// Start to get an Image from the nearest screen (Prepare capture data)
CDC            dcWindow;
dcWindow.CreateDCW(L"DISPLAY", NULL, NULL, NULL);

// Get the nearest monitor rect
CMonitorInfo::GetMonitorRect(m_hWnd, rc);

CBitmap        bmp, oldbmp;
CDC            dcMem;
CClientDC    dc(m_hWnd);
dcMem.CreateCompatibleDC();
bmp.CreateCompatibleBitmap(dcWindow, rc.Width(), rc.Height());
oldbmp = dcMem.SelectBitmap(bmp);
dcMem.BitBlt(0, 0, rc.Width(), rc.Height(), dcWindow, rc.left, rc.top, SRCCOPY);

SetCaptureImage(&bmp);

dcMem.SelectBitmap(oldbmp);
ShowWindow(SW_RESTORE);

Capturing a Window

A Window shows up covering your screen after the whole screen has been copied.

C++
void CMCFrameWnd::CaptureWindow()
{
    // Get full screen size.
    CRect        rcMonitor;

    ShowWindow(SW_MINIMIZE);
    Sleep(100);

    // Start to get an Image from the nearest screen (Prepare capture data)
    CDC            dcWindow;
    dcWindow.CreateDCW(L"DISPLAY", NULL, NULL, NULL);

    // Get the nearest monitor Rect
    CMonitorInfo::GetMonitorRect(m_hWnd, rcMonitor);

    CBitmap        bmp, oldbmp;
    CDC            dcMem;
    CClientDC    dc(m_hWnd);
    dcMem.CreateCompatibleDC();
    bmp.CreateCompatibleBitmap(dcWindow, rcMonitor.Width(), rcMonitor.Height());
    oldbmp = dcMem.SelectBitmap(bmp);
    dcMem.BitBlt(0, 0, rcMonitor.Width(),
    rcMonitor.Height(), dcWindow, rcMonitor.left, rcMonitor.top, SRCCOPY);
    dcMem.SelectBitmap(oldbmp);

    if (m_pwndTemparyScreenWnd != NULL)
        delete m_pwndTemparyScreenWnd;
    m_pwndTemparyScreenWnd = new CTemparyScreenWnd(this);
    m_pwndTemparyScreenWnd->mode = CTemparyScreenWnd::Mode::TEMPARYSCREEN_WINDOW;
    m_pwndTemparyScreenWnd->Create(m_hWnd, rcMonitor, 0, WS_POPUP | WS_VISIBLE, WS_EX_TOPMOST);
    m_pwndTemparyScreenWnd->GetWindowData();
    m_pwndTemparyScreenWnd->SetScreenImage(bmp);
    m_pwndTemparyScreenWnd->ShowMonitorScreen();
    SetForegroundWindow(m_pwndTemparyScreenWnd->m_hWnd);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Korea (Republic of) Korea (Republic of)
I am a chef. I am currently living in Ecuador. I am taking a rest here hanging out with many people and animals.

Comments and Discussions

 
PraiseGreat Work. 5! Pin
simonp_ca19-Jul-19 13:46
simonp_ca19-Jul-19 13:46 
QuestionSome comments Pin
Michael Haephrati2-Apr-17 2:27
professionalMichael Haephrati2-Apr-17 2:27 
GeneralMy vote of 5 Pin
GastonCroze29-Mar-17 19:25
GastonCroze29-Mar-17 19:25 
GeneralOld solution. Pin
Evgeny Pereguda12-Nov-15 0:02
Evgeny Pereguda12-Nov-15 0:02 
GeneralRe: Old solution. Pin
Taehoon Kim 100412-Nov-15 0:21
Taehoon Kim 100412-Nov-15 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.