-
Display Color dialog
How can i display the color dialog of windows API? ive tryed this:
Code:
#include <windows.h>
#include "resource.h"
#include "commdlg.h"
#include <string>
using namespace std;
HWND ghDlg = 0;
RGB custColors[16];
CHOOSECOLOR color;
color.lStructSize = sizeof(CHOOSECOLOR);
color.hwndOwner = ghDlg;
color.lpCustColors = custColors;
BOOL CALLBACK
ColorDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return true;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON:
ChooseColor(color);
return true;
case WM_CLOSE:
DestroyWindow(hDlg);
return true;
case WM_DESTROY:
PostQuitMessage(0);
return true;
}
return true;
}
return false;
}
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{
ghDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG), 0, ColorDlgProc);
ShowWindow(ghDlg, showCmd);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(GetMessage(&msg, 0, 0, 0))
{
if(ghDlg == 0 || !IsDialogMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
but i dont really know how to do it...what should i change to that code to display it?
that program has a dialog with just a button that when u press it, it should display the color dialog. i dont want it to be functional, just to display it.
Thank you very much
Cherry.
-
The next code works for me:
Code:
CHOOSECOLOR color;//the struct for the dialog
COLORREF ccref[16];//custom colors
COLORREF selcolor=0x000000;//the default selected color
memset(&color,0,sizeof(color));
color.lStructSize=sizeof(CHOOSECOLOR);
color.hwndOwner=hwnd;
color.lpCustColors=ccref;
color.rgbResult=selcolor;
color.Flags=CC_RGBINIT;
if(ChooseColor(&color))
{
selcolor=color.rgbResult;
//redraw with the new color
}
The 'ccref' is the 16 custom colors (the 2 lines of colors before the default colors), you shuld have to setup with a list of colors on start; the reference says:
Code:
lpCustColors: pointer to an array of 16 COLORREF values that contain red, green, blue (RGB) values for the custom
color boxes in the dialog box. If the user modifies these colors, the system updates the array with the new RGB
values. To preserve new custom colors between calls to the ChooseColor function, you should allocate static
memory for the array.
So if the user changes any custom color it will be automatically saved on the COLORREF list and it will be accessible from next dialog calls, so the 'ccref' should be a global static variable, do not reset each function call; and 'selcolor' also should be (or should be able to get the value of) an also global static var.
Hope that helps
Niara
-
Thank you very much! it works!
just one more question, what is memset for?
-
To set all the members of the "color" structure to 0, to ensure random data isn't left in the unset members.
-
oh well thank you very much!