[C++ Builder] Image canvas 설정 Backup 방법

빵욱·2025년 8월 12일

C++Builder with VCL

목록 보기
4/6

TImage 컨트롤을 사용하여 이미지를 그려야 하는 작업이 있었다.

TImage 객체의 canvas를 사용해서 그리는데 기존 적용된 폰트, 색, 사이즈등을 변경 후 사용한다음에 다시 원상태(기본상태) 복원하면서 사용했다.
나중에 설정 변경후 꼬이면 안되니까...

Font같은 경우 TFont객체에 설정을 담은 후 함수 호출이 끝나는 시점에 다시 복원.
이때 TFont는 델파이 머시기 객체라고 꼭 new로 선언하라고 한다..
그래서 delete를 꼭 해주거나 했어야 했는데 Guard역할을 구조체를 이용해서 하니 좋은 것 같아서 메모.

struct STFontGuard
{
    TCanvas *c;
    String name;
    int size, orientation;
    TColor color;
    TFontStyles style;
    __fastcall STFontGuard(TCanvas *c)
        : c(c), name(c->Font->Name), size(c->Font->Size), orientation(c->Font->Orientation), color(c->Font->Color),
          style(c->Font->Style)
    {
    }
    __fastcall ~STFontGuard()
    {
        c->Font->Name = name;
        c->Font->Size = size;
        c->Font->Orientation = orientation;
        c->Font->Color = color;
        c->Font->Style = style;
    }
};
// TCanvas * canvas
STFontGuard guard(canvas);

이렇게 하니 함수 끝날 때 구조체 소멸자가 호출되면서 폰트 정보를 복원.
다른 정보를 백업 후 복원할 때 좋은 방법인 것 같다.

profile
rove drink eat

0개의 댓글