Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All get the value 0, the wrong color

(windows 10, Microsoft Visual C++ 2010)


What I have tried:

HWND hChromeWnd = NULL;
hChromeWnd = ::FindWindow(NULL, _T("NAVER - Chrome"));

HDC h_temp_dc;
COLORREF ref_temp;

h_temp_dc = ::GetDC(hChromeWnd);	

ref_temp = GetPixel(h_temp_dc, 200, 200);

int nR = GetRValue(ref_temp);
int nG = GetGValue(ref_temp);
int nB = GetBValue(ref_temp);

strTemp.Format(_T("%d, %d, %d"), nR, nG, nB);
AfxMessageBox(strTemp);
Posted
Updated 4-May-22 17:12pm

1 solution

I would add error checking to make sure the FindWindow and GetDC calls actually work. I tried using FindWindow and I couldn't get it to work for me.

I ended up writing a little class that calls EnumWindows and uses a callback function that calls GetWindowText. That worked for me. Here is the code I used :
C++
class FindWin
{
public:
    FindWin( PCSTR text, bool partial=true );

    // can be overridden to do something to the window found

    virtual bool Find()
    {
        m_Found = false;
        if( m_Text && * m_Text )
            EnumWindows( EnumFunc, (LPARAM)this );
        return m_Found;
    }

    // return a handle to the window found

    HWND        GetWnd()        { return m_hWnd; }

protected:
    BOOL        Matches( HWND hwnd, PCSTR wintext );

    static BOOL CALLBACK EnumFunc( HWND hwnd, LPARAM lParam );

protected:
    PCSTR       m_Text          { nullptr };
    size_t      m_Length        { 0 };
    HWND        m_hWnd          { NULL };
    bool        m_Found         { false };
    bool        m_Partial       { false };      // allowing partial match
};

///////////////////////////////////////////////////////////////////////

FindWin::FindWin( PCSTR text, bool partial )
{
    m_Text = text;
    m_Length = strlen( m_Text );
    m_Partial = partial;
}

///////////////////////////////////////////////////////////////////////

BOOL FindWin::Matches( HWND hwnd, PCSTR wintext )
{
    if( m_Partial )
        m_Found = strncmp( wintext, m_Text, m_Length ) == 0;
    else
        m_Found = strcmp( wintext, m_Text ) == 0;
    
    if( m_Found )
        m_hWnd = hwnd;
    return (int) ! m_Found;     // return false if found
}

///////////////////////////////////////////////////////////////////////

BOOL CALLBACK FindWin::EnumFunc( HWND hwnd, LPARAM lParam )
{
    FindWin * pew = (FindWin *)lParam;
    char wtext[ MAX_PATH+4 ] = { 0 };
    GetWindowText( hwnd, wtext, MAX_PATH );
    return pew->Matches( hwnd, wtext );
}
Here is the code that uses this class :
C++
void ReadPixelValue()
{
    FindWin finder( "Microsoft Spy++" );
    if( ! finder.Find() )
    {
        printf( "Error - unable to find window\n" );
        return;
    }

    HWND hwnd = finder.GetWnd();
    HDC hdc = ::GetDC( hwnd );
    if( ! hdc )
    {
        printf( "Error - unable to obtain device context\n" );
        return;
    }

    COLORREF color = GetPixel( hdc, 20, 20 );

    printf( "pixel color is %06X\n", color );
}
 
Share this answer
 
Comments
Member 15570851 4-May-22 23:42pm    
We confirmed that the FindWindow GetDC GetPixel function works properly.

We have confirmed that we get the color of another dialog window with the FindWindow function.
But all get black in Chrome Browser
Shao Voon Wong 4-May-22 23:51pm    
Perhaps your Chrome browser is using hardware acceleration for graphics rendering? This means it is not using DC for rendering but DirectX.
Member 15570851 5-May-22 0:03am    
What should I do?
please explain in detail
Shao Voon Wong 5-May-22 0:10am    
Maybe you can try to capture the image using DirectX? You need to modify the code in the article below to capture the window content instead of a screen. I cannot help you with this because it is too complicated in a post.

https://www.codeproject.com/Articles/5256890/ScreenCapture-Single-Header-DirectX-Library
Member 15570851 6-May-22 22:43pm    
Thank you.
It feels good to know the cause.
in the settings of the chrome browser
Disable hardware acceleration
GetPixel found the color well.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900