Thread: Display Color dialog

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    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.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    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

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Thank you very much! it works!
    just one more question, what is memset for?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    To set all the members of the "color" structure to 0, to ensure random data isn't left in the unset members.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    oh well thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  3. about scan 7-segment display via parallel port
    By mobdawg in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 06:11 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Printf to display chars
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2002, 05:01 PM