BWHazel
BWHazel

Reputation: 1474

In Win32, how can a Change Color dialog be used to change STATIC text?

I am relatively new to the Win32/Windows API (non-MFC), and am trying to change the text colour of a static text control. It is already drawn to the screen in black, but I want to change it to another colour using the Windows Colour Chooser dialog, which is opened on clicking a button. Is this possible?

For the button, the WM_COMMAND message is handled on clicking. So far, I have written:

CHOOSECOLOR ccColour;
ccColour.lStructSize = sizeof(ccColour);
ccColour.hwndOwner = hWnd;
ccColour.rgbResult = crLabelTextColour;
ccColour.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&ccColour) == TRUE)
{
    // crLabelTextColour is a COLORREF global variable assigned on loading the program
    crLabelTextColour = ccColour.rgbResult;
}

This code, however, fails with an unhandled exception at the if statement, and I'm not sure why! Other examples seem to write code like this.

Upvotes: 2

Views: 1303

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

ChooseColor() crashes because you are not initializing the CHOOSECOLOR structure completely. You are only setting 3 fields, the rest will contain garbage. You'll need to zero-initialize everything, simple to do:

  CHOOSECOLOR ccColour = {0};

Upvotes: 1

Related Questions