Delphi - 如何向 TDataModule 发送 Windows 消息?

发布于 2024-09-15 15:48:08 字数 255 浏览 2 评论 0原文

我需要向 Delphi 2010 应用程序中的 TDataModule 发送 Windows 消息。

我想使用

PostMessage(???.Handle, UM_LOG_ON_OFF, 0,0);

问题

TDataModule 没有Handle。我怎样才能向它发送Windows消息?

I need to send a windows message to a TDataModule in my Delphi 2010 app.

I would like to use

PostMessage(???.Handle, UM_LOG_ON_OFF, 0,0);

Question:

The TDataModule does not have a Handle. How can I send a windows message to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-09-22 15:48:08

你可以很容易地给它一个把手。看一下类单元中的AllocateHWND。调用它来为您的数据模块创建一个句柄,并定义一个将处理 UM_LOG_ON_OFF 的简单消息处理程序。

You can give it a handle easily enough. Take a look at AllocateHWND in the Classes unit. Call this to create a handle for your data module, and define a simple message handler that will process UM_LOG_ON_OFF.

天煞孤星 2024-09-22 15:48:08

这是一个示例,演示如何使用 Handle 创建 TDataModule 的后代

uses
  Windows, Winapi.Messages,
  System.SysUtils, System.Classes;

const
  UM_TEST = WM_USER + 1;

type
  TMyDataModule = class(TDataModule)
  private
    FHandle: HWND;
  protected
    procedure   WndProc(var Message: TMessage); virtual;
  public
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy(); override;
    property    Handle : HWND read FHandle;
  end;

...

uses
  Vcl.Dialogs;

constructor TMyDataModule.Create(AOwner : TComponent);
begin
  inherited;

  FHandle := AllocateHWND(WndProc);
end;

destructor  TMyDataModule.Destroy();
begin
  DeallocateHWND(FHandle);

  inherited;
end;

procedure   TMyDataModule.WndProc(var Message: TMessage);
begin
  if(Message.Msg = UM_TEST) then
  begin
    ShowMessage('Test');
  end;
end;

,然后我们可以向数据模块发送消息,如下所示:

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostMessage(MyDataModule.Handle, uMyDataModule.UM_TEST, 0, 0);
end;

Here is an example demonstrating how to create a TDataModule's descendant with an Handle

uses
  Windows, Winapi.Messages,
  System.SysUtils, System.Classes;

const
  UM_TEST = WM_USER + 1;

type
  TMyDataModule = class(TDataModule)
  private
    FHandle: HWND;
  protected
    procedure   WndProc(var Message: TMessage); virtual;
  public
    constructor Create(AOwner : TComponent); override;
    destructor  Destroy(); override;
    property    Handle : HWND read FHandle;
  end;

...

uses
  Vcl.Dialogs;

constructor TMyDataModule.Create(AOwner : TComponent);
begin
  inherited;

  FHandle := AllocateHWND(WndProc);
end;

destructor  TMyDataModule.Destroy();
begin
  DeallocateHWND(FHandle);

  inherited;
end;

procedure   TMyDataModule.WndProc(var Message: TMessage);
begin
  if(Message.Msg = UM_TEST) then
  begin
    ShowMessage('Test');
  end;
end;

Then we can send messages to the datamodule, like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostMessage(MyDataModule.Handle, uMyDataModule.UM_TEST, 0, 0);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文