Click here to Skip to main content
12,507,343 members (64,081 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.9K views
4.5K downloads
21 bookmarked
Posted

Capture Program using GDI

, 12 Nov 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
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 opened 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

https://www.microsoft.com/en-us/download/details.aspx?id=48145

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

//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

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 show up covering your screen after the whole screen has been copied.

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)

Share

About the Author

Taehoon Kim 1004
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralOld solution. Pin
Evgeny Pereguda12-Nov-15 0:02
memberEvgeny Pereguda12-Nov-15 0:02 
GeneralRe: Old solution. Pin
TaehoonKim100412-Nov-15 0:21
memberTaehoonKim100412-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 12 Nov 2015
Article Copyright 2015 by Taehoon Kim 1004
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid