2013-06-20 59 views
0

我在我的代碼裏面創建了一個名爲Picture的代碼類,裏面有類似Bitmap^Sytem::String path和其他的屬性。我想創建一個指示圖片形狀的新屬性。在C++中定義ref類

我想要有三個不同的形狀類,一個是正方形,一個是水平矩形,最後是一個垂直矩形。通過查看BitMap像素的寬度和高度,我希望能夠更改我的圖片的屬性以匹配三個類中的一個。問題是我不知道我會如何做到這一點。舉個例子,比如說我有這樣的矩形:

_____________________ 
|      | 
|      | 
|_____________________| 

Picture->TemplateType = HORIZONTALRECTANGLE 

到目前爲止,我的圖片類看起來是這樣的:

public ref class Picture{ 
public: 
    System::String^ path; 
    BitMap^ image; 
    PosOnSlide *PositionAtributes; 
    bool EmptyPic; 
    bool PlacedOnSlide; 
}; 

我總是可以做到這一點

public ref class Picture{ 
public: 
    System::String^ path; 
    BitMap^ image; 
    PosOnSlide *PositionAtributes; 
    bool EmptyPic; 
    bool PlacedOnSlide; 
      int TemplateType // 0 = square, 1 = vertical, 2 = horrizontal 
}; 

但我認爲這代碼維護將更容易#定義類或枚舉,或者具有三種不同屬性的東西。

我有什麼選擇?

回答

1

是的,你說得對使用枚舉可能是一個簡單的解決方案:

enum class PictureType 
{ 
    Square, 
    HorizontalRectangle, 
    VerticalRectangle 
}; 

public ref class Picture 
{ 
public: 
    System::String^ path; 
    BitMap^ image; 
    PosOnSlide *PositionAtributes; 
    bool EmptyPic; 
    bool PlacedOnSlide; 
    PictureType Type; 
}; 

int main(array<System::String ^> ^args) 
{ 
    Picture^ picture = gcnew Picture(); 
    picture->Type = PictureType::Square; 

    return 0; 
} 

但你可能要分開你的類型和產生取決於位圖屬性不同的實例:

public ref class Picture 
{ 
public: 
    System::String^ path; 
    BitMap^ image; 
    PosOnSlide *PositionAtributes; 
    bool EmptyPic; 
    bool PlacedOnSlide; 
    Picture(System::String^ path, BitMap^ bitmap) 
    { 
     this->path = path; 
     this->image = bitmap; 
    } 
}; 

public ref class Square : Picture 
{ 
public: 
    Square(System::String^ path, BitMap^ bitmap) 
     : Picture(path, bitmap) 
    { 
    } 
}; 

public ref class HorizontalRectangle : Picture 
{ 
public: 
    HorizontalRectangle(System::String^ path, BitMap^ bitmap) 
     : Picture(path, bitmap) 
    { 
    } 
}; 

public ref class VerticalRectangle : Picture 
{ 
public: 
    VerticalRectangle(System::String^ path, BitMap^ bitmap) 
     : Picture(path, bitmap) 
    { 
    } 
}; 

public ref class PictureFactory 
{ 
public: 
    static Picture^ GetPicture(System::String^ path, BitMap^ bitmap) 
    { 
     Picture^ picture = nullptr; 
     if (bitmap->Height == bitmap->Width) 
     { 
      picture = gcnew Square(path, bitmap); 
     } 
     else if (bitmap->Height < bitmap->Width) 
     { 
      picture = gcnew HorizontalRectangle(path, bitmap); 
     } 
     else if (bitmap->Height > bitmap->Width) 
     { 
      picture = gcnew VerticalRectangle(path, bitmap); 
     } 

     return picture; 
    } 
}; 

int main(array<System::String ^> ^args) 
{ 
    Picture^ square = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 100)); 
    Picture^ hrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 10)); 
    Picture^ vrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(10, 100)); 

    System::Console::WriteLine(square->GetType()); 
    System::Console::WriteLine(hrect->GetType()); 
    System::Console::WriteLine(vrect->GetType()); 

    return 0; 
} 

這取決於你如何使用物體,越簡單越好。 :)

+0

非常感謝! – user1334858

+0

@ user1334858:不客氣。 :) – Pragmateek