您的位置:程序门 -> delphi -> vcl组件开发及应用



当鼠标在listbox窗体上滑动时如何使滑过的文字高亮显示


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


当鼠标在listbox窗体上滑动时如何使滑过的文字高亮显示
发表于:2007-07-31 07:35:27 楼主
各位老师好:
我想实现如下功能:
当鼠标在listbox窗体上滑动时使滑过的文字高亮显示,我在listbox控件的onmousemove(鼠标移动)事件中写了如下代码:
listbox1.itemindex:=y   div   20;

一个listbox窗体中有许多行文字,一个窗体内显示不完,如只能显示10行。如果鼠标在listbox窗体内的头10行滑动,则listbox中的文字高亮显示,可如果文字超过了第10行,则不能高亮显示,高亮显示的文字又变成了listbox窗体的前10行。

我研究了很长时间这个问题,不能解决。望各位老师不吝指点。
发表于:2007-07-31 09:44:121楼 得分:0

procedure   tform4.listbox1drawitem(control:   twincontrol;   index:   integer;
    rect:   trect;   state:   townerdrawstate);
var
    frect:   trect;
begin
    frect   :=   listbox1.itemrect(index);
    if   odhotlight   in   state   then
    begin
        listbox1.canvas.brush.color   :=   cllime;
        listbox1.canvas.fillrect(frect);
        listbox1.canvas.textout(frect.left,   frect.top,   listbox1.items[index]);
    end   else   if   odselected   in   state   then
    begin                    
        listbox1.canvas.brush.color   :=   clblue;
        listbox1.canvas.fillrect(frect);
        listbox1.canvas.drawfocusrect(frect);
        listbox1.canvas.textout(frect.left,   frect.top,   listbox1.items[index]);
    end   else
    begin
        listbox1.canvas.brush.color   :=   clwhite;
        listbox1.canvas.fillrect(frect);
        listbox1.canvas.textout(frect.left,   frect.top,   listbox1.items[index]);
    end;
end;

procedure   tform4.listbox1mousemove(sender:   tobject;   shift:   tshiftstate;   x,
    y:   integer);
var
    frect:   trect;
    i,   fitem:   integer;
    state:   townerdrawstate;
begin
    fitem   :=   listbox1.itematpos(point(x,   y),   true);
    if   fitem   >   -1   then
    begin
        frect   :=   listbox1.itemrect(fitem);
        for   i   :=   0   to   listbox1.count   -   1   do
        begin
            state   :=   [oddefault];
            if   i   =   fitem   then
                include(state,   odhotlight);
            if   listbox1.itemindex   =   i   then
                include(state,   odselected);
            listbox1drawitem(listbox1,   i,   frect,   state)
        end;
    end;
end;


快速检索

最新资讯
热门点击