2014-02-26 66 views
2

我在代碼中遇到問題。我重寫與GDI是繪製圖(從電consomation)現有模塊+(是的Visual Studio微軟和CO) 這裏是我的代碼來繪製一個圖形傳遞char *作爲參數

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(例如)獲取字符串,但不會在字符串中傳遞的顏色之後繪製矩形。 我不知道爲什麼。 我試圖dinamically分配兩個標籤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的代碼(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

1)有什麼問題嗎? 2)你使用'malloc'分配一些東西,然後用'delete []'刪除,這是錯誤的。 –

+1

什麼不適用於它? –

+0

我只是編輯我的問題。我不清楚。問題是最後一個函數(DrawFilledSolidRectangle)不使用正確的顏色,即使字符串傳遞良好。 – AMDG

回答

0

使用條件表達式使這很簡單:

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

我知道條件表達式,但我想避免測試'plage [i]'值來優化代碼。 – AMDG

+1

不成熟的優化是浪費時間和精力。編寫代碼然後對其進行分析以找出哪些瓶頸在哪裏進行優化。 –