在进销存等数据库系统中,在数据查询统计及打印报表时,根据用户要求,需要提供彩色数据表格和灰度报表,以便用户能够直观了解不同的分类数据信息,通过Delphi和Windows底层开发技术,我们可以自定义数据表格组件,实现在同一表格中对不同类别的数据显示不同的颜色;我们还可以重新设计报表,开发出能够定制数据行显示不同灰度的报表组件。下面分别阐述如下。
大多数的数据浏览控件是从标准控件中演变过来的,它们具有一些相似的特性,都是通过TDataSource控件连接到TTable或者TQuery控件,尤其是TDBGrid控件,它可以以网格的形式显示数据库表中全部记录的所有字段信息。但是TDBGrid控件的Color属性只能从整体上修改网格显示的颜色,如果要对应不同的数据行根据需求的变化灵活显示不同的颜色,就需要我们通过编程实现了。这里要用到DefalultDrawing这个属性,该属性是布尔型属性,它用于控制网格中各网格单元的绘制方式。在缺省情况下,该属性的值为True,也就是说Delphi使用网格本身缺省的方法绘制网格中各网格单元,并填充各网格单元中的内容,各网格单元中的数据根据其对应的字段控件的DisplayFormat属性和EditFormat属性进行显示和绘制。如果DefalultDrawing属性被设置为False,Delphi不会自动地绘制网格中各网格单元和网格单元中的数据,用户必须自己为TDBGrid控件的OnDrawDataCell事件编写相应的程序用于绘制各网格单元和其中的数据。具体程序代码如下:
……
procedure TFZhanKuanFenLeiRep.DBGrid2DrawDataCell (Sender: TObject;const Rect: TRect; Field: TField; State: TGridDrawState); var pmbm,qcbm:string; begin pmbm:=datasource1.DataSet.Fields[0].AsString; qcbm:=datasource1.DataSet.Fields[1].AsString; case Length(pmbm) of (在需要的地方显示灰色) 1: (Sender as TDBGrid).Canvas.Brush.Color := clgray; 2: (Sender as TDBGrid).Canvas.Brush.Color := clgray; 4,6,8,10,12: begin if datasource1.DataSet.FieldByName('yjdbz').asstring='0' then if qcbm='0000' then (Sender as TDBGrid).Canvas.Brush.Color := clbtnface (在需要的地方显示白色) else (Sender as TDBGrid).Canvas.Brush.Color := Clwhite else (Sender as TDBGrid).Canvas.Brush.Color := cl3dlight; end end; (用定义的刷子填充矩形) (Sender as TDBGrid).Canvas.FillRect(Rect); (Sender as TDBGrid).Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2,Field.AsString); end; ……
至此,配合程序中的数据,我们就可以用TDBGrid控件来显示和编辑彩色数据表格中的数据了。
下面阐述的是如何显示灰度报表。在Delphi的Qreport组中有Quickrep控件,设置该控件的属性,加入DetailBand,在DetailBand的BeforePrint事件中编写代码如下:
…… procedure TFZhanKuanFenLeiRep.DetailBand1BeforePrint(Sender: TQRCustomBand; var PrintBand: Boolean); var len:integer; st:string; label le1; begin if Trim(SQuickRep1.DataSet['PMBM']) = '0' then begin SetQRShapeColor([QRShape2, QRShape3, QRShape6, QRShape12, QRShape13, QRShape14], clgray); end else case Length(SQuickRep1.DataSet.Fields[0].AsString) of 2: SetQRShapeColor([QRShape2, QRShape3, QRShape6, QRShape12, QRShape13, QRShape14], clgray); 4,6,8,10,12: begin if SQuickRep1.DataSet.Fields[0].AsString=' 合计' then DetailBand1.Color:=clGray else begin len:=Length(SQuickRep1.DataSet.Fields[0].AsString); st:=SQuickRep1.DataSet.Fields[0].AsString; SQuickRep1.DataSet.Next; if not SQuickRep1.DataSet.EOF then begin if st=copy(SQuickRep1.DataSet.Fields[0].AsString,1,len) then SetQRShapeColor([QRShape2, QRShape3, QRShape6, QRShape12, QRShape13, QRShape14], cl3DLight) else SetQRShapeColor([QRShape2, QRShape3, QRShape6, QRShape12, QRShape13, QRShape14], clWhite); // DetailBand1.Color:=clWhite; SQuickRep1.DataSet.Prior; end else SetQRShapeColor([QRShape2, QRShape3, QRShape6, QRShape12, QRShape13, QRShape14], clWhite); // DetailBand1.Color:=clWhite; end; end; end; end; ……
该段代码将查询统计到的数据进行分类,并且分别显示不同的灰度颜色。其中SetQRShapeColor过程为将指定的报表显示格填充指定的颜色,其程序代码编写如下:
procedure SetQRShapeColor(Shapes: array of TQRShape; PColor: TColor); var X: Integer; begin for X := 0 to High(Shapes) do Shapes[X].Brush.Color := PColor; end;
以上程序代码在Delphi6.0中编译通过。
|