|
김병은 님이 쓰신 글 :
: ListView에서 GridLine property를 사용할때
:
: 가령 .exe는 빨간색 .bat는 노란색으로
: row의 Color를 바꿀 수 있는 방법은 없습니까?
:
: 답변 부탁 드립니다.
:
임펠리테리입니다.
도스시절에 쓰던 MDir과 비슷한 모양으로 만들어보려고 하시나보네요. 저도 도스시절에 비슷하게 만들어본 경험이 있고 해서 옛추억도 생각이 나서.. Mdir과 거의 똑같이 해봤습니다.
먼저 몇가지 가정.
1. 리스트뷰의 ViewStyle은 vsReport로 설정되어 있다.
2. vsReport 상태에서는 SmallImages 프로퍼티에 연결된 이미지리스트가 사용되므로, SmallImages에 연결된 이미지리스트가 있는가를 검사해서 연결되어 있을 경우 그려주고 없으면 안그린다.
방법.
먼저 리스트뷰의 OwnerDraw 프로퍼티를 true로 세팅하세요.
그런 후 리스트뷰의 OnDrawItem 이벤트의 핸들러를 다음과 같이 작성합니다.
void __fastcall TForm1::ListView1DrawItem(TCustomListView *Sender,
TListItem *Item, TRect &Rect, TOwnerDrawState State)
{
TColor ExtColor;
AnsiString FileExt = ExtractFileExt(Item->Caption).LowerCase();
if(FileExt == ".exe") ExtColor = clYellow;
else if(FileExt == ".bat") ExtColor = clRed;
else ExtColor = clBlack;
if(State.Contains(odSelected))
{
ListView1->Canvas->Font->Color = clWhite;
ListView1->Canvas->Brush->Color = ExtColor;
}
else
{
ListView1->Canvas->Font->Color = ExtColor;
ListView1->Canvas->Brush->Color = clWhite;
}
int CaptionLeft = Rect.Left + 2;
if(ListView1->SmallImages != NULL)
{
ListView1->SmallImages->Draw(ListView1->Canvas, CaptionLeft, Rect.Top, Item->ImageIndex, true);
CaptionLeft = CaptionLeft + ListView1->SmallImages->Width;
}
Rect.Left = Rect.Left + CaptionLeft;
ListView1->Canvas->FillRect(Rect);
ListView1->Canvas->TextRect(Rect, Rect.Left+2, Rect.Top+2, Item->Caption);
if(State.Contains(odFocused)) ListView1->Canvas->DrawFocusRect(Rect);
}
그럼 이만...
|