How do I read data under the cursor in Windows?

Posted on 2005-04-20
Last Modified: 2010-04-17

When you have no access to an application written in Windows, there may still be a need to "read" information under the cursor.  Of course, the data is in pixel form and would need to be converted to character format when the cursor is not over a picture.  

I have noticed that there are a few applications that can "magnify" the pixels under the cursor within their application (such as SnagIt! from TechSmith.  I recall several other programs that create a "magnifying glass" as a cursor to "Zoom in" while using a floating cursor.  

My goal is to create an application that remains external to any specific Windows application, but has the capability of "grabbing" a block of pixels when the right mouse button is used.  (if the application uses the right mouse button, I may need to find a way to activate the "grab" function some other way).

Anyone a detailed Windows person that can help me create the "cursor capture" function?
Question by:DSIGRIS
    LVL 9

    Expert Comment

    First of all, you'll have to install a mouse hook to get mouse messages while the mouse cursor is over a window belonging to another process.

    Secondly, to copy a portion of the screen, you need to get the screen DC - i.e. GetDC(NULL), and BitBlt from it to a memory DC compatible with the screen DC.

    LVL 6

    Expert Comment

    Try these piece of code.

    void CClrFndrDlg::OnLButtonDown(UINT nFlags, CPoint point)
        // say m_static is a label.
        // if the user clicks the that label we ready to read the pixel under the mouse cursor.
        CRect rect;
        if (IsInside(&rect, point)) // check whether mouse is clicked inside the label
            m_bTrackingMouse = true;
        CDialog::OnLButtonDown(nFlags, point);

    void CClrFndrDlg::OnLButtonUp(UINT nFlags, CPoint point)
        if (m_bTrackingMouse)
            m_bTracking = false;
        CDialog::OnLButtonUp(nFlags, point);

    void CClrFndrDlg::OnMouseMove(UINT nFlags, CPoint point)
        CString szString;
        POINT pt;

        if (m_bTrackingMouse)
                // display the position of the mouse
                szString.Format ("%d, %d", pt.x, pt.y);

                CDC *pDesktopHandle = CDC::FromHandle ( ::GetDCEx(NULL, NULL, 0));
                m_colCurrent = pDesktopHandle->GetPixel(pt);

                BYTE byRed   = GetRValue(m_colCurrent);
                BYTE byGreen = GetGValue(m_colCurrent);
                BYTE byBlue  = GetBValue(m_colCurrent);

                //  display the pixel color under the mouse
                szString.Format("%02x, %02x, %02x", byRed, byGreen, byBlue);

                //  release all dc handles
        CDialog::OnMouseMove(nFlags, point);

    Hope this will help.

    Author Comment

    This looks great.  I have no idea how the code could be compiled and executed as a program.  Is this code meant to be placed inside of a Visual Basic program, or is it meant to be used in conjunction with a lower level language such as Java?

    Since I am a beginner in the area of programming at lower level codes (I have only done a few programs in VB), I presume I should begin studying an appropriate language for this kind of processes.  Can you recommend documentation or study course that I might take to work on programs at this level?

    LVL 9

    Expert Comment

    code sample is C++ & MFC

    Author Comment

    I know what C++ looks like, but I do know what MFC does.  Can you elaborate?
    LVL 9

    Accepted Solution

    Microsoft Foundation Classes - comes with VC++

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    has22 challenge 11 56
    NotAlone Challenge 20 65
    Exe program is not a valid Win 32 application 15 74
    noX challenge 17 54
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now