Link to home
Start Free TrialLog in
Avatar of DSIGRIS
DSIGRIS

asked on

How do I read data under the cursor in Windows?


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?
Avatar of rcarlan
rcarlan

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.

Radu
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;
    m_static.GetWindowRect(&rect);
    ClientToScreen(&point);
    if (IsInside(&rect, point)) // check whether mouse is clicked inside the label
    {
        SetCapture();
        m_bTrackingMouse = true;
    }
    CDialog::OnLButtonDown(nFlags, point);
}

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

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

    if (m_bTrackingMouse)
    {
            GetCursorPos(&pt);
            // display the position of the mouse
            szString.Format ("%d, %d", pt.x, pt.y);
            m_editCursorPosition.SetWindowText((LPCTSTR)szString);

            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);
            m_editRGBValue.SetWindowText((LPCTSTR)szString);

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

Hope this will help.
Avatar of DSIGRIS

ASKER

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?

code sample is C++ & MFC
Avatar of DSIGRIS

ASKER

I know what C++ looks like, but I do know what MFC does.  Can you elaborate?
ASKER CERTIFIED SOLUTION
Avatar of rcarlan
rcarlan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial