|
아래 소스에는 문제가 없는 것 같네요. 아마도 다른 부분에서 실수를 하신게 아닌가 싶습니다.
혹시 Font를 지정하시고, Listbox 의 프로퍼티중, Style을 부분을 default 값인 lbStandard로 설정해 두시진 않으셨나요?
Listbox 의 프로퍼티중, Style을 부분을 lbOwnerDrawFixed 이나 lbOwnerDrawVariable로 설정하셔야, DrawItem 이벤트를 사용하실 수 있습니다.
아래는 간단한 예제입니다.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Items->Add ("Hello");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State)
{
switch(Index % 5) {
case 0 :
ListBox1->Canvas->Font->Color= clNavy;
break;
case 1 :
ListBox1->Canvas->Font->Color= clMaroon;
break;
case 2 :
ListBox1->Canvas->Font->Color= clOlive;
break;
case 3 :
ListBox1->Canvas->Font->Color= clBlack;
break;
case 4 :
ListBox1->Canvas->Font->Color= clTeal;
break;
}
ListBox1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, ListBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------
김상훈 님이 쓰신 글 :
: 리스트 박스에서 각 아이템의 덱스트 색깔이 각 조건에 의해서 바꾸어지도록 코딩을 햇는데..각 라인이 바뀌는 것이 아니라..전체가 다 바뀌어 집니다..어디가 잘 못 되었는지 선배님들의 많은 가르침을 기다리겠습니다...그럼 즐거운 주말이 돼세요...
:
: bool __fastcall TMsgForm::ReadMsg( void )
: {
: ....
: ....
: ....
: tmp_level=....;
: ...
: }
:
: void __fastcall TMsgForm::ListBox1DrawItem(TWinControl *Control, int Index,
: TRect &Rect, TOwnerDrawState State)
: {
: if( tmp_level == MSG_LVL_LOW){
: ListBox1->Canvas->Font->Color= clWhite;}
: else if( tmp_level == MSG_LVL_MED){
: ListBox1->Canvas->Font->Color = clAqua;}
: else if( tmp_level == MSG_LVL_HIGH){
: ListBox1->Canvas->Font->Color = clYellow;}
: else if( tmp_level == MSG_LVL_ERROR){
: ListBox1->Canvas->Font->Color = clFuchsia;}
: else if( tmp_level == MSG_LVL_FATAL){
: ListBox1->Canvas->Font->Color = clRed;}
:
: ListBox1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, ListBox1->Items->Strings[Index]);
:
: }
|