|
생성된 TImage 객체는 TList등 재 사용할 수 있도록 보관 후 사용해야 합니다.
아래는 간단한 사용 예를 보였습니다.
1. "그림 추가" 버튼을 누르면 Windows Folder에 있는 그림(깃털, 낙시, .. 등)을 화면에 추가
2. ComboxBox 선택시 선택된 그림 제거
- heredity -
*** Unit.H ***
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:
void __fastcall Button1Click(TObject *Sender);
void __fastcall cbEraseChange(TObject *Sender);
private:/
public:
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
*** Unit.CPP ***
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TList *g_plstImage;
TComboBox *g_cbErase;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
g_plstImage = new TList();
TButton * pbtn = new TButton( this );
pbtn->Parent = this;
pbtn->Left = 0;
pbtn->Top = 0;
pbtn->Caption = "그림추가";
pbtn->OnClick = Button1Click;
g_cbErase = new TComboBox( this );
g_cbErase->Parent = this;
g_cbErase->Left = 0;
g_cbErase->Top = 30;
g_cbErase->Width = 200;
g_cbErase->OnChange = cbEraseChange;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( g_plstImage != NULL ) {
PCHAR apchImageFile[] = {
"C:\\Windows\\깃털.bmp",
"C:\\Windows\\낚시.bmp",
"C:\\Windows\\바람부는 들판.bmp",
"C:\\Windows\\부채.bmp",
"C:\\Windows\\붉은 꽃.bmp",
"C:\\Windows\\붉은 카펫.bmp",
"C:\\Windows\\붉은 회벽.bmp",
"C:\\Windows\\비누 방울.bmp",
"C:\\Windows\\커피 잔.bmp",
"C:\\Windows\\파란 레이스 16.bmp",
"C:\\Windows\\회벽.bmp"
};
TImage *pimg = new TImage( this );
int nImgCnt = g_plstImage->Count;
int nFiles = sizeof(apchImageFile) / sizeof(apchImageFile[0]);
PCHAR pchFile = apchImageFile[nImgCnt % nFiles];
int nWidth = Screen->Width / 10;
int nHeight = Screen->Height / 10;
AnsiString asStr;
pimg->Parent = this;
pimg->Left = nImgCnt % 10 * nWidth;
pimg->Top = (nImgCnt / 10) * nHeight;
pimg->Width = nWidth;
pimg->Height = nHeight;
pimg->Stretch = true;
if( FileExists( pchFile ) ) {
pimg->Picture->LoadFromFile( pchFile );
}
else {
pimg->Picture->Assign( NULL );
}
asStr.sprintf( "%d번째 그림 : %s", nImgCnt, pchFile );
g_cbErase->Items->Add( asStr );
g_plstImage->Add( pimg );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbEraseChange(TObject *Sender)
{
int nIdx = g_cbErase->ItemIndex;
if( (nIdx >= 0) & (nIdx < g_plstImage->Count) ) {
TImage *pimg = (TImage *) g_plstImage->Items[nIdx];
if( pimg != NULL ) {
g_plstImage->Items[nIdx] = NULL;
g_cbErase->Items->Strings[nIdx] = "삭제됨";
delete pimg;
}
}
}
//---------------------------------------------------------------------------
이성제 님이 쓰신 글 :
: TImage를 동적생성 하고
:
: 부모를 Form1로 지정 합니다.
:
: 이 과정을 몇번 반복 하고나서
:
: 동적 생성한 것을 중간에 하나만 없애고 싶은데..
:
: Delete(TImage);
:
: 이런식으로 하니까..
:
: 마지막에 생성된것 하나만 없어지고 그 뒤론 아무 반응도 하지 않네요'';
:
: 어떻게 해야 할지 ㅠ
:
: 빠른 답변 부탁드립니다.
|