自画Listbox,Style设置为 lbOwnerDrawVariable 后出现的问题。(100)

  • 主题发起人 topfortune
  • 开始时间
T

topfortune

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,我用的是BCB6. 在自画ListBox的时候,我将Style设置为lbOwnerDrawVariable,因为我的列表中的每个项目高度可能不同。现在画是能画出来,但是当鼠标滚轮在ListBox中上下滚动时,Listbox的刷新很混乱。不知道大家有没有碰到过这种问题?
 
T

topfortune

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我的代码: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 link "cxContainer"#pragma link "cxControls"#pragma link "cxListBox"#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]);}//---------------------------------------------------------------------------
 
G

guanyue7613

Unregistered / Unconfirmed
GUEST, unregistred user!
我以前是把ItemHeight设置成最大的那个item的height,所有的item都用那个height,然后小于那个高度的就画在中间,感觉会好一点。
 

Similar threads

D
回复
0
查看
728
DelphiTeacher的专栏
D
D
回复
0
查看
733
DelphiTeacher的专栏
D
D
回复
0
查看
584
DelphiTeacher的专栏
D
顶部