unit tabletxt;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ComCtrls,comobj,OleServer,WordXP,ActiveX;const
  wm_yb=wm_user+8;type
  tfiletype=(mytxt,myword,myExcel);
  ttabletxt = class(tcomponent)
  private
    procedure mymessage(var msg:tmessage);message wm_yb;
  protected
  public
    procedure mysave(myfilename:string);
  published
  end;               procedure Register;implementationprocedure Register;
begin
  RegisterComponents('yb', [ttabletxt]);
end;
procedure ttabletxt.mysave(myfilename: string);
begin
   postmessage(application.mainform.Handle,wm_yb,1,0);
end;
procedure ttabletxt.mymessage(var msg: tmessage);
begin
  showmessage('dffffffffffeeeeeeeeeeeefdsf');
  inherited;
end;end.
基本代码如上所示,但无法收到自定义消息,为何,如何解决,请高手赐教

解决方案 »

  1.   

    postmessage(application.mainform.Handle,wm_yb,1,0);
    错了,这是向应用程序的主窗口发送消息,ttabletxt 接受不到的。
    另外ttabletxt 从tcomponent派生是不行的,因为tcomponent没有窗口,不能直接接受消息。————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————
      

  2.   

    要不
    ttabletxt = class(twincontrol)   postmessage(ttabletxt.Handle,wm_yb,1,0);
      

  3.   

    twincontrol 有句柄 所以可以接受消息
      

  4.   

    to:citytramper(阿琪) 可是我做的是非可视控件啊
      

  5.   

    ttabletxt.perform();
    无法通过编译
      

  6.   

    TComponent没有消息队列,不能处理消息;TControl及其派生类才可以。
    有一个变通的办法:
    1.定义一个自定义的消息
    2.为你的TTableText写一个WindowProc,在其中处理你的消息
    3.找到一个TControl的派生类实例(如一个窗体,假定为Form1),保证Form1的生命周期比你的TTableText实例要长
    4.创建TTableText实例时,用它的WindowProc替换Form1的WindowProc
    5.别忘了在TTableText的WindowProc中调用Form1原来的WindowProc
    6.如果你有多个TTableText实例,那你要考虑创建和释放的先后次序,别搞乱了
    7.然后要处理你的消息时向Form1发就可以了
      

  7.   

    漏了一点
    4.5释放的时候恢复Form1原来的WindowProc
      

  8.   

    看看Timer是怎么做的。它在内部创建了一个窗口句柄,然后通过这个句柄来创建Windows的Timer以及接收Windows的WM_TIMER消息。具体请看Delphi的源代码。
      

  9.   

    还是有点不明白, movingboy2你有好一点的例子吗???我想参一下,不要太长,长了看了就头晕
      

  10.   

    100分全给我?好啊!
      
    ttabletxt = class(tcomponent)
      protected
        procedure WindowProc(var Message: TMessage);
      public
        Handle: HWnd;
        constructor Create(AOwner: TComponent);override;
        destructor  Destroy;override;
      end;implementationconstructor ttabletxt.Create(AOwner: TComponent);
    begin
      inherited;
      Handle := Classes.AllocateHWnd(WindowProc);
    end;procedure ttabletxt.WindowProc(var Message: TMessage);
    begin
      if Message.Msg = wm_yb then
        showmessage('dffffffffffeeeeeeeeeeeefdsf');
    end;destructor ttabletxt.Destroy;
    begin
      if Handle <> 0 then
        Classes.DeallocateHWnd(Handle);
      inherited;
    end;
    发送消息示例:
    SendMessage(tabletxt1.Handle, wm_yb, 0, 0);—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  11.   

    lxpbuaa(桂枝香在故国晚秋)的代码我没有试过,但思路与我的想法很接近了,比我的更简洁。
      

  12.   

    lxpbuaa(桂枝香在故国晚秋) 
    我狂晕,这是我在大富翁的帖子的答案,但看你这么乐于肋人的份人,就给你80分吧
      

  13.   

    yubing8(yubing8) (▲)
    你晕啥啊,我从来就不去大富翁,神经病。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  14.   

    wm_yb你看看这是什么,我是我名字的简写,这段代码是我自已写的,至少下次你发帖的时候要改一下这个吧
      

  15.   

    哦,就因为这个啊?你这个帖子中不是有:
    “const
      wm_yb=wm_user+8;”
    么?我没改是怕你看不懂!神经病。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————