Firing_Sky:
假设你的句柄为Handle, 试试这样行不: if TObject(Handle) is TForm then ...

解决方案 »

  1.   

    我写了个函数,它是用CLASSNAME进行比较来判断传进来的窗口句柄是否指向一个DELPHI的的窗口类型(TFORM)。另外,我想问一下,将一个句柄绑定到窗口是什么意思?TFORM中HANDLE是只读的。
    函数及测试程序如下:其中函数叫ISMYWINDOWunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
        function ismywindow(hw:HWND;FormClass: TFormClass):boolean;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
     if ismywindow(form1.Handle,tform1 ) then  showmessage(form1.ClassName);
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      hclass:string;
    begin
      SetLength(hclass,50);
      if iswindow(form1.Handle) then
      begin
         GetClassName(form1.Handle,pchar(hclass),50);
         showmessage(hclass);
      end ;
    end;function TForm1.ismywindow(hw: HWND;FormClass: TFormClass): boolean;
    var
      hclass:string;
    begin
      result:=false;
      setlength(hclass,50);
      if iswindow(hw) then
        begin
          GetClassName(hw,pchar(hclass),50);
          hclass:=Trim(string(pchar(hclass)));
          if hclass=formclass.ClassName then result:=true;
        end;
    end;end.
      

  2.   

    Lin:我原来的用法是这样的Var
      H:HWND;
      Ctrl:TWinControl;
    begin
      ……
      得到一个窗体句柄H
      ……
      Ctrl:=TWinControl(H);
      if Ctrl is TForm then  //运行到这儿就报错了
      begin
        ……
        对Ctrl进行操作
        ……
      end;
    end;这好像和你说的方法类似,但会报错929 :你说的那些东西我知道
    我的源代码如上,你看看就明白我所说的“绑定一个句柄到TWinControl”是什么意思了
      

  3.   

    Firing_Sky:
      我想了一下,无论是你的还是我的方法都不可能行:窗口句柄转换成类指针后,该指针指向的内存位置不可预测,用is运算符号极有可能引起非法内存访问而导致VCL内核出错。
      解决方法有一个:将Form的Self指针用SetWindowLong存放于GWL_USERDATA常量处,获得窗口句柄后再GetWindowLong,最后转换成TForm就可以了。
      

  4.   

    反过来看,一个Form的窗口句柄是 From.Handle,所以你的代码会报错,
    var aForm : TForm ; H,实际是 AForm.Handle,
    可以这样,用windowsAPI的GetWindowLong得到 H,的CLASSNAME是 TForm
      

  5.   

    to : lin你贴的好快:),我写的时候还没有见你贴上来:)
      

  6.   

    就是因为窗口句柄转换成类指针后,该指针指向的内存位置不可预测, 所以我用了TWinControl强制转换啊!
      

  7.   

    if FindControl(Handle) = Form1 then
      

  8.   

    olo是对的,顺便谢谢:也解决了我的问题。
      

  9.   

    olo is right
    窗口的句柄和窗口本身的指针不是一个概念。