Mainfrm.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit Mainfrm;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, DB, DBTables, Grids, DBGrids, ExtCtrls, DBCtrls, ExtDlgs;
  6. type
  7.   TfrmMain = class(TForm)
  8.     Table1: TTable;
  9.     DataSource1: TDataSource;
  10.     Panel1: TPanel;
  11.     DBGrid1: TDBGrid;
  12.     DBImage1: TDBImage;
  13.     DBNavigator1: TDBNavigator;
  14.     OpenPictureDialog1: TOpenPictureDialog;
  15.     procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  16.       DataCol: Integer; Column: TColumn; State: TGridDrawState);
  17.     procedure DBImage1DblClick(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     { Public declarations }
  22.   end;
  23. var
  24.   frmMain: TfrmMain;
  25. implementation
  26. {$R *.dfm}
  27. //实现根据动物的重量分三个级别,用不同的颜色表示
  28. procedure TfrmMain.DBGrid1DrawColumnCell(Sender: TObject;
  29.   const Rect: TRect; DataCol: Integer; Column: TColumn;
  30.   State: TGridDrawState);
  31. begin
  32.   if DBGrid1.DataSource.DataSet.FieldByName('Weight').AsInteger <= 10 then
  33.     DBGrid1.Canvas.Font.Color := clRed
  34.   else if DBGrid1.DataSource.DataSet.FieldByName('weight').AsInteger <= 20 then
  35.     DBGrid1.Canvas.Font.Color := clBlue
  36.   else
  37.     DBGrid1.Canvas.Font.Color := clGreen;
  38.   DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, state);
  39. end;
  40. //双击DBImage1组件,存取图片
  41. procedure TfrmMain.DBImage1DblClick(Sender: TObject);
  42. begin
  43. //判断数据集是否处于编辑或添加状态
  44.   if (DBImage1.DataSource.DataSet.State  <> dsEdit) and
  45. (DBImage1.DataSource.DataSet.State  <> dsInsert) then
  46.   raise Exception.Create('数据集不处于编辑或添加状态');
  47.   if OpenPictureDialog1.Execute then
  48.   DBImage1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  49. end;
  50. end.