简体   繁体   中英

Passing char * as an argument

I've got a problem in my code. I'm rewriting an existing module that draw graph (from electrical consomation) with GDI+ (yes Visual Studio Microsoft and co) Here is my code to draw one graph

void CourbeComptage::afficheCourbeJour_nonEDF(CWnd *cwnd)
{
    dessin ligne,rectangle,texte;
    int i;
    int lx = x + margeH;
    int ly = y + haut - margeV;
    int x1, y1, x2, y2;

    if(flagCourbe)
    {
        afficheGraduation();

        // 4 - tracer la courbe de consommation avec des plages horaires
        for(i=0;i<24;i++)
        {
            x1 = lx + i * pasX;
            y1 = ly - coorX_conso[i];
            x2 = lx + (i + 1) * pasX;
            y2 = ly - 1;

            plage[i] = (plage[i] > 3) ? 3 : (plage[i] < 1) ? 1 : plage[i];

            if(typeCourbe == COURBE_BARRE_3D)
            {
                drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);
                switch(plage[i])
                {
                    case 1: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HC", "transparent", 1); break;
                    case 2: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HP", "transparent", 1); break;
                    case 3: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2,  "conso_P", "transparent", 1); break;
                }
            }
            else if(typeCourbe == COURBE_BARRE)
            {
                switch(plage[i])
                {
                    case 1: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, "conso2_HC", "transparent", 1); break;
                    case 2: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, "conso2_HP", "transparent", 1); break;
                    case 3: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2,  "conso2_P", "transparent", 1); break;
                }
            }
            else if(this->typeCourbe == COURBE_LIGNE)
            {
                if(i!= 23)
                {
                    switch(plage[i])
                    {
                        case 1: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], "conso2_HC"); break;
                        case 2: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], "conso2_HP"); break;
                        case 3: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1],  "conso2_P"); break;
                    }
                }
                // A ecrire 4 types point x 2 lignes de courbe (conso et react)
            }
        }
    }
}

In order to simplify my code and avoid some test I just want to transform that :

switch(plage[i])
{
    case 1: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HC", "transparent", 1); break;
    case 2: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HP", "transparent", 1); break;
    case 3: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2,  "conso_P", "transparent", 1); break;
}

Into that :

drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);

So I rewrite this function like that :

void CourbeComptage::afficheCourbeJour_nonEDF(CWnd *cwnd)
{
    static char gradient[4][9] = {"default",  "conso_HC",  "conso_HP",  "conso_P"},
    solid[4][10] = {"default", "conso2_HC", "conso2_HP", "conso2_P"};

    dessin ligne,rectangle,texte;
    int i;
    int lx = x + margeH;
    int ly = y + haut - margeV;
    int x1, y1, x2, y2;

    if(flagCourbe)
    {
        afficheGraduation();

        // 4 - tracer la courbe de consommation avec des plages horaires
        for(i=0;i<24;i++)
        {
            x1 = lx + i * pasX;
            y1 = ly - coorX_conso[i];
            x2 = lx + (i + 1) * pasX;
            y2 = ly - 1;

            plage[i] = (plage[i] > 3) ? 3 : (plage[i] < 1) ? 1 : plage[i];

            if(typeCourbe == COURBE_BARRE_3D)
            {
                drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);
            }
            else if(typeCourbe == COURBE_BARRE)
            {
                drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, solid[plage[i]], "transparent", 1);
            }
            else if(this->typeCourbe == COURBE_LIGNE)
            {
                if(i!= 23)
                {
                    drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], solid[plage[i]]);
                }

            }
        }
    }
}

And that don't works, meaning that the DrawFilledSolidRectangle (for example) get the string, but it don't draw the rectangle following the color passed in the string. I don't know why. I tried to dinamically allocate the two tab gradient and solid but that don't works too.

Here are the prototypes of some functions :

void DrawLine(int x1, int y1, int x2, int y2, char * penName);
void DrawFilledSolidRectangle(int x1, int y1, int x2, int y2, char * colorName, char * borderPen, int borderSize = 1, bool ombre = false);
void DrawFilledGradientRectangle(int x1, int y1, int x2, int y2, char * gradientName, char * borderPen, int borderSize = 1, bool ombre = false);

And here is the code of the DrawFilledSolidRectangle (DrawFilledGradientRectangle is the same except the Brush that is used)

Rect * rects = (Rect *)malloc(borderSize * sizeof(Rect));
Rect * tmp;
int i;

if(ombre) DrawRectangle(x1, y1, x2, y2, borderPen, true);
for(i = 0 ; i < borderSize ; i++)
{
    tmp = new Rect(x1+i, y1+i, x2-x1-i, y2-y1-i);
    rects[i] = *tmp;
}
DrawRectangles(rects, borderSize, borderPen);

x1 += i;
y1 += i;
x2 -= x1+i;
y2 -= y1-i;

SolidBrush * b = new SolidBrush(couleurs[colorName]);

gNaa->FillRectangle(b, x1, y1, x2, y2);
delete[](rects);

Thanks for your help, and sorry for my english.

使用条件表达式使这一过程变得非常简单:

drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, (plage[i] == 1 ? "conso_HC" : (plage[i] == 2 ? "conso_HP" : "conso_P")), "transparent", 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM