Delphi中record结构体定义发送接收传递使用
定义消息结构体TMsg:
type PMsg = ^TMsg; TMsg = record FontName :string[20]; FontColor :Integer; FontSize :Integer; FontStyle :string[4]; Content :array[0..4096] of Char; //不能超过UDP包长 end;
通过UDP协议发送结构体及将结构体成员赋值给RichEdit控件属性:
procedure TForm1.btnSendClick(Sender: TObject); var Smsg:TMsg; //发送消息结构 OldCount:integer; begin //发送 Smsg.FontName:=RichEdit2.Font.Name; Smsg.FontColor:=RichEdit2.Font.Color; Smsg.FontSize:=RichEdit2.Font.Size; Smsg.FontStyle:=DeStyle(RichEdit2.Font.Style); StrPCopy(Smsg.Content,ConvertMsgToCmd(RichEdit2)); IdUDPServer1.SendBuffer(edtRemoteIP.Text,4001,Smsg,SizeOf(Smsg)); //本机显示 RichEdit1.SelStart:=MaxInt; RichEdit1.Paragraph.FirstIndent:=0; RichEdit1.SelAttributes.Color := clGreen; RichEdit1.Lines.Add(DateTimeToStr(Now)+' : 你对 xxx 说 :'); RichEdit1.Paragraph.FirstIndent:=18; OldCount:=Length(RichEdit1.Text); RichEdit1.SelStart:=MaxInt; RichEdit1.SelAttributes.Color :=Smsg.FontColor; RichEdit1.SelAttributes.Name:=Smsg.FontName; RichEdit1.SelAttributes.Size:=Smsg.FontSize; RichEdit1.SelAttributes.Style:=EnStyle(Smsg.FontStyle); ConvertMsgToFace(RichEdit1,Smsg.Content,OldCount); end;
通过UDP协议接收结构体及将结构体成员赋值给RichEdit控件属性
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); var Rmsg:TMsg; //接收消息结构 OldCount:integer; begin //接收到的数据流直接可以传递给TMsg结构体,然后可以直接获取成员值 AData.ReadBuffer(Rmsg,AData.Size); RichEdit1.SelStart:=MaxInt; RichEdit1.Paragraph.FirstIndent:=0; RichEdit1.SelAttributes.Color := clBlue; RichEdit1.Lines.Add(DateTimeToStr(Now)+' : 来自 '+ABinding.PeerIP+' 的消息 :'); RichEdit1.Paragraph.FirstIndent:=18; OldCount:=Length(RichEdit1.Text); //showmessage(rmsg.FontName); //showmessage(rmsg.FontStyle); //showmessage(rmsg.Content); RichEdit1.SelStart:=MaxInt; RichEdit1.SelAttributes.Color :=Rmsg.FontColor; RichEdit1.SelAttributes.Name:=Rmsg.FontName; RichEdit1.SelAttributes.Size:=Rmsg.FontSize; RichEdit1.SelAttributes.Style:=EnStyle(Rmsg.FontStyle); ConvertMsgToFace(RichEdit1,Rmsg.Content,OldCount); end;
本文来自博客园,作者:IT情深,转载请注明原文链接:https://www.cnblogs.com/wh445306/p/16751834.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?