传递char *作为参数

时间:2014-02-26 15:57:34

标签: c++ visual-studio gdi+

我的代码中存在问题。我正在重写一个用GDI +绘制图形(来自电气安慰)的现有模块(是Visual Studio Microsoft和co) 这是我绘制一个图表的代码

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)
            }
        }
    }
}

为了简化我的代码并避免一些测试我只想转换它:

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;
}

进入那个:

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

所以我重写了这个函数:

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]]);
                }

            }
        }
    }
}

这不起作用,意味着DrawFilledSolidRectangle(例如)获取字符串,但它不会在字符串中传递的颜色之后绘制矩形。 我不知道为什么。 我试图动态地分配两个标签gradientsolid,但这也不起作用。

以下是一些功能的原型:

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);

这是DrawFilledSolidRectangle的代码(除了使用的Brush之外,DrawFilledGradientRectangle是相同的)

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);

感谢您的帮助,对不起我的英语。

1 个答案:

答案 0 :(得分:0)

使用条件表达式使这非常简单:

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