• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Control handles user-defined message?

I made a component inherited from TControl and I need to make this component catches a user defined message (WM_USER+1) that is currently being caught by the form. Is this possible and how?
0
karouri
Asked:
karouri
  • 3
1 Solution
 
hubdogCommented:
unit MyControl;

interface

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

type
  TMyControl = class(TControl)
  private
    { Private declarations }
  protected
    { Protected declarations }
    procedure WMOwndefine(var Msg:TMessage);message wm_user+1;
  public
    { Public declarations }
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Sample', [TMyControl]);
end;

{ TMyControl }

procedure TMyControl.WMOwndefine(var Msg: TMessage);
begin
  //do what you want
end;

end.
0
 
karouriAuthor Commented:
That is exactly what I am doing, however it didn't 'do what I want'.
When I define the same definition as above for the form   (i.e. TForm1.WMOwndef(var msg:TMessage);message WM_USER+2;)  I am able to respond from within TForm1.WMOwndef, but the component isn't able to do it ..
0
 
hubdogCommented:
sorry for i forgot Tcontrol has not window handle so it can not receive message.
so a solution is your control shuld be inherited from Twincontrol or TCustomControl.
Or you can allocate a window for message loop.such as
unit MyControl;

interface

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

type
  TMyControl = class(TControl)
  private
    { Private declarations }
    FHandle:THandle;
  protected
    { Protected declarations }
    procedure WndProc(var Msg:TMessage);
  public
    { Public declarations }
    Constructor Create(Aowner:TComponent);override;
    Destructor Destroy;override;
    property Handle:THandle read FHandle;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TMyControl]);
end;

{ TMyControl }

constructor TMyControl.Create(Aowner: TComponent);
begin
  inherited;
  FHandle:=AllocateHwnd(WndProc);
end;

destructor TMyControl.Destroy;
begin
  DeallocateHWnd(FHandle);
  inherited;
end;

procedure TMyControl.WndProc(var Msg: TMessage);
begin
  try
    if Msg.Msg=wm_user+1 then
      showmessage('ok')
    else msg.Result:= DefWindowProc(FHandle, Msg.msg, msg.wParam, msg.lParam);
  except
    Application.HandleException(Self);
  end;
end;

end.

now you can use sendmessage(mycontrol1.handle,wm_user+1,0,0)
that will be ok

good luck

hubdog
0
 
hubdogCommented:
or you can use
postmessage(hwnd_broadcast,wm_user+1,0,0);
to sendmessage .
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now