Firing_Sky:
假设你的句柄为Handle, 试试这样行不: if TObject(Handle) is TForm then ...
假设你的句柄为Handle, 试试这样行不: if TObject(Handle) is TForm then ...
解决方案 »
- 请教高人:winsock UDP Server 接收数据后调用消息处理类,却丢失客户端地址
- 当一个字符串长度超过1024时 赋值给delphi7 Tmemo 自动折行了?
- 一个难题急盼您的解决,请您帮忙
- DBGridEh导出问题
- 求一简单稳定的界面皮肤控件!!
- delphi创建dll的问题!急
- 异常的问题!!
- 在前台调用存储过程出错什么原因?
- 请问GetSystemMetrics这个api函数是做什么用的??
- 用ole创建excel时,怎样判断当前的工作表是否打开?如果打开了怎么关闭呢?
- how to modify the timage control so as to fit a very large bitmap?
- Who has the components of making professional report?
函数及测试程序如下:其中函数叫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.
H:HWND;
Ctrl:TWinControl;
begin
……
得到一个窗体句柄H
……
Ctrl:=TWinControl(H);
if Ctrl is TForm then //运行到这儿就报错了
begin
……
对Ctrl进行操作
……
end;
end;这好像和你说的方法类似,但会报错929 :你说的那些东西我知道
我的源代码如上,你看看就明白我所说的“绑定一个句柄到TWinControl”是什么意思了
我想了一下,无论是你的还是我的方法都不可能行:窗口句柄转换成类指针后,该指针指向的内存位置不可预测,用is运算符号极有可能引起非法内存访问而导致VCL内核出错。
解决方法有一个:将Form的Self指针用SetWindowLong存放于GWL_USERDATA常量处,获得窗口句柄后再GetWindowLong,最后转换成TForm就可以了。
var aForm : TForm ; H,实际是 AForm.Handle,
可以这样,用windowsAPI的GetWindowLong得到 H,的CLASSNAME是 TForm
窗口的句柄和窗口本身的指针不是一个概念。