DBGrid компонент, который отображает разными цветами
DBGrid компонент, который отображает разными цветами
DbGrid компонент, который отображает разными цветами удалённые, обновлённые и добавленные записи unit atcdbgrid; (c) aveen tech 2001 - 2002 filename: atcdbgrid.pas version date author comment 1.0 13/06/2000 majid vafai jahan create. overview - this grid is inherited from dbgrid and add some required functionality to it. functionality: - record type are all records that may be modified, unmodified, inserted, deleted. - coloring according to record type. - show selected record type. *) interface uses windows, messages, sysutils, classes, graphics, controls, forms, dialogs, grids, dbgrids, dbtables, db; const alignflags : array [talignment] of integer = ( dt_left or dt_wordbreak or dt_expandtabs or dt_noprefix, dt_right or dt_wordbreak or dt_expandtabs or dt_noprefix, dt_center or dt_wordbreak or dt_expandtabs or dt_noprefix ); rtl: array [boolean] of integer = (0, dt_rtlreading); type tcachedshow = (csmodify, csunmodify, csremoved, csinserted, csall, csnormal); tatcdbgrid = class(tdbgrid) private fcachedshow: tcachedshow; fmodifiedcolor: tcolor; finsertedcolor: tcolor; fdeletedcolor: tcolor; procedure setcachedshow(const value: tcachedshow); protected procedure drawdatacell(const rect: trect; field: tfield; state: tgriddrawstate); override; procedure drawcolumncell(const rect: trect; datacol: integer; column: tcolumn; state: tgriddrawstate); override; public constructor create(aowner: tcomponent); override; published property atccachedshow: tcachedshow read fcachedshow write setcachedshow; property atcdeletedcolor: tcolor read fdeletedcolor write fdeletedcolor; property atcinsertedcolor: tcolor read finsertedcolor write finsertedcolor; property atcmodifiedcolor: tcolor read fmodifiedcolor write fmodifiedcolor; end; procedure register; implementation (******************************************************************************) procedure register; begin registercomponents('atc db compo', [tatcdbgrid]); end; (******************************************************************************) constructor tatcdbgrid.create(aowner: tcomponent); (* description: record type showing is all except deletes. *) begin inherited; fcachedshow := csnormal; fdeletedcolor := clgray; finsertedcolor := claqua; fmodifiedcolor := clred; end; (******************************************************************************) procedure tatcdbgrid.drawcolumncell(const rect: trect; datacol: integer; column: tcolumn; state: tgriddrawstate); (* description: on drawing column color updated records. *) var arect: trect; begin inherited; if not assigned(column.field) then exit; // copy rect into variable. copyrect(arect, rect); if assigned(datalink) and (datalink.active) and (datalink.dataset <> nil) then begin // если текущая запись изменена if datalink.dataset.updatestatus = usmodified then begin canvas.brush.color := atcmodifiedcolor; canvas.font.color := clblack; canvas.fillrect(rect); drawtext(canvas.handle, pchar(column.field.text), length(column.field.text), arect, alignflags[column.alignment] or rtl[userighttoleftalignmentforfield(column.field, column.alignment)]); end // если текущая запись добавлена. else if datalink.dataset.updatestatus = usinserted then begin canvas.brush.color := atcinsertedcolor; canvas.font.color := clblack; canvas.fillrect(rect); drawtext(canvas.handle, pchar(column.field.text), length(column.field.text), arect, alignflags[column.alignment] or rtl[userighttoleftalignmentforfield(column.field, column.alignment)]); end // если текущая запись удалена. else if datalink.dataset.updatestatus = usdeleted then begin canvas.brush.color := atcdeletedcolor; canvas.font.color := clwhite; canvas.fillrect(rect); drawtext(canvas.handle, pchar(column.field.text), length(column.field.text), arect, alignflags[column.alignment] or rtl[userighttoleftalignmentforfield(column.field, column.alignment)]); end; end; end; (******************************************************************************) procedure tatcdbgrid.drawdatacell(const rect: trect; field: tfield; state: tgriddrawstate); (* Описание: Рисуем ячейки *) var arect: trect; begin inherited; copyrect(arect, rect); if assigned(datalink) and (datalink.active) and (datalink.dataset <> nil) then begin // если текущая запись изменена if datalink.dataset.updatestatus = usmodified then begin canvas.brush.color := clred; canvas.font.color := clblack; canvas.fillrect(rect); drawtext(canvas.handle, pchar(field.text), length(field.text), arect, alignflags[field.alignment] or rtl[userighttoleftalignmentforfield(field, field.alignment)]); end // если текущая запись добавлена. else if datalink.dataset.updatestatus = usinserted then begin canvas.brush.color := claqua; canvas.font.color := clblack; canvas.fillrect(rect); drawtext(canvas.handle, pchar(field.text), length(field.text), arect, alignflags[field.alignment] or rtl[userighttoleftalignmentforfield(field, field.alignment)]); end // если текущая запись удалена. else if datalink.dataset.updatestatus = usdeleted then begin canvas.brush.color := clgray; canvas.font.color := clwhite; canvas.fillrect(rect); drawtext(canvas.handle, pchar(field.text), length(field.text), arect, alignflags[field.alignment] or rtl[userighttoleftalignmentforfield(field, field.alignment)]); end; end; end; (******************************************************************************) procedure tatcdbgrid.setcachedshow(const value: tcachedshow); (* description: record type for showing in grid. parameters: value cached record show. *) begin fcachedshow := value; if componentstate = [csdesigning] then exit; if not assigned(datasource) or not assigned(datasource.dataset) then exit; // для показа только выбранного типа записей. if assigned(datalink) and assigned(datalink.dataset) and (datalink.active) then begin case fcachedshow of csall: tbdedataset(datasource.dataset).updaterecordtypes := [rtmodified, rtinserted, rtdeleted, rtunmodified]; csmodify: tbdedataset(datasource.dataset).updaterecordtypes := [rtmodified]; csunmodify: tbdedataset(datasource.dataset).updaterecordtypes := [rtunmodified]; csinserted: tbdedataset(datasource.dataset).updaterecordtypes := [rtinserted]; csremoved: tbdedataset(datasource.dataset).updaterecordtypes := [rtdeleted]; csnormal: tbdedataset(datasource.dataset).updaterecordtypes := [rtmodified, rtinserted, rtunmodified]; end; end; end; (******************************************************************************) end.