Delphi消息的广播方式(先RegisterWindowMessage,后SendMessage HWND_BROADCAST,最后改写接收窗口的WndProc)

///消息广播只能将消息传递到接收消息的主程序中,MDIChild窗体不能接收到广播消息;/

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
    Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
public
    procedure WndProc(var message:TMessage);override;
end;

var
    Form1: TForm1;
    MyMessage: DWORD;

implementation
{$R *.dfm}
{ TForm1 }

procedure TForm1.WndProc(var message: TMessage); // 可处理自定义消息
begin
    if message.Msg=MyMessage then // 第3步:重载Windows消息处理过程,处理接收到的MyMessage消息;
    ShowMessage(FloatToStr(Handle)+ 'MyMessage'); // 每次程序关闭后再启动,句柄值不一样。但不关闭就是同一个值
    inherited WndProc(Message); // 其实没什么用,但是屏蔽后出错
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    SendMessage(HWND_BROADCAST,MyMessage,0,0); //第2步:发送MyMessage消息,相当于广播;而且两个程序都执行了的话,一个处理完另一个才能开始处理。可以试试PostMessage的区别
end;

/预先注册好Windows消息;第一种方法
procedure TForm1.FormCreate(Sender: TObject);
begin
    //同一字符串,如'MyMessageMe' 在不同的程序中调用RegisterWindowMessage注册消息时,返回的MyMessage结果是一样的。这样为在不同程序之间广播消息提供了可能。即不同程序注册消息时,必须注册相同的注符串。
    MyMessage:=RegisterWindowMessage('MyMessageMe'); //第1步:注册Windows消息,重点在于返回的值MyMessage
end;

/预先注册好Windows消息;第二种方法
{ initialization
MyMessage:=RegisterWindowMessage('MyMessageMe');
}


//BroadcastSystemMessage,可以在进程之间广播消息;
procedure TForm1.SendSMSMessage;
var
    SMSMessage:Cardinal;
    recipt:Cardinal;
begin
    SMSMessage:=RegisterWindowMessage('SendSMSMessage');
    recipt:=BSM_ALLDESKTOPS; //所以桌面程序都可以接收
    BroadcastSystemMessage(BSF_POSTMESSAGE,@recipt,SMSMessage,0,0);
end;

end.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr
在 VB.NET 中,可以使用 Windows API 函数来实现接收外部发来的字符串消息。 首需要在代码中声明以下函数: ```vbnet Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean ``` 其中 `RegisterWindowMessage` 用于注册一个自定义的消息,`PostMessage` 用于发送消息到指定的窗口。 接下来,在需要接收消息窗口中,可以使用以下代码来接收消息: ```vbnet Protected Overrides Sub WndProc(ByRef m As Message) Const WM_COPYDATA As Integer = &H4A If m.Msg = WM_COPYDATA Then Dim data As CopyDataStruct = CType(m.GetLParam(GetType(CopyDataStruct)), CopyDataStruct) Dim msg As String = New String(data.lpData) ' 处理接收到的消息 MessageBox.Show(msg) End If MyBase.WndProc(m) End Sub ``` 其中 `WM_COPYDATA` 是用来接收复制数据的消息,`CopyDataStruct` 是一个结构体,用来存储接收到的数据。 需要注意的是,接收消息窗口需要调用 `RegisterWindowMessage` 函数来注册一个唯一的自定义消息,发送消息的程序需要使用该消息来向接收消息窗口发送数据。 以下是一个简单的示例代码: 接收消息窗口: ```vbnet Imports System.Runtime.InteropServices Public Class Form1 Public Structure CopyDataStruct Public dwData As IntPtr Public cbData As Integer Public lpData As IntPtr End Structure Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean Private Const WM_COPYDATA As Integer = &H4A Private msgId As Integer Protected Overrides Sub WndProc(ByRef m As Message) If m.Msg = msgId Then Dim data As CopyDataStruct = CType(m.GetLParam(GetType(CopyDataStruct)), CopyDataStruct) Dim msg As String = New String(data.lpData) MessageBox.Show(msg) End If MyBase.WndProc(m) End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load msgId = RegisterWindowMessage("MyMsg") End Sub End Class ``` 发送消息的程序: ```vbnet Imports System.Runtime.InteropServices Public Class Form1 Public Structure CopyDataStruct Public dwData As IntPtr Public cbData As Integer Public lpData As IntPtr End Structure Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Boolean Private Const WM_COPYDATA As Integer = &H4A Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim hWnd As IntPtr = FindWindow(Nothing, "Form1") If hWnd <> IntPtr.Zero Then Dim msgId As Integer = RegisterWindowMessage("MyMsg") Dim data As New CopyDataStruct With { .dwData = IntPtr.Zero, .cbData = TextBox1.Text.Length, .lpData = Marshal.StringToHGlobalAnsi(TextBox1.Text) } PostMessage(hWnd, WM_COPYDATA, msgId, data) End If End Sub End Class ``` 在上面的示例代码中,接收消息窗口使用了 `MyMsg` 作为自定义消息的名称,发送消息的程序也使用了相同的名称。当发送消息的程序点击按钮时,会向接收消息窗口发送一个包含文本框中的文本的消息接收消息窗口会弹出一个消息框显示接收到的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值