Search code examples
cwinapimingw32

Why GCC doesn't report any errors or warnings when using WinAPI: COLORREF/RGB?


The correct usage is:

COLORREF COLOR = RGB (255,255,255);

However, This

COLORREF COLOR = (255,255,255);

can be compiled without any warnings or errors.

Why? So strange? Thanks.

Reference:

  1. COLORREF

  2. RGB()


Solution

  • The type COLORREF is a typedef for a DWORD. The line

    COLORREF COLOR = (255,255,255);
    

    is equivalent to

    DWORD COLOR = 255;
    

    and therefore compiles fine. It may not do what you want however.

    The reason is that expr1, expr2 yields the value of expr2 in C, so 255, 255, 255 has a value of 255. The brackets have no effect here. See:

    http://en.wikipedia.org/wiki/Comma_operator

    Note that the RGB macro itself just makes the appropriate DWORD (some number) from red, green and blue components. Numbers essentially are COLORREFs here.