ClientMain.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
- unit ClientMain;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, IPC, Spin, ComCtrls;
- type
- TfrmClient = class(TForm)
- edtSessionName: TEdit;
- btnConnect: TButton;
- GroupBox1: TGroupBox;
- edtReceiveString: TEdit;
- Label3: TLabel;
- Label4: TLabel;
- Label6: TLabel;
- sedtReceiveInteger: TSpinEdit;
- StatusBar: TStatusBar;
- Label5: TLabel;
- GroupBox2: TGroupBox;
- edtSendString: TEdit;
- sedtSendInteger: TSpinEdit;
- Label1: TLabel;
- Label2: TLabel;
- btnSend: TButton;
- procedure btnSendClick(Sender: TObject);
- procedure btnConnectClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- IPCClient: TIPCClient; //定义IPCClient
- procedure IPCClientConnect(AHwnd: HWND);
- procedure IPCClientDisconnect(AHwnd: HWND);
- procedure IPCClientData(MsgPointer: Pointer);
- { Private declarations }
- public
- { Public declarations }
- end;
- type
- {定义一个发送和接收数据记录类型}
- PIPCData = ^TIPCData;
- TIPCData = record
- SendString: string[30];
- SendInteger: integer;
- end;
- var
- frmClient: TfrmClient;
- implementation
- {$R *.DFM}
- //主窗体创建事件
- procedure TfrmClient.FormCreate(Sender: TObject);
- begin
- IPCClient := TIPCClient.Create(self);
- IPCClient.OnConnect := IPCClientConnect;
- IPCClient.OnDisconnect := IPCClientDisConnect;
- IPCClient.OnData := IPCClientData;
- end;
- //连接或断开服务器
- procedure TfrmClient.btnConnectClick(Sender: TObject);
- begin
- if btnconnect.Caption = '连接' then
- begin
- if not IPCClient.Active then
- IPCClient.SessionName := edtSessionName.Text;
- IPCClient.Open;
- end
- else
- IPCClient.Close;
- end;
- //发送数据
- procedure TfrmClient.btnSendClick(Sender: TObject);
- var
- MsgData: TIPCData;
- begin
- if IPCClient.Active then
- begin
- MsgData.SendString := edtSendString.Text;
- MsgData.SendInteger := sedtSendInteger.Value;
- IPCClient.SendMsg(@MsgData, Sizeof(MsgData));
- end;
- end;
- //连接服务器事件
- procedure TfrmClient.IPCClientConnect(AHwnd: HWND);
- begin
- Statusbar.Panels[0].Text := '连接状态 :已连接';
- Statusbar.Refresh;
- btnConnect.Caption := '断开连接'
- end;
- //断开服务器连接事件
- procedure TfrmClient.IPCClientDisconnect(AHwnd: HWND);
- begin
- Statusbar.Panels[0].Text := '连接状态 :未连接';
- Statusbar.Refresh;
- btnConnect.Caption := '连接'
- end;
- //接收数据事件
- procedure TfrmClient.IPCClientData(MsgPointer: Pointer);
- var
- AMsg: TIPCData;
- begin
- AMsg := TIPCData(MsgPointer^);
- edtReceiveString.Text := AMsg.SendString;
- sedtReceiveInteger.Value := AMsg.SendInteger;
- end;
- end.