【问题标题】:Avoid message broadcast避免消息广播
【发布时间】:2012-05-04 07:54:53
【问题描述】:

我不知道如何解释,但这里是: 我有一个带有一些组合框的网格表格。从这个表单我创建另一个来编辑数据。编辑表单也有一些组合框,如网格。 这些组合框中的值可以从第三种形式进行编辑。 如果它们被编辑,我会向所有打开的表单发送类似广播的消息以更新组合框。

procedure HsBrodcastUpdate;
var
  i: integer;
begin
  for i := 1 to Screen.FormCount - 1 do
    SendMessage(Screen.Forms[i].Handle, WM_FORMUPDATES, 0, 0);
end;

在应该执行更新的每个表单上,我都有:

procedure FormUpdate(var aMessage: TMessage); message WM_FORMUPDATES;

这就像在步枪就足够的情况下使用猎枪一样。 将消息发送到创建编辑表单的表单就足够了

我不确定它是否会提高性能,但我想尝试一下。

我的问题:我怎样才能不使用发送到所有表单的 HsBrodcastUpdate 而是将消息发送到创建发送消息的表单的表单。

【问题讨论】:

  • 您无法想象在正常工作的 Windows 系统上每秒发送多少条消息 :)。消除您的消息不会影响性能。
  • 在编辑表单中使用OnEditionDone之类的事件并在owner表单中创建事件处理程序。或仅向所有者表单发送/发布消息。
  • ..或者调用所有者控件perform()方法
  • 如果您想知道为什么没有人回答,那是因为您还没有提出问题。

标签: forms delphi message


【解决方案1】:

我会使用类方法来完成这个

  Form1 = class(TForm1)
  ...
  private
  ...
  public
     Class procedure UpdateComboBox;
     ...
  end;

那么程序如下所示

  class procudure TForm1.UpdateComboBox;
  var
     F: TForm2;//This is the target form
     I: Integer;
  begin
     F := nil;
     for i := Screen.FormCount - 1 DownTo 0 do
      if (Screen.Forms[i].Name = '<The Form Name Here>') then
         F := Screen.Forms[I] As TForm2;
     if F <> nil then
     begin
        //update your form's F.comboBox here
     end;
  end;

【讨论】:

    【解决方案2】:

    我在您的帖子中没有看到任何问题,所以这甚至可能不是您想要的。听起来您可能正在寻找实现观察者模式的方法。

    您可以查看以下两篇文章作为示例,但还有很多其他文章。 http://tdelphihobbyist.blogspot.com/2009/10/observer-design-pattern-in-delphi-push.html http://sourcemaking.com/design_patterns/observer/delphi

    【讨论】:

      【解决方案3】:

      您可以通过在表单上公开事件处理程序来构建事件链。 研究这个例子:

      {- Main Form }
      
      Type
      
      TFormMain = Class(TForm)
        private
          procedure UpdateCombo( Sender : TObject);
          procedure InvokeOtherForm;
      end;
      
      TFormMain.InvokeOtherForm;
      var 
        OtherForm : TOtherForm;
      begin
        OtherForm := TOtherForm.Create( Nil);
        try
          OtherForm.OnUpdateCombo := Self.UpdateCombo;  // Link update event !
          OtherForm.ShowModal;
        finally
          OtherForm.Free;
        end;
      end;
      
      TFormMain.UpdateCombo( Sender : TObject);
      begin
        {- Update the combos ... }
        ...
      end;
      

      {- OtherForm }
      Type
      
      TOtherForm = Class(TForm)
        private
          FOnUpdateCombo : TNotifyEvent;
          procedure InvokeThirdForm;
          procedure UpdateCombo( Sender : TObject);
        public
          OnUpdateCombo : TNotifyEvent read FOnUpdateCombo write FOnUpdateCombo;
        end;
      
      TOtherForm.InvokeThirdForm;
      var 
        ThirdForm : TThirdForm;
      begin
        ThirdForm := TThirdForm.Create( Nil);
        try
          ThirdForm.OnUpdateCombo := Self.UpdateCombo;  // Link update event !
          ThirdForm.ShowModal;
        finally
          ThirdForm.Free;
        end;
      end;
      
      TOtherForm.UpdateCombo( Sender : TObject);
      begin
        {- Do some internal updates }
        ...
        {- Pass on update information event }
        if Assigned(FOnUpdateCombo) then
          FOnUpdateCombo( Sender);
      end;
      

      {- ThirdForm }
      Type
      
      TThirdForm = Class(TForm)
        private
          FOnUpdateCombo : TNotifyEvent;
          procedure UpdateCombo;
        public
          OnUpdateCombo : TNotifyEvent read FOnUpdateCombo write FOnUpdateCombo;
        end;
      
      TThirdForm.UpdateCombo;
      begin
        {- Do some internal updates }
        ...
        {- Pass on update information event }
        if Assigned(FOnUpdateCombo) then
          FOnUpdateCombo( Self);
      end;
      

      【讨论】:

        猜你喜欢
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        • 2019-09-07
        • 1970-01-01
        • 2013-09-09
        • 2019-05-11
        • 2015-08-02
        相关资源
        最近更新 更多