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?
LVL 3
karouriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hubdogCommented:
or you can use
postmessage(hwnd_broadcast,wm_user+1,0,0);
to sendmessage .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.