⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.~pas

📁 Delphi面向对象编程思想附书源码 好用哦!
💻 ~PAS
字号:
unit MainFrm;

interface

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

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ListBox1: TListBox;
    btnAdd: TButton;
    btnDelete: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure btnAddClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
  private
    FItems:TStrings;
  public
    Property MyItems:TStrings read FItems write FItems;
  end;

var
  Form1: TForm1;
  Pic1,Pic2,Pic3,Pic4,Pic5:TBitmap;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Pic1:=TBitmap.Create;
  Pic1.LoadFromFile('s1.bmp');
  Pic2:=TBitmap.Create;
  Pic2.LoadFromFile('s2.bmp');
  Pic3:=TBitmap.Create;
  Pic3.LoadFromFile('s3.bmp');
  Pic4:=TBitmap.Create;
  Pic4.LoadFromFile('s4.bmp');
  Pic5:=TBitmap.Create;
  Pic5.LoadFromFile('s5.bmp');
  MyItems:=TStringlist.Create;
  with MyItems do
  begin
    AddObject('航空母舰',Pic1);
    AddObject('战列舰',Pic2);
    AddObject('巡洋舰',Pic3);
    AddObject('护卫舰',Pic4);
    AddObject('驱逐舰',Pic5);
  end;
  ComboBox1.Items:=MyItems;
  ComboBox1.DropDownCount:=5;
  ComboBox1.ItemIndex:=0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var i:Integer;
begin
 for i:=1 to 5 do
  FindComponent('Pic'+ IntToStr(i)).Free;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
 Pic:TBitmap;
 LeftOffset,TopOffset:Integer;
begin
 with TComboBox(Control).Canvas do
  begin
   FillRect(Rect);
   Pic:=TBitmap(MyItems.Objects[Index]);
   if Pic<>nil then
    begin
     BrushCopy(Bounds(Rect.Left+2,Rect.Top+2,Pic.Width,Pic.Height),
     Pic,Bounds(0,0,Pic.Width,Pic.Height),clRed);
     LeftOffset:=Pic.Width+8;
     TopOffset:=Pic.Height div 2;
    end;
    TextOut(Rect.Left+LeftOffset,Rect.Top+TopOffset,MyItems[Index]);
  end;
end;

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
  Height:=Pic1.Height;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var i:Integer;
begin
 for i:=1 to 5 do
 TBitmap(FindComponent('Pic'+ IntToStr(i))).Free;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
 Pic:TBitmap;
 LeftOffset,TopOffset:Integer;
begin
 with TListBox(control).Canvas do
  begin
   FillRect(Rect);
   Pic:=TBitmap((Control as TListBox).Items.Objects[Index]);
   if Pic<>nil then
    begin
     BrushCopy(Bounds(Rect.Left+2,Rect.Top+2,Pic.Width,Pic.Height),
     Pic,Bounds(0,0,Pic.Width,Pic.Height),clRed);
     LeftOffset:=Pic.Width+8;
     TopOffset:=Pic.Height div 2;
    end;
    TextOut(Rect.Left+LeftOffset,Rect.Top+TopOffset,(Control as TListBox).Items[Index]);
  end;
end;

procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
  Height:=Pic1.Height;
end;

procedure TForm1.btnAddClick(Sender: TObject);
var i:integer;
begin
  i:=ComboBox1.ItemIndex;
  ListBox1.Items.AddObject(ComboBox1.Items[i],ComboBox1.Items.Objects[i]);
end;

procedure TForm1.btnDeleteClick(Sender: TObject);
var i:integer;
begin
  i:=ListBox1.ItemIndex;
  ListBox1.Items.Delete(i);
end;

end.

⌨️ 快捷键说明

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