• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

I have a C++ application which I need altered to simply change the colors of controls

Hi Experts, I have no C++ backround.  I write in VB and ASP.  I currently have a project which is very urgent and requires immediate attention.

I curently have an application which has several display controls.  I need to be able to edit the color of these in my ini file.

e.g.

[Display]
BackgroundColor=
BorderColor=
StaticTextColor=
DynamicTextColor=
GraphOutlineColor=
GraphActiveElementColor=
PageHighlightColor=
AlarmColor=

Then when my app starts up it will extract this data  and implent it to my GUI.  I am sure this is relatvely easy (when you know how!!) as the current app uses other info from the ini file.  I want to use this as a learning experience for myself, I seek help due to the urgency of this project.

I will be happy to mail the code over to anyone wishing to help.  I will also give extra points
0
RickyHanna
Asked:
RickyHanna
1 Solution
 
AlexFMCommented:
To read data from ini file use GetPrivateProfileString function. For example, to read BackGroundColor value from settings.txt file use this code:

TCHAR s[256];
GetPrivateProfileString(_T("Display", _T("BackgroundColor"), _T("Black"), s, 256, _T("settings.txt"));

The answer how to apply these colors in application depends on application type (using MFC or not, managed or unmanaged) and controls which are used in it.
0
 
Svetlin_PanayotovCommented:
I suppose your project is Managed C++
So here's an example on how to accomplish something like this:

Add this methods to your form:
                   System::Void SaveColor(LPSTR szElement, Color oColor)
                   {
                         LPCSTR szColorName = new char[250];
                         szColorName =(char*) Runtime::InteropServices::Marshal::StringToHGlobalAnsi(oColor.ToString()).ToPointer();
                         WritePrivateProfileString("Display",szElement,szColorName,"c:\\color.ini");
                   }
                  
                   System::Drawing::Color GetColor(LPSTR szElement)
                   {
                         LPSTR szResult = new char[251];
                         GetPrivateProfileString("Display",szElement,"Color [Gray]",szResult,250,"c:\\color.ini");
                         String* sColorName = new String(szResult);
                         Color oRetVal = Color::FromName(sColorName);
                         return oRetVal;
                   }

Just replace "c:\\color.ini" with your INI file name
After that you can use them like this:

this->BackColor = this->GetColor("BackgroundColor"); //set form background color
SaveColor("LblColor",label1->get_BackColor()); //save label background color

Hope this 'll help

Svetlin
0
 
RickyHannaAuthor Commented:
Thanks to you both, I can see what your code is trying to accomplish here. My problem is that this is the first time I have even opened C++ and don't know where to put the code or how to see the controls in desogn time that I need to change.

I was hoping I could mail the project to you and learn from what you are doing, to be quite honest I'm a bit lost.

0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
Svetlin_PanayotovCommented:
Well - to be honest - even if you mail me your project I won't have time to look at it or write some code.So I can only recommend you to do some reading and get yourself familiar with C++ (and the IDE).
You can start here:
http://www.codeproject.com/managedcpp/#C%2B%2B%2FCLI

Svetlin
0
 
RickyHannaAuthor Commented:
I need someone to look at my code and edit it.  In vb it's simple to see controls and change properties.  I can't even see the form in design view yet??

I am lost and need guidance

HELP!
0
 
RickyHannaAuthor Commented:
Anyone?
0
 
itsmeandnobodyelseCommented:
Look at that alternative to the VC6 solution. I used two structs now to associate colors to RGB values, respectively to associate variable names to variable pointers. Also, i replaced char by TCHAR in case you want to change to wide strings.

Regards, Alex

// define an index of all valid colors
enum ColorIndex {
    IDX_BLACK,          
    IDX_RED,
    IDX_GREEN,          
    IDX_BLUE,          
    IDX_WHITE,          
    IDX_GREY,          
    IDX_YELLOW,    
    IDX_PINK,          
    MAX_IDX_COLOR  // size increases automatically
};

// connect color name to RGB value
struct ColorDef
{
    COLORREF rgb;    // RGB value of color or constatnt if already defined
    char*    color;  // name of color as defined in inifile
};


ColorDef colors[MAX_IDX_COLOR] =
{
    { RGB(0,0,0)       ,  _T("BLACK"),  },                
    { RGB(255,0,0)     ,  _T("RED"),    },                
    { RGB(0,255,0)     ,  _T("GREEN"),  },                
    { RGB(0,0,255)     ,  _T("BLUE"),   },                  
    { RGB(255,255,255) ,  _T("WHITE"),  },                
    { RGB(128,128,128) ,  _T("GREY"),   },                
    { RGB(255,255,0)   ,  _T("YELLOW"), },                
    { RGB(255,0,255)   ,  _T("PINK"),   },                
};

// define an index of all variables (color keys in inifile)
enum GUIColor {
      BACKGROUNDCOLOR,
      BORDERCOLOR,
      DISABLECOLOR,        
      STATICTEXTCOLOR,
      DYNAMICTEXTCOLOR,
      GRAPHOUTLINECOLOR,
      GRAPHACTIVEELEMENTCOLOR,
      BARGRAPHCOLOR,
      PAGEHIGHLIGHTCOLOR,
      ALARMCOLOR,
      MAX_GUI_COLOR  // size grows automatically
};

// associate name to pointer of variable
struct GUIColorDef
{
    TCHAR*    var;     // the name of the variable as defined in inifile
    COLORREF* pVar;    // the pointer to the global variable
};

GUIColorDef guiColors[MAX_GUI_COLOR] =
{
    { _T("BackgroundColor"),           &backgroundcolor,           },
    { _T("BorderColor"),               &bordercolor,               },
    { _T("DisableColor"),              &disablecolor,              },
    { _T("StaticTextColor"),           &statictextcolor,           },
    { _T("DynamicTextColor"),          &dynamictextcolor,          },
    { _T("GraphOutlineColor"),         &graphoutlinecolor,         },
    { _T("GraphActiveElementColor"),   &graphactiveelementcolor,   },
    { _T("BarGraphColor"),             &bargraphcolor,             },
    { _T("PageHighlightColor"),        &pagehighlightcolor,        },
    { _T("AlarmColor"),                &alarmcolor,                },
};                                                            
                                                               
const int EQUAL = 0;

void getColorsFromInifile(LPCTSTR pszInifile)
{
    TCHAR szBuffer[128] = _T("");;

    //  Look for color entries in inifile, e.g. BorderColor=RED
    for (int i = 0; i < MAX_GUI_COLOR; ++i)
    {
        // section is [Display]
        int nLen = GetPrivateProfileString(_T("Display"), guiColors[i].var, _T(""), szBuffer, sizeof(szBuffer), pszInifile);
        // nLen == 0: no definition or wrong key
        if (nLen > 0)
        {
            // loop all valid colors of array colors
            for (int n = 0; n < MAX_IDX_COLOR; ++n)
            {
                // compare with entry of inifile
                if (_tcsicmp(colors[n].color, szBuffer) == EQUAL)
                {
                    // set variable using the coresponding RGB value
                    *(guiColors[i].pVar) = colors[n].rgb;
                    break;
                }
            }
        }
    }
}

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now