Delphi XE 5 中 TListBox的OnMeasureItem 中 改变高度Height 运行结果还是设定的ItemHeight,高分求解决

代码如下:
procedure TForm1.provMeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);
begin
if Index mod 2=0 then height:=36 else height:=24;
end;

希望做一个Listbox,偶数行的高度大一些,奇数行的高度小一些
已选lbOwnerDrawVariable
设定初始ItemHeight为10

运行结果如下:

显然这是每一个都是初始设定的ItemHeight=10
难道OnMeasureItem事件没有发挥作用?还是我哪里写错?求解答
另外我在Delphi 7 上做过同样的尝试也是得到同样的错误结果

解决再追加20!!!
这个磨了我很久!!!!5个小时!!!!!!求速度解答!!!!!!!!

另外怎么设定某一个Item的背景色,比如选中的那个Item背景色变红色,其他背景还是白色的?这个再加15分!!!!!!!!!
或者选中的那个很丑的蓝色可以改成其他颜色么??

再不行有谁直接提供一个美化的ListBox组件????要Delphi XE 5 的就是****190.bpl版本
我就是想改掉TListBox原本很丑的面貌!!!!!ScrollBar也是!!!!!!选中框也是!!!!!!太难看了!!!!我要美化!!!!!!不过上面OnMeasureItem的问题一定要解决!!!!!!心都不安稳了,整个人都不好了

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  ListBox1.Style := lbOwnerDrawVariable;
  for I := 1 to 50 do
    ListBox1.Items.Add(Format('Item %d', [I]));
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
  if Index mod 2 = 0 then
    Height := 36
  else
    Height := 24;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  R: TRect;
begin
  with ListBox1.Canvas do
  begin
    // èƒŒæ™¯è‰²
    if State * [odSelected, odFocused] <> [] then
      Brush.Color := clRed
    else
      Brush.Color := ListBox1.Color;
    FillRect(Rect);
    // æ–‡å­—
    if State * [odSelected, odFocused] <> [] then
      Brush.Color := clHighlightText
    else
      Brush.Color := ListBox1.Font.Color;
    R := Rect;
    InflateRect(R, -4, 0);
    SetBkMode(Handle, TRANSPARENT);
    DrawText(Handle, ListBox1.Items[Index], -1, R,
      DT_SINGLELINE or DT_VCENTER);
    // å¦‚果有焦点,则再绘制一次焦点框,达到擦除的目的
    if odFocused in State then
      DrawFocusRect(Rect);
  end;
end;追问

早已设置,不过是lbOwnerDrawVariable吧,还是不行

追答

刚贴了代码和截图,你拿回去试试看

追问

再问一下,滚动条如何美化?

追答

滚动条没那么容易,要HookAPI

追问

试过了,还是不行,新建一个文件就可以。。。看来我程序某个地方有问题
formcreate有这句self.prov.Style:=lbOwnerDrawVariable;
先不谈drawitem
这两个是完全一样的,但是就是不行,你知道哪里有问题么

追答

这基本不大可能,除非你其他地方修改了Style属性或MeasureItem事件

追问

跟踪发现ProvMeasureItem没有被执行,但是DrawItem执行了,可能是什么原因呢

追答

看看你的ListBox的OnMeasureItem事件是否正确挂接到了ProvMeasureItem上

温馨提示:答案为网友推荐,仅供参考