如标题.

解决方案 »

  1.   

    你写在什么事件中啊?试试写在Form的onpaint或者listbox的OnDrawItem事件里。
      

  2.   

    谢谢,
    试过了,还是不行.
    原码如下:
      With Self.ListBox1.Canvas Do
        Begin
          theRect := Rect(X, Y, X + TextAddWidth, Y + TextAddHeight);
          TextRect(theRect, X, Y, Str);
        End;
      

  3.   

    自己继承一下TCustomListBox,然后重写DrawItem试试,参考一下
    type
      TmyListBox = class(TCustomListBox)
      {function GetCurrentItem(): TA;
      function GetTotalItemLength(): integer;}
     private
       //procedure CMFontChanged(var Msg: TMessage);
     protected
       procedure DrawItem(Index: Integer; Rect: TRect;
           State: TOwnerDrawState); override;
     public
       constructor Create(AOwner: TComponent); override;
     published
       property Align;
       property Anchors;
       property BevelEdges;   .........
       property OnDrawItem;
     end;procedure TmyListBox.DrawItem(Index: Integer; Rect: TRect;
     State: TOwnerDrawState);
    var
     Flags: Longint;
     Data: String;
    begin
     {SetLength(ItemHeightList,Items.Count);
     SetLength(TotalItemHeight,Items.Count);}
     Canvas.FillRect(Rect);
     if Index < Items.Count then begin
       Canvas.Font := Font;
       {if odSelected in State then
         Canvas.Font.Color := clSkyBlue;}
       Flags := DrawTextBiDiModeFlags(
           DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_EDITCONTROL);
       if not UseRightToLeftAlignment then
         Inc(Rect.Left, 2)
       else
         Dec(Rect.Right, 2);   Data := Items[Index];
       DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
       {ItemHeightList[Index] := Rect.Top;
       TotalItemHeight[Index] := Rect.Bottom - Rect.Top;}
     end;
    end;
      

  4.   

    还是不不行,请再指教.我是接收串口信息,当接受到的信息符合条件时,就用TextRect输出到listbox上
      

  5.   

    怎么会?把代码贴出来直接添加到listbox里面不可以吗
      

  6.   

    就是将如下代码加到listbox里面的ondrawitem事件中吧,我刚才加了,不行procedure TFrmMain.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
     Flags: Longint;
     Data: String;
    begin
     {SetLength(ItemHeightList,Items.Count);
     SetLength(TotalItemHeight,Items.Count);}
     Canvas.FillRect(Rect);
     if Index < Self.ListBox1.Items.Count then begin
       Canvas.Font := Font;
       {if odSelected in State then
         Canvas.Font.Color := clSkyBlue;}
       Flags := DrawTextBiDiModeFlags(
           DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_EDITCONTROL);
       if not UseRightToLeftAlignment then
         Inc(Rect.Left, 2)
       else
         Dec(Rect.Right, 2);   Data := Self.ListBox1.Items[Index];
       DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
       {ItemHeightList[Index] := Rect.Top;
       TotalItemHeight[Index] := Rect.Bottom - Rect.Top;}
     end;
    end;
      

  7.   

    刚才跟踪了一下,在执行ListBox.Canvas.TextRect时,根本没有调用上面的TFrmMain.ListBox1DrawItem过程.
    是不是还需要加些什么才能
      

  8.   

    ListBox1.Style := lbOwnerDrawFixed;
      

  9.   

    刚才设置了listbox的style属性,还是没有调用OnDrawItem,切换后,仍然没有以前显示的数据.
      

  10.   

    你试试这行代码,ListBox的items中加一些文件名:
    procedure TfrmSysSetup.inListBox1DrawItem(Control: TWinControl;
      Index: Integer; Rect: TRect; State: TOwnerDrawState);
    var
      Icon: TICon;
      SHFileInfo: TSHFileInfo;
    begin
      with Control as TListbox do
      begin
        Canvas.FillRect(Rect);//填充以前画的内容
        try
          Icon := TIcon.Create;
          SHGetFileInfo(PChar(bsSkinListBox1.Items.Strings[Index]),
            0,
            SHFileInfo,
            sizeof(SHFileInfo),
            SHGFI_ICON or SHGFI_SMALLICON);//取得当前程序的小图标
          Icon.Handle := SHFileInfo.hIcon;
          Canvas.Draw(Rect.Left, Rect.Top, Icon);//画出这个图标
          icon.Free;
        except
        end;
        // erase
        // draw item
    //显示当前Item的文字
        Canvas.TextOut(18, Rect.Top,
          bsSkinListBox1.Items[Index]);//    InflateRect (Rect, -1, -1);
    //处理状态为激活或选中时的样子
        if (odFocused in State) and (odSelected in State) then
          Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); //
      end;
    end;
      

  11.   

    我都说过了,我用的代码很简单:
    首先,初始化listbox.canvas
      With Self.ListBox1.Canvas Do
        Begin
          Brush.Color := clHotLight;
          Brush.Style := bsSolid;
          Font.Size := 22;      //    TextAddWidth := TextWidth(Copy(Self.ComboBox1.Items[1],1,13));
          Font.Style := [fsBold];
          Font.Color := Self.LabeledEdit2.Color;
          Font.Charset := GB2312_CHARSET;      TextAddWidth := TextWidth('0123456789ABC');
          TextAddHeight := TextHeight(Self.ComboBox1.Items[1]);
        End;
    其次 ,写屏 当然这都是写成函数供调用的.
      With Self.ListBox1.Canvas Do
        Begin
          theRect := Rect(X, Y, X + TextAddWidth, Y + TextAddHeight);
          TextRect(theRect, X, Y, Str);
        End;
      

  12.   

    突然发现,我的代码并未对listbox的item写数据,所以放在onDrawItem里面的代码都是不执行的.
      

  13.   

    今天换了个显示控件将listbox换成image,其他都未变,程序在前后台切换时,显示的数据不会再丢失了,但是又出来一个新问题,每当数据用TextRect()更新时,屏幕上的image区域有明显闪烁现象,虽然时间很短,但总觉得不爽,因为数据的估计5秒钟就刷新一次.
      

  14.   

    在 formcreate事件中加入:
      Self.DoubleBuffered := True;
      

  15.   

    to keyz:
    加入聊DoubleBuffered了
    好像没有效也,老兄.