delphi 开发使用的基类B

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のComboObject;
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.
 

与大家分享一个Delphi 7自定义单元,完成自定义消息和自定义进度条的显示,效果可参见截图所示。在源代码中,showmessage是弹出消息提示窗口,showprogress是显示进度条,为了更好的看到效果,本示例显示进度条正在运行的状态,进度条和弹出消息框,作者:周劲羽   该单元提供以下几个过程用于显示动态提示窗体:   ShowProgress - 显示进度条窗体   HideProgress - 隐藏进度条窗体   UpdateProgress - 更新当前进度   UpdateProgressTitle - 更新窗体标题   使用方法:在需要显示提示窗口的单元中uses本单元,当需要显示提示信息时直接调用ShowXXXX过程即可。   注意事项:同一时间屏幕上只能显示一个进度窗体,窗体显示时其它所有窗体均不能使用,但显示该窗体的代码仍可以继续运行。   来看一下参数如何定义:   procedure ShowProgress(const Title: string; AMax: Integer = 100; vIsShowProgress: Boolean = false);   {* 显示进度条窗体,参数为窗体标题以及最大值,默认 100(百分比形式),可自定义成其他值}   procedure HideProgress;   {* 关闭进度条窗体}   procedure UpdateProgress(Value: Integer);   {* 更新当前进度,参数为进度值:当 Max 为 100 时可接受范围为 0..100,此时 Value 代表百分比}   procedure UpdateProgressTitle(const Title: string);   {* 更新进度条窗体标题,参数为标题}   procedure UpdateProgressMax(Value: Integer);   {* 更新进度条最大值,参数为新的最大值}   implementation   {$R *.DFM}   var    ProgressForm: TProgressForm = nil; // 进度条窗体实例    FormList: Pointer; // 被禁用的窗体列表指针   详细的单元使用方法,请下载查看源码中的代码文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值