自己写的一个超级烂的类!警戒自己以后不要再写出如此糟糕的类!

  1. unit uTEditFill;
  2. interface
  3. uses Windows,SysUtils,Classes,Messages;
  4. const MaxSize = 10000;
  5. type
  6.   TFormHandle = record
  7.      Handle:THandle;
  8.      Title:array [0..256]of Char;
  9.      ClassName:array [0..256]of Char;
  10.      end;
  11.   TEditFill = class
  12.     private
  13.       FHandle:THandle;//目标窗口句柄
  14.       FFormTitle:String;//目标窗口标题
  15.       FData:TList;//写入Edit窗体内的数据
  16.       FDestClassName:String;
  17.       FCount:Integer;//计数器
  18.       procedure SetFormTitle(const Value: string);
  19.       procedure SetHandle(const Value: THandle);
  20.       procedure SetData(const Value: TList);
  21.       function  GetParentHandle():THandle;//设置 
  22.       procedure SetDestClassName(const Value: String);
  23.       procedure SetCount(const Value: Integer);
  24.     public
  25.       constructor Create();
  26.       destructor Destroy();
  27.       function FillEdit():boolean;//填充Edit窗体数据
  28.     published
  29.       property Data:TList read FData write SetData;
  30.       property Count:Integer read FCount write SetCount;
  31.       property Handle:THandle read FHandle write SetHandle;
  32.       property DestClassName:String read FDestClassName write SetDestClassName;
  33.       property FormTitle:string read FFormTitle write SetFormTitle;
  34.       function EnumChildWndProc(AhWnd:LongInt;AlParam:lParam):boolean;stdcall;//尤其是这里其实这个 用法本身就不对
  35.   end;
  36. implementation
  37. { TEditFill }
  38. constructor TEditFill.Create;
  39. begin
  40.    FHandle:=0;
  41.    FCount:=0;
  42.    FFormTitle:='';
  43.    FDestClassName:='';
  44.    FData:=TList.Create;
  45. end;
  46. destructor TEditFill.Destroy;
  47. begin
  48.   FHandle:=0;
  49.   FFormTitle:='';
  50.   FCount:=0;
  51.   if FData.List <> nil then
  52.   FData.Free;
  53. end;
  54. function TEditFill.EnumChildWndProc(AhWnd:LongInt;AlParam:lParam):boolean;stdcall;
  55. var
  56.    ClassName,Text:PChar;
  57. begin
  58.    if AhWnd <> 0 then
  59.       GetClassName(AhWnd,@ClassName,256)
  60.    else
  61.    begin
  62.       Raise Exception.Create('目标窗口句柄为空');
  63.       exit;
  64.    end;
  65.    if SameText(String(ClassName),DestClassName) then
  66.    begin
  67.       SendMessage(aHwnd,WM_GETTEXT,0,Integer(Text));
  68.       if Text = '' then
  69.       begin
  70.          if Count <= Data.Count then
  71.             SendMessage(aHwnd,WM_SETTEXT,0,Integer(Data.Items[Count]));
  72.           Count:=Count+1;
  73.       end;
  74.    end;
  75.    Result:=True;
  76. end;
  77. function TEditFill.FillEdit: boolean;
  78. var
  79.   aHandle:THandle;
  80. begin
  81.   Result:=True;
  82.   if FHandle <> 0 then
  83.   begin
  84.      aHandle:=FHandle;
  85.      EnumChildWindows(aHandle,Self.MethodAddress('EnumChildWndProc'),0)
  86.   end
  87.   else
  88.     Result:=False;
  89. end;
  90. function TEditFill.GetParentHandle: THandle;
  91. var
  92.    aHandle:THandle;
  93. begin
  94.    if FFormTitle = '' then
  95.    begin
  96.       raise Exception.Create('目标窗口标题不能为空');
  97.       exit;
  98.    end;
  99.    if FindWindow(nil,Pchar(FFormTitle))<> 0 then
  100.       aHandle:= FindWindow(nil,Pchar(FFormTitle));
  101.    Result:=aHandle;
  102. end;
  103. procedure TEditFill.SetCount(const Value: Integer);
  104. begin
  105.    FCount:=Value;
  106. end;
  107. procedure TEditFill.SetData(const Value: TList);
  108. begin
  109.   if Value.List <> nil then
  110.     FData.Assign(Value,laCopy,nil);
  111. end;
  112. procedure TEditFill.SetDestClassName(const Value: String);
  113. begin
  114.   FDestClassName := Value;
  115. end;
  116. procedure TEditFill.SetFormTitle(const Value: string);
  117. begin
  118.   FFormTitle:=Value;
  119. end;
  120. procedure TEditFill.SetHandle(const Value: THandle);
  121. begin
  122.   if Value <> 0 then
  123.      FHandle:=Value;
  124. end;
  125. end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值