unit LibVCLs;
interface
uses
Classes, StdCtrls, ComCtrls, Windows, Messages, SysUtils,dbTables,
Typinfo, WinProcs, Graphics, Controls, Forms, Db,dbgrids,
dbclient;
type
TableEvent = array[1..10] of TMethod ;
function CopyTreeNode(xSource, xDest: TTreeNode; Mode: TNodeAttachMode): TTreeNode;
function CheckActive( xTable : TDataSet ) : Boolean ; // 浪琩郎琌竒秨币Τ戈
function GetObjectMethod (xComponent: TComponent; xMethod : string):TMethod;
function GetStringProperty (xComponent: TComponent; xProperty: string):string;
function GetOrdinalProperty(xComponent: TComponent; xProperty: string):Integer;
function GetVariantProperty(xComponent: TComponent; xProperty: string):variant;
procedure SetObjectMethod (xComponent: TComponent; xMethod : string; xValue: TMethod);
procedure SetStringProperty (xComponent: TComponent; xProperty: string; xValue: string);
procedure SetOrdinalProperty(xComponent: TComponent; xProperty: string; xValue: Longint);
function DataSetOf(xComponent :TComponent) : TDataset;
function DataSourceOf(xComponent :TComponent) : TDataSource;
procedure CloseTableEvent( xDataSource : TDataSource ; var xEventRecord : TableEvent ) ;
procedure RestoreTableEvent( xDataSource : TDataSource ; var xEventRecord : TableEvent ) ;
procedure FindChangeControl( xParent:TWinControl;var xCtrlList:TStringList; var xLastFocus:TWinControl ) ;
procedure ChangeFieldColor( xForm:TForm; xField,xDot:string; xColor: TColor);
procedure ChangeFieldEnabled(xForm:TForm;xDataSource:TDataSource;xField,xDot:string; Value: Boolean);
procedure ResizeGridColumns(xGrid: TDBGrid); // 穝﹚竡 Grid Column 糴
procedure MoveSelectedItems(xSource, xDest: TCustomListBox);
procedure CopySelectedItems(xSource, xDest: TCustomListBox);
procedure MoveAllItems(xSource, xDest: TCustomListBox);
procedure FreeListObj(xControl:TWinControl); // 睲埃List ┪ ComboBox いObject;
procedure FreeAllListObj(xForm:TForm); // 睲埃Formい┮ΤListのComboObject;
procedure ListBoxToVarArray(xBox: TCustomListBox; var xVar: Variant; xIndex: integer);
function GetControlObject( xTable : TClientDataSet; const xField : string ) : string ;
function SetFieldValue( mField : TField ; mValue : string ) : Boolean ;
procedure BindParameters(Query: TQuery; DeltaData: TClientDataSet); // 峨 Update SQL 把计
function CheckPropertyExist(xComponent: TComponent; xProperty: String): Boolean;
function MyRect(x1,y1,x2,y2:integer):TRect;
implementation
uses
Dialogs, LibStrs;
function GetObjectMethod(xComponent: TComponent; xMethod : string):TMethod;
var
mPropInfo: PPropInfo ;
begin
mPropInfo:=GetPropInfo(xComponent.ClassType.ClassInfo,xMethod);
if mPropInfo<>nil then
Result:=GetMethodProp(xComponent,mPropInfo)
else
begin
Result.Code:=nil;
Result.Data:=nil;
end;
end;
function GetStringProperty(xComponent: TComponent; xProperty: string): string;
var
mPropInfo: PPropInfo;
begin
Result := '';
if (xComponent = nil) or (xProperty = '') then exit;
mPropInfo := GetPropInfo(xComponent.ClassInfo, xProperty);
if (mPropInfo <> nil) then
Result := GetStrProp(xComponent, mPropInfo);
end;
function GetOrdinalProperty(xComponent: TComponent; xProperty: string): Integer;
var
mPropInfo: PPropInfo;
begin
Result := 0;
if (xComponent = nil) or (xProperty = '') then exit;
mPropInfo := GetPropInfo(xComponent.ClassInfo, xProperty);
if (mPropInfo <> nil) then
Result := GetOrdProp(xComponent, mPropInfo);
end;
function GetVariantProperty(xComponent: TComponent; xProperty: string): Variant;
var
mPropInfo: PPropInfo;
begin
Result := 0;
if (xComponent = nil) or (xProperty = '') then exit;
mPropInfo := GetPropInfo(xComponent.ClassInfo, xProperty);
if (mPropInfo <> nil) then
Result := GetVariantProp(xComponent, mPropInfo);
end;
procedure SetStringProperty(xComponent: TComponent; xProperty: string; xValue: string);
var
mPropInfo: PPropInfo;
begin
if (xComponent = nil) or (xProperty = '') then exit;
mPropInfo := GetPropInfo(xComponent.ClassInfo, xProperty);
if (mPropInfo <> nil) then
SetStrProp(xComponent, mPropInfo, xValue)
end;
procedure SetOrdinalProperty(xComponent: TComponent; xProperty: string; xValue: Longint);
var
mPropInfo: PPropInfo;
begin
if (xComponent = nil) or (xProperty = '') then exit;
mPropInfo := GetPropInfo(xComponent.ClassInfo, xProperty);
if (mPropInfo <> nil) then
SetOrdProp(xComponent, mPropInfo, xValue)
end;
procedure SetObjectMethod(xComponent: TComponent; xMethod : string; xValue: TMethod);
var
mPropInfo: PPropInfo;
begin
mPropInfo:=GetPropInfo(xComponent.ClassType.ClassInfo,xMethod);
if mPropInfo<>nil then
SetMethodProp(xComponent,mPropInfo,xValue);
end;
// :
procedure CloseTableEvent( xDataSource : TDataSource ; var xEventRecord : TableEvent ) ;
var
mNilEvent : TMethod ;
begin
mNilEvent.Code := nil ;
mNilEvent.Data := nil ;
xEventRecord[01] := GetObjectMethod( xDataSource.DataSet , 'AfterEdit' ) ;
xEventRecord[02] := GetObjectMethod( xDataSource.DataSet , 'AfterInsert' ) ;
xEventRecord[03] := GetObjectMethod( xDataSource.DataSet , 'AfterOpen' ) ;
xEventRecord[04] := GetObjectMethod( xDataSource.DataSet , 'AfterPost' ) ;
xEventRecord[05] := GetObjectMethod( xDataSource.DataSet , 'BeforeEdit' ) ;
xEventRecord[06] := GetObjectMethod( xDataSource.DataSet , 'BeforeInsert') ;
xEventRecord[07] := GetObjectMethod( xDataSource.DataSet , 'BeforeOpen' ) ;
xEventRecord[08] := GetObjectMethod( xDataSource.DataSet , 'BeforePost' ) ;
xEventRecord[09] := GetObjectMethod( xDataSource , 'onDataChange') ;
xEventRecord[10] := GetObjectMethod( xDataSource , 'onStateChange');
SetObjectMethod( xDataSource.DataSet , 'AfterEdit' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'AfterInsert' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'AfterOpen' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'AfterPost' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeEdit' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeInsert' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeOpen' , mNilEvent) ;
SetObjectMethod( xDataSource.DataSet , 'BeforePost' , mNilEvent) ;
SetObjectMethod( xDataSource , 'onDataChange' , mNilEvent) ;
SetObjectMethod( xDataSource , 'onStateChange', mNilEvent) ;
end ;
// :
procedure RestoreTableEvent( xDataSource : TDataSource ; var xEventRecord : TableEvent ) ;
begin
SetObjectMethod( xDataSource.DataSet , 'AfterEdit' , xEventRecord[01]) ;
SetObjectMethod( xDataSource.DataSet , 'AfterInsert' , xEventRecord[02]) ;
SetObjectMethod( xDataSource.DataSet , 'AfterOpen' , xEventRecord[03]) ;
SetObjectMethod( xDataSource.DataSet , 'AfterPost' , xEventRecord[04]) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeEdit' , xEventRecord[05]) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeInsert' , xEventRecord[06]) ;
SetObjectMethod( xDataSource.DataSet , 'BeforeOpen' , xEventRecord[07]) ;
SetObjectMethod( xDataSource.DataSet , 'BeforePost' , xEventRecord[08]) ;
SetObjectMethod( xDataSource , 'onDataChange' , xEventRecord[09]) ;
SetObjectMethod( xDataSource , 'onStateChange', xEventRecord[10]) ;
end ;
// 穝﹚竡 Grid Column 糴
procedure ResizeGridColumns(xGrid: TDBGrid);
var
I: Integer;
MWidth: Integer;
mUnitWidth: Integer;
begin
{ ㄓ琌 Infopower, Τㄇ Property ぃ, ┮ぃノ
if not xGrid.DataSource.DataSet.Active then exit;
if xGrid.FieldCount = 0 then exit;
MWidth := 0;
for I := 0 to xGrid.FieldCount do // 夹逆
begin
Inc(MWidth); // 娩絬㎝だ筳絬
MWidth := MWidth + xGrid.ColWidths[I];
end;
Inc(MWidth);
MWidth := MWidth + GetSystemMetrics(SM_CYHSCROLL) + 1; // 辈禸
if MWidth < xGrid.ClientWidth then
begin
mUnitWidth := (xGrid.ClientWidth - MWidth) div xGrid.FieldCount;
for I := 1 to xGrid.FieldCount do // 夹逆ぃ衡,眖 1 秨﹍
xGrid.ColWidths[I] := xGrid.ColWidths[I] + mUnitWidth;
end
else
xGrid.Parent.ClientWidth := MWidth + 5; // ぃ琵绢禸瞷,翴}
end;
procedure MoveSelectedItems(xSource, xDest: TCustomListBox);
var
I: Integer;
begin
for I := xSource.Items.Count - 1 downto 0 do
if xSource.Selected[I] then
begin
xDest.Items.Add(xSource.Items[I]);
xSource.Items.Delete(I);
end;
end;
procedure CopySelectedItems(xSource, xDest: TCustomListBox);
var
I: Integer;
begin
for I := 0 to xSource.Items.Count - 1 do
if xSource.Selected[I] then
if xDest.Items.IndexOf(xSource.Items[I]) < 0 then
xDest.Items.Add(xSource.Items[I]);
end;
procedure MoveAllItems(xSource, xDest: TCustomListBox);
var
I: Integer;
begin
for I := xSource.Items.Count - 1 downto 0 do
begin
xDest.Items.Add(xSource.Items[I]);
xSource.Items.Delete(I);
end;
end;
function CopyTreeNode(xSource, xDest: TTreeNode; Mode: TNodeAttachMode): TTreeNode;
var
mNode: TTreeNode;
I: Integer;
begin
// Result := nil;
// 玻ネ糷
mNode := nil;
case Mode of
naAdd: mNode := xDest.Owner.Add(xDest, xSource.Text);
naAddFirst: mNode := xDest.Owner.AddFirst(xDest, xSource.Text);
naAddChild: mNode := xDest.Owner.AddChild(xDest, xSource.Text);
naAddChildFirst: mNode := xDest.Owner.AddChildFirst(xDest, xSource.Text);
naInsert: mNode := xDest.Owner.Insert(xDest, xSource.Text);
end;
// 玻ネ┮Τ甝糷
for I := 0 to xSource.Count - 1 do
CopyTreeNode(xSource.Item[I], mNode, naAddChild);
Result := mNode;
end;
function CheckEmpty(aForm:TForm; xDataSet:TDataSet):boolean;// 浪琩ぃフ逆, SetFocus
var
I: Integer;
begin
Result:=True;
if (xDataSet=nil) then exit ;
with xDataSet do
for I := 0 to FieldCount -1 do
if Fields[i].Required and (Fields[i].FieldKind = fkData) and (Fields[i].AsString='') then
begin
Fields[i].FocusControl;
MessageDlg('['+Fields[i].DisplayLabel+'] value required!',mtError,[mbOK],0);
Result:=False;
Break;
end;
end;
function CheckActive( xTable : TDataSet ) : Boolean ;
begin
Result := False ;
if xTable=nil then Exit;
with xTable do
try
if not Active then Exit;
Result := ( State in [dsBrowse] ) and not ( EOF and BOF ) ;
except
Result := False ;
end ;
end ;
procedure FreeListObj(xControl: TWinControl);
var
i:integer;
begin
with xControl do
begin
if xControl is TCustomListBox then //TListBox
if TCustomListBox(xControl).items.count<>0 then //Τobjects
for i:=TCustomListBox(xControl).items.count -1 downto 0 do
TCustomListBox(xControl).Items.delete(i);
if xControl is TComboBox then //TComboBox
if TComboBox(xControl).Items.Objects[0]<>nil then //Τobjects
for i:=TComboBox(xControl).items.count -1 downto 0 do
TComboBox(xControl).Items.delete(i);
end;
end;
procedure FreeAllListObj(xForm: TForm);
var
i:integer;
begin
with xForm do
for i:=0 to ControlCount-1 do
if Controls[i] is TWinControl then
FreeListObj(TWinControl(Controls[i]));
end;
procedure ListBoxToVarArray(xBox: TCustomListBox; var xVar: Variant; xIndex: integer);
var
i: integer;
mVal: string;
begin
mVal:= '';
for i:= 0 to xBox.items.count - 1 do
mVal:= mVal+ xBox.items[i]+ ',';
xVar[xIndex]:= copy(mVal, 1, length(mVal)-1);
end;
procedure FindChangeControl( xParent:TWinControl;var xCtrlList:TStringList; var xLastFocus:TWinControl ) ;
type
LastControl = record
name: string;
TabOrder: Integer;
end;
function Findinclude(mParent: TWinControl): string;
var
// mi: Integer;
// mj: Integer;
mObj: TWinControl;
begin
mObj := mParent ;
{
mj := 0;
for mi := 0 to mParent.ControlCount - 1 do
if mParent.Controls[mi] is TWinControl then
with TWinControl(mParent.Controls[mi]) do
if Visible and (TabOrder > mj) then
begin
mj := TabOrder;
mObj := TWinControl(mParent.Controls[mi]);
end;
}
Result := mObj.name;
end;
var
mSearch: array[0..9] of LastControl;
I: Integer;
J: Integer;
mComponent : TComponent ;
begin
FillChar(mSearch, SizeOf(mSearch), #0);
for I := 0 to xParent.Owner.ComponentCount - 1 do
begin
mComponent := xParent.Owner.Components[I] ;
if mComponent is TWinControl then
with TWinControl(mComponent) do
if Visible and (Parent.Parent = xParent ) then
begin
J := TTabSheet(Parent).PageIndex;
if (mSearch[J].TabOrder < TabOrder) or (Trim(mSearch[J].name) = '') then
begin
mSearch[J].name := Findinclude(TWinControl(mComponent)) ;
mSearch[J].TabOrder := TabOrder;
end;
end;
end ;
with TPageControl( xParent ) do
begin
J := PageCount ;
if CompareText( Pages[J-1].Name , 'TbsBrowse' ) = 0 then Dec( J ) ;
for I := 0 to 9 do
if (mSearch[I].name <> '') and ( I < J) then
begin
xCtrlList.Add(mSearch[I].name);
xLastFocus := TWinControl(xParent.Owner.FindComponent(mSearch[I].name));
end ;
end ;
end;
// э跑 Key 逆肅︹
procedure ChangeFieldColor( xForm:TForm; xField,xDot:string; xColor: TColor);
var
J : Integer;
mField : string;
begin
while ( xField<>'' ) do
begin
mField := Trim( CutToken(xField, xDot) );
if (mField = '') then exit ;
for J := 0 to xForm.ComponentCount - 1 do
if (CompareText(GetStringProperty(xForm.Components[J], 'DataField' ), mField) = 0) then
SetOrdinalProperty(xForm.Components[J],'Color', xColor)
end;
end;
procedure ChangeFieldEnabled(xForm:TForm;xDataSource:TDataSource;xField,xDot:string; Value: Boolean);
var
J : Integer;
mField : string;
begin
while ( xField<>'' ) do
begin
mField := Trim( CutToken(xField, xDot) );
mField := Trim( CutToken(mField, ' ' ) ); // 穦Τ患糤┪患搭北
for J := 0 to xForm.ComponentCount - 1 do
begin
if CompareText(mField, GetStringProperty(xForm.Components[J], 'DataField')) = 0 then
begin
if xDataSource=DataSourceOf(xForm.Components[J]) then
begin
if not Value then
SetOrdinalProperty(xForm.Components[J], 'Enabled', 0)
else
SetOrdinalProperty(xForm.Components[J], 'Enabled', 1);
end;
end;
end;
end;
end;
function GetControlObject( xTable : TClientDataSet; const xField : string ) : string ;
var
mValue : string ;
mPtr : Integer;
mField : string ;
begin
mPtr := 0 ;
Result := '' ;
{ TwwClientDataSet property ControlType, 硂柑⊿Τ, ┮ぃノ
with xTable.ControlType do
while ( mPtr < Count ) and ( Result = '' ) do
begin
mValue := Strings[mPtr] ;
mField := Cuttoken( mValue , ';' ) ;
if CompareText( mField , xField ) = 0 then
begin
Cuttoken( mValue , ';' ) ;
Result := Cuttoken( mValue , ';' ) ;
end ;
Inc( mPtr ) ;
end ;}
end ;
function SetFieldValue( mField : TField ; mValue : string ):Boolean ;
var
mReadOnly : Boolean ;
begin
Result := False ;
if mField = nil then exit ;
with mField do
begin
mReadOnly := ReadOnly ;
ReadOnly := False ;
try
AsString := mValue ;
Result := True ;
finally
ReadOnly := mReadOnly ;
end ;
end ;
end ;
// 峨 Update SQL 把计
procedure BindParameters(Query: TQuery; DeltaData: TClientDataSet);
var
I: Integer;
Old: Boolean;
Param: TParam;
PName: string;
Field: TField;
Value: Variant;
begin
with Query do
for I := 0 to Params.Count - 1 do
begin
Param := Params[I];
PName := Param.Name;
Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0;
if Old then System.Delete(PName, 1, 4);
Field := DeltaData.FindField(PName);
if not Assigned(Field) then Continue;
if Old then
Param.AssignFieldValue(Field, Field.OldValue)
else
begin
Value := Field.NewValue;
{ When we have no new value, use the old value }
if VarIsEmpty(Value) then Value := Field.OldValue;
Param.AssignFieldValue(Field, Value);
end;
end;
end;
//---------------------------------------------------------
// 眔DATASET PROPERTY
//---------------------------------------------------------
function DataSetof(xComponent :TComponent) : TDataset;
var
mDataSourcePropInfo,mDataSetPropInfo : PPropInfo ;
mDataSource :TDataSource;
begin
mDataSourcePropInfo:= Typinfo.GetPropInfo(xComponent.ClassInfo,'DataSource');
Result := nil;
if mDataSourcePropInfo <> Nil then
begin
mDataSetPropInfo := nil;
mDataSource :=TDataSource(GetOrdProp(xComponent, mDataSourcePropInfo));
mDataSetPropInfo :=Typinfo.GetPropInfo(mDataSource.ClassInfo,'DataSet');
if mDataSetPropInfo <> nil then
begin
Result := TDataSet(GetOrdProp(mDataSource, mDataSetPropInfo)) ;
end;
end;
end;
//---------------------------------------------------------
// 眔DATASource PROPERTY
//---------------------------------------------------------
function DataSourceOf(xComponent :TComponent) : TDataSource;
var
mDataSourcePropInfo:PPropInfo ;
mDataSource :TDataSource;
begin
Result := nil;
mDataSourcePropInfo:= Typinfo.GetPropInfo(xComponent.ClassInfo,'DataSource');
if mDataSourcePropInfo <> nil then
Result:=TDataSource(GetOrdProp(xComponent, mDataSourcePropInfo));
end;
function CheckPropertyExist(xComponent: TComponent; xProperty: String): Boolean;
var
mPropInfo: PPropInfo;
begin
Result:= False;
mPropInfo:= nil;
if not((xComponent= nil)or(xProperty= '')) then
mPropInfo:= GetPropInfo(xComponent.ClassInfo, xProperty);
Result:= mPropInfo<> nil;
end;
function MyRect(x1,y1,x2,y2:integer):TRect;
begin
Result:=Rect(x1,y1,x2,y2);
end;
end.