unit1.pas

来自「Delphi7编程80例(完全版)」· PAS 代码 · 共 70 行

PAS
70
字号
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Memo1: TMemo;
    Button1: TButton;
    procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  lpstr:PChar;
  c,h:integer;
  tc:TRect;
begin
  with Control as TListBox do
  begin
    c:=length(items[index]);
    lpstr:=PChar(Items[index]);
    tc:=clientrect;
    h:=drawtext(Canvas.Handle,lpstr,c,tc,DT_CALCRECT or DT_WORDBREAK);
  end;
  Height:=h+4;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  listbox1.Items.Assign(memo1.Lines);
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  lpstr:PChar;
  c:integer;
begin
  with Control as TListBox do
  begin
    Canvas.FillRect(Rect);
    c:=length(items[index]);
    lpstr:=PChar(Items[index]);
    drawtext(Canvas.Handle,lpstr,c,Rect,DT_WORDBREAK);
  end;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?