随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

首先建议初学者不要在这些小的技巧上下太多功夫, 学好基础才是根本;
我的博客上这种东西不多, 这是大家讨论时, 话赶话赶出来的, 知道有这种可行性即可, 没有多少实用价值.

本例效果图:



此例回复来宾在
http://www.cnblogs.com/del/archive/2008/02/26/1081644.html 碰到的问题.

全部代码如下(测试时注意给主窗体焦点):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{这里主要是设置控件位置}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel1.Align := alTop;
  Panel1.Height := 30;
  Edit1.Top := 4;
  Edit1.Left := 12;
  Edit1.Width := 72;
  Edit1.Clear;
  Button1.Top := Edit1.Top - 1;
  Button1.Left := Edit1.Left + Edit1.Width + 12;
  Button1.Caption := '确认捕获, 用回车键';
  Button1.Width := Canvas.TextWidth(Button1.Caption) + 32;
  Button1.Default := True;
  Button2.Top := Button1.Top;
  Button2.Left := Panel1.ClientWidth - Button2.Width - 12;
  Button2.Anchors := [akRight];
  Button2.Caption := '过来吧你';
  Timer1.Interval := 100;
end;

{捕获窗口句柄, 显示在标题}
procedure TForm1.Timer1Timer(Sender: TObject);
var
  pt: TPoint;
begin
  if Boolean(GetCursorPos(pt)) then
    Text := IntToStr(WindowFromPoint(pt));
end;

{把确认的目标窗口的句柄显示在 Edit1}
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := Self.Text;
end;

{捕获了干什么呢? 譬如让它当自己的"干儿子"吧}
procedure TForm1.Button2Click(Sender: TObject);
var
  h: HWND;        {这是被捕捉窗口的句柄}
  r: TRect;       {这是被捕捉窗口的矩形}
  pw,ph: Integer; {这是被捕捉窗口的宽度与高度}
begin
  h := StrToInt(Edit1.Text);
  Windows.SetParent(h, Self.Handle);
  GetWindowRect(h,r);
  pw := r.Right - r.Left;
  ph := r.Bottom - r.Top;
  MoveWindow(h, 0, Panel1.Height, pw, ph, True);
end;

end.

发现还没处理计算器的关闭呢, 用处不大, 不再重做了.
posted on 2008-03-09 21:27  万一  阅读(...)  评论(...编辑  收藏