T
topfortune
Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我用的是BCB6. 在自画ListBox的时候,我将Style设置为lbOwnerDrawVariable,因为我的列表中的每个项目高度可能不同。现在画是能画出来,但是当鼠标滚轮在ListBox中上下滚动时,Listbox的刷新很混乱。不知道大家有没有碰到过这种问题? 下面是我的代码:Unit1.h//---------------------------------------------------------------------------#ifndef Unit1H#define Unit1H//---------------------------------------------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#include <Dialogs.hpp>#include <CheckLst.hpp>//---------------------------------------------------------------------------class TForm1 : public TForm{__published: // IDE-managed Components TListBox *ListBox1; TButton *Button1; TOpenDialog *OpenDialog1; void __fastcall Button1Click(TObject *Sender); void __fastcall ListBox1MeasureItem(TWinControl *Control, int Index, int &Height); void __fastcall ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State);private: // User declarationspublic: // User declarations __fastcall TForm1(TComponent* Owner);};//---------------------------------------------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------------------------------------------#endifunit1.cpp//---------------------------------------------------------------------------#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){ if( OpenDialog1->Execute() ) { ListBox1->Items->LoadFromFile( OpenDialog1->FileName ); }}//---------------------------------------------------------------------------void __fastcall TForm1::ListBox1MeasureItem(TWinControl *Control, int Index, int &Height){ Height = ListBox1->ItemHeight + Index%10;}//---------------------------------------------------------------------------void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State){ if(State.Contains(odSelected)) { ListBox1->Canvas->Brush->Color = clNavy; } else { if( Index%2 == 0 ) ListBox1->Canvas->Brush->Color = clSilver; else ListBox1->Canvas->Brush->Color = clWindow; } ListBox1->Canvas->FillRect(Rect); ListBox1->Canvas->TextOut(Rect.Left+2, Rect.Top+1, ListBox1->Items->Strings[Index]);}//---------------------------------------------------------------------------