GetPixel issue

Hello, I am trying to use the GetPixel function in my program. I tried just including <windows.h> but I still couldent use the function. So this is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    FARPROC fGetPixel;
    HINSTANCE gdi32 = LoadLibrary("gdi32.dll");
    if(gdi32)
    {
        fGetPixel = GetProcAddress(gdi32, "GetPixel");
        HDC device = GetDC(NULL);
        if(device)
        {
            COLORREF colour = (*fGetPixel) (device, 0, 0);
            int red = GetRValue(colour);
            int green = GetGValue(colour);
            int blue = GetBValue(colour);
            cout << "Red: " << red << endl;
            cout << "Green: " << green << endl;
            cout << "Blue: " << blue << endl;
        }
        FreeLibrary(gdi32);
    }
    return 0;
}
But when I try to run it, I get this error:error: too many arguments to function (I am using Code::Blocks) I have looked up the function on msdn, and it definatly takes three parameters, so I cant figure out why this is happening. If anyone could help me I would be very greatful, thankyou.
Last edited on
If you load a library using LoadLibrary() and obtain the addess of a function with GetProceAddress() then you don't really need the header file.

But your problem probably resides on the declaration of fGetPixel. It is incorrect. The declaration needs to match the signature of GetPixel(), which you can see @ http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx .

This means that you must declare a function pointer that mathes GetPixel(), like this:

1
2
typedef WINAPI COLORREF (*GETPIXELFN)(HDC, int, int);
GETPIXELFN fGetPixel;


NOTE: You still need Windows.h for the definitions of WINAPI, COLORREF and HDC.
Here is an example method to retrive desktop DPI

DIPs = pixels / (DPI/96.0)


1
2
3
4
5
6
7
8
9
10
float g_DPIScaleX = 1.0f;
float g_DPIScaleY = 1.0f;

void InitializeDPIScale(HWND hwnd)
{
    HDC hdc = GetDC(hwnd);
    g_DPIScaleX = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0f;
    g_DPIScaleY = GetDeviceCaps(hdc, LOGPIXELSY) / 96.0f;
    ReleaseDC(hwnd, hdc);
}


I learn direct2D so I can't help you with GDI, sorry :(

Topic archived. No new replies allowed.