用sendmessage发送自定义消息给特定窗口
我们要怎样用sendmessage(或postmessage)来发送自定义消息给特定的窗口,来实现类似socket的功能?查阅了好多资料,终于摸索出来用sendmessage来模拟winsocket的聊天功能。。。
在此感谢@sych!
代码分享如下:
程序代码:
server_demo=NEWOBJECT([form1]) server_demo.show *!* RETURN ************************************************** *-- Form: form1 (d:\documents\visual foxpro 项目\server_demo.scx) *-- 父类: form *-- 基类: form *-- 时间戳: 08/13/24 08:34:02 PM * DEFINE CLASS form1 AS form DoCreate = .T. Caption = "Server_demo" target = 0 Name = "form1" ADD OBJECT text1 AS textbox WITH ; Height = 20, ; Left = 0, ; Top = 2, ; Width = 100, ; Name = "Text1" ADD OBJECT command1 AS commandbutton WITH ; Top = 0, ; Left = 108, ; Height = 25, ; Width = 60, ; Caption = "\<Send", ; Name = "Command1" ADD OBJECT edit1 AS editbox WITH ; Height = 224, ; Left = 0, ; Top = 24, ; Width = 372, ; Name = "Edit1" PROCEDURE callback PARAMETERS nhwnd,nmsg,nparam,lparam IF nmsg=0x401 &&Windows为我们预留的自定义消息代码 MESSAGEBOX(this.Caption +[ data arrival!]) ************************************************************* **********不知道具体收到多少个字节,只好一个个检测*********** *********所幸windows规定发送的字符串以字符chr(0)结尾********* *********不知道有没有其他办法来获得指定地址的文字数?********* i=0 DO WHILE .t. c=SYS(2600,lparam+i,1) IF c=CHR(0) n=i exit ELSE i=i+1 loop ENDIF enddo this.eDIT1.Value=this.edit1.value+TRANSFORM(TIME())+[ Recieved: ]+SYS(2600,lparam,n)+CHR(13) ENDIF ENDPROC PROCEDURE queryunload CLEAR EVENTS ENDPROC PROCEDURE Activate *!* IF VARTYPE(m.client_demo)="O" AND !ISNULL(m.client_demo) *!* IF VARTYPE(m.client_demo)="X" IF VARTYPE(client_demo)="U" client_demo=NEWOBJECT([form2]) client_demo.show this.target=m.client_demo.hwnd BINDEVENT(this.target,0x401,m.client_demo,"callback") ENDIF READ EVENTS ENDPROC PROCEDURE Init DECLARE inte SendMessage IN WIN32API integer,integer,integer,string ENDPROC PROCEDURE command1.Click IF !EMPTY(thisform.text1.value) n=sendmessage(thisform.target,0x401,0,thisform.text1.Value) MESSAGEBOX(TRANSFORM(n)) thisform.edit1.Value=thisform.edit1.Value+TRANSFORM(TIME())+[ Send: ]+thisform.text1.Value+CHR(13) thisform.text1.Value='' ENDIF ENDPROC ENDDEFINE * *-- EndDefine: form1 ************************************************** ************************************************** *-- Form: form2 (d:\documents\visual foxpro 项目\client_demo.scx) *-- 父类: form *-- 基类: form *-- 时间戳: 08/13/24 08:39:07 PM * DEFINE CLASS form2 AS form DoCreate = .T. Caption = "Client_demo" target = 0 Name = "form2" autocenter=.t. ADD OBJECT text1 AS textbox WITH ; Height = 20, ; Left = 0, ; Top = 2, ; Width = 100, ; Name = "Text1" ADD OBJECT command1 AS commandbutton WITH ; Top = 0, ; Left = 108, ; Height = 25, ; Width = 60, ; Caption = "\<Send", ; Name = "Command1" ADD OBJECT edit1 AS editbox WITH ; Height = 224, ; Left = 0, ; Top = 24, ; Width = 372, ; Name = "Edit1" PROCEDURE callback PARAMETERS nhwnd,nmsg,nparam,lparam IF nmsg=0x401 MESSAGEBOX(this.caption+[ data arrival!]) ************************************************************* **********不知道具体收到多少个字节,只好一个个检测*********** *********所幸windows规定发送的字符串以字符chr(0)结尾********* i=0 DO WHILE .t. c=SYS(2600,lparam+i,1) IF c=CHR(0) n=i exit ELSE i=i+1 loop ENDIF enddo this.eDIT1.Value=this.edit1.value+TRANSFORM(TIME())+[ Received: ]+SYS(2600,lparam,n)+CHR(13) ENDIF ENDPROC PROCEDURE Unload CLEAR EVENTS IF vart(m.server_demo)="O" m.server_demo.release ENDIF ENDPROC PROCEDURE Init DECLARE inte SendMessage IN WIN32API integer,integer,integer,string IF VARTYPE(m.server_demo)!="O" OR ISNULL(m.server_demo) MESSAGEBOX([Pls run server_demo at first!]) RETURN .f. ELSE this.target=m.server_demo.HWnd ENDIF BINDEVENT(this.target,0x401,m.server_demo,"callback") ENDPROC PROCEDURE command1.Click IF !EMPTY(thisform.text1.value) sendmessage(thisform.target,0x401,0,thisform.text1.Value) thisform.edit1.Value=thisform.edit1.Value+TRANSFORM(TIME())+[ Send: ]+thisform.text1.Value+CHR(13) thisform.text1.Value='' ENDIF ENDPROC ENDDEFINE * *-- EndDefine: form2 **************************************************
[此贴子已经被作者于2024-8-18 19:53编辑过]