0

I want to use the ChooseColor function in a console application. http://msdn.microsoft.com/en-us/library/windows/desktop/ms646912(v=vs.85).aspx

I'm using their sample code to test it out, and I have included windows.h like they told me, but I get the errors:

undefined reference to 'ChooseColorA@4'
undefined reference to 'CreateSolidBrush@4'

I'm using Code::Blocks, and I have tried including "Windows.h" and "Commdlg.h", but without luck. Do I need to mess around with Linker settings just to use windows.h? My code:

#include <windows.h>

int main()
{
    CHOOSECOLOR cc;                 // common dialog box structure
    static COLORREF acrCustClr[16]; // array of custom colors
    HWND hwnd;                      // owner window
    HBRUSH hbrush;                  // brush handle
    static DWORD rgbCurrent;        // initial color selection

    // Initialize CHOOSECOLOR
    ZeroMemory(&cc, sizeof(cc));
    cc.lStructSize = sizeof(cc);
    cc.hwndOwner = hwnd;
    cc.lpCustColors = (LPDWORD) acrCustClr;
    cc.rgbResult = rgbCurrent;
    cc.Flags = CC_FULLOPEN | CC_RGBINIT;

    if (ChooseColor(&cc)==TRUE)
    {
        hbrush = CreateSolidBrush(cc.rgbResult);
        rgbCurrent = cc.rgbResult;
    }
    return 0;
}
david
  • 292
  • 5
  • 20
  • Look at the documentation of those functions. It should say: *Library: gdi32.lib* and *Library: comdlg32.lib*. Yes, linking is a good thing. It would take much longer than it already does to compile windows.h without. – chris Dec 14 '13 at 02:26
  • 1
    Note also that you are testing the result of ChooseColor incorrectly. Read the documentation more closely. – Raymond Chen Dec 14 '13 at 02:33
  • 1
    What are you going to do with a brush in a console application? – Jonathan Potter Dec 14 '13 at 03:01

1 Answers1

2

You'll need to add the gdi32 library to your project so it links, not just include.

see: How do I link to a library with Code::Blocks?

Community
  • 1
  • 1
Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60