为什么我写的类响应不到Windows 消息??? 请高手们帮忙,类代码如下:(50分)

  • 主题发起人 主题发起人 deepfar
  • 开始时间 开始时间
D

deepfar

Unregistered / Unconfirmed
GUEST, unregistred user!
请高手们帮忙,类代码如下:
unit UWindowMoving;

interface

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

type
TWindowMoving = class(TComponent)
private
FMainForm: TForm;
public
procedure WindowMoving(var Msg: TMessage); message WM_MOVING;

constructor Create(AMainForm: TForm);
destructor Destroy; override;
end;

implementation

constructor TWindowMoving.Create(AMainForm: TForm);
begin
inherited Create(AMainForm);
FMainForm := AMainForm;
end;
destructor TWindowMoving.destroy;
begin
inherited destroy;
end;

procedure TWindowMoving.WindowMoving(var Msg: TMessage);
begin
inherited;
showmessage('Moving Window!');
end;

end.
 
只有窗体才能接收WM_MOVING消息吧。
TWindowMoving = class(TComponent)
这样可能不行。
 
我想做一个类来处理主窗口 Moving 的消息,有什么好的方法吗?
 
这样的话要么继承TForm拦截WM_MOVING消息,要么在类里,替换Form的WindowProc处理它。
如果替换的话就是先定义一个TWndMethod类型过程
在替指定Form的WindowProc方法.
再在你定义的TWndMethod过程里处里相应的消息即可
 
兄弟你调试一下,我也是参考别人的写的,没有调试过。
unit UWindowMoving;

interface

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

type
TWindowMoving = class(TComponent)
private
FMainForm: TForm;
NewWndProc, OldWndProc: Pointer;
public
constructor Create(AMainForm: TForm);
destructor Destroy; override;
procedure NewWndMethod;
end;

implementation

constructor TWindowMoving.Create(AMainForm: TForm);
begin
inherited Create(AMainForm);
FMainForm := AMainForm;
NewWndProc := MakeObjectInstance(NewWndMethod);
OldWndProc := Pointer(SetWindowLong(FMainForm.Handle, gwl_WndProc, Longint(NewWndProc)));
end;

destructor TWindowMoving.destroy;
begin
inherited destroy;
SetWindowLong(FMainForm.Handle, gwl_WndProc, Longint(OldWndProc));
FreeObjectInstance(NewWndProc);
end;

procedure TWindowMoving.NewWndMethod;
begin
case Msg.Msg of
WM_MOVING: showmessage('Moving Window!');
WM_XXXX2: //
end;
Msg.Result := CallWindowProc(OldWndProc, Handle, Msg.Msg, Msg.WParam, Msg.LParam);
end;

end.
 
创建的时候AllocHwnd一个句柄出来,
 
谢谢,我试一下!!
 
to :babibean
NewWndProc := MakeObjectInstance(NewWndMethod);
这里有错,
[Warning] UWindowMoving.pas(28): Symbol 'MakeObjectInstance' is deprecated
[Error] UWindowMoving.pas(28): Incompatible types: 'Parameter lists differ'
[Warning] UWindowMoving.pas(34): Symbol 'FreeObjectInstance' is deprecated
帮我看看好吗?
 
to:jfyes
请给个例子好吗?
 
.............
TWindowMoving = class(TWinControl)
.............
 
谢谢了,搞定!!
原来 NewWndMethod 是有参数的 (var nMsg: Tmessage)
只要加入 参数表就对了.
 
多人接受答案了。
 
后退
顶部