在Listboxes加背景图

来源:计算机等级考试    发布时间:2012-08-29    计算机等级考试视频    评论

  编辑特别推荐:

  全国计算机等级考试(等考)指定教材

  全国计算机等级考试学习视频

  全国计算机等级考试网上辅导招生

  全国计算机等级考试时间及科目预告

  考试大教育全国计算机等级考试在线测试平台

  全国计算机等级考试资料下载

  全国计算机等级考试论坛

  计算机等级考试四级应用题解析汇总

  2009年下半年全国计算机二级考试时间是2009年9月19日至23日。2009年下半年全国计算机二级考试报名时间会在考前两个月开始报名!更多优质资料尽在考试大论坛 考试大在线题库

  建立一个窗体
  2. 放一个ComboBox和Listbox
  3. 改变Component的Style为csOwnerDrawVariable和ListBox的Style为lbOwnerDrawVariable。
  4. 声明5个Tbitmap的全局变量
  5. 覆盖Form的OnCreate.
  6. 覆盖ComboBox的OnDraw.
  7. 覆盖ComboBox的OnMeasureItem.
  8. 释放资源在Form的OnClose.
  Unit Ownerdrw;
  interface
  uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;
  type
  TForm1 = class(Tform)
  ComboBox1: TComboBox;
  ListBox1: TListBox;
  procedure FormCreate(Sender: Tobject);
  procedure FormClose(Sender: Tobject; var Action: TCloseAction);
  procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: Trect; State: TOwnerDrawState);
  procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
  procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: Trect; State: TOwnerDrawState);
  procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
  private
  { Private declarations }
  public
  { Public declarations }
  end;
  var
  Form1: TForm1;
  TheBitmap1, TheBitmap2, TheBitmap3, TheBitmap4,
  TheBitmap5 : Tbitmap;
  implementation
  {$R *.DFM}
  procedure TForm1.FormCreate(Sender: Tobject);
  begin
  TheBitmap1 := Tbitmap.Create;
  TheBitmap1.LoadFromFile(C://delphi//images//buttons//globe.bmp);
  TheBitmap2 := Tbitmap.Create;
  TheBitmap2.LoadFromFile(C://delphi//images//buttons//video.bmp);
  TheBitmap3 := Tbitmap.Create;
  TheBitmap3.LoadFromFile(C://delphi//images//buttons//gears.bmp);
  TheBitmap4 := Tbitmap.Create;
  TheBitmap4.LoadFromFile(C://delphi//images//buttons//key.bmp);
  TheBitmap5 := Tbitmap.Create;
  TheBitmap5.LoadFromFile(C://delphi//images//buttons//tools.bmp);
  ComboBox1.Items.AddObj (Bitmap1: Globe, TheBitmap1);
  ComboBox1.Items.AddObject(Bitmap2: Video, TheBitmap2);
  ComboBox1.Items.AddObject(Bitmap3: Gears, TheBitmap3);
  ComboBox1.Items.AddObject(Bitmap4: Key, TheBitmap4);
  ComboBox1.Items.AddObject(Bitmap5: Tools, TheBitmap5);
  ListBox1.Items.AddObject(Bitmap1: Globe, TheBitmap1);
  ListBox1.Items.AddObject(Bitmap2: Video, TheBitmap2);
  ListBox1.Items.AddObject(Bitmap3: Gears, TheBitmap3);
  ListBox1.Items.AddObject(Bitmap4: Key, TheBitmap4);
  ListBox1.Items.AddObject(Bitmap5: Tools, TheBitmap5);
  end;
  procedure TForm1.FormClose(Sender: Tobject; var Action: TCloseAction);
  begin
  TheBitmap1.Free;
  TheBitmap2.Free;
  TheBitmap3.Free;
  TheBitmap4.Free;
  TheBitmap5.Free;
  end;
  procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: Trect; State: TOwnerDrawState);
  var
  Bitmap: Tbitmap;
  Offset: Integer;
  begin
  with (Control as TComboBox).Canvas do
  begin
  FillRect(Rect);
  Bitmap := Tbitmap(ComboBox1.Items.Objects[Index]);
  if Bitmap $#@60;$#@62; nil then
  begin
  BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  Bitmap.Height), clRed);
  Offset := Bitmap.width + 8;
  end;
  { display the text }
  TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index])
  end;
  end;
  procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index:
  Integer; var Height: Integer);
  begin
  height:= 20;
  end;
  procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: Trect; State: TOwnerDrawState);
  var
  Bitmap: Tbitmap;
  Offset: Integer;
  begin
  with (Control as TListBox).Canvas do
  begin
  FillRect(Rect);
  Bitmap := Tbitmap(ListBox1.Items.Objects[Index]);
  if Bitmap $#@60;$#@62; nil then
  begin
  BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  Bitmap.Height), clRed);
  Offset := Bitmap.width + 8;
  end;
  { display the text }
  TextOut(Rect.Left + Offset, Rect.Top, Listbox1.Items[Index])
  end;
  end;
  procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
  begin
  height:= 20;
  end;
  end.
  //该窗体的DFM文件
  object Form1: TForm1
  Left = 211
  Top = 155
  Width = 435
  Height = 300
  Caption = Form1
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = System
  Font.Style = []
  PixelsPerInch = 96
  OnClose = FormClose
  OnCreate = FormCreate
  TextHeight = 16
  object ComboBox1: TComboBox
  Left = 26
  Top = 30
  Width = 165
  Height = 22
  Style = csOwnerDrawVariable
  ItemHeight = 16
  TabOrder = 0
  OnDrawItem = ComboBox1DrawItem
  OnMeasureItem = ComboBox1MeasureItem
  end
  object ListBox1: TListBox
  Left = 216
  Top = 28
  Width = 151
  Height = 167
  ItemHeight = 16
  Style = lbOwnerDrawVariable
  TabOrder = 1
  OnDrawItem = ListBox1DrawItem
  OnMeasureItem = ListBox1MeasureItem
  end
  end

  2009年9月全国计算机等级考试时间及科目预告

  2009年NCRE考试有新变化

  2009年全国计算机等级考试大纲

  全国计算机等级考试历年真题及答案

  2009年上半年全国计算机等级考试试题及答案

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答