熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

用Delphi實現局域網內消息傳遞

2013-11-11 21:04:11  來源: Delphi編程 
本程序介紹如何在局域網內安裝了信使服務的Windows 計算機之間傳遞消息
  
  向窗體上添加兩個TLabel組件兩個TEdit組件和一個TButton組件設計完成的主界面如圖所示
  
 

  
主界面

  
  首先聲明NetMessageBufferSend函數該函數在netapidll庫中
  
  type
  NET_API_STATUS = LongInt;
  function NetMessageBufferSend(servername: LPCWSTR; msgname: LPCWSTR;
  fromname: LPCWSTR; buf: Pointer;
  buflen: DWORD): NET_API_STATUS;
  stdcall;external netapidll;
  
  在程序運行過程中單擊Send按鈕就會向Computer文本框指定的計算機發送Content文本框中輸入的消息響應代碼如下
  
  procedure TFormButtonClick(Sender: TObject);
  var
  WideMsg:PWideChar;
  DestName:PWideChar;
  begin
  DestName:=PWideChar(WideString(EditText));
  WideMsg:=PWideChar(WideString(EditText));
  NetMessageBufferSend(nilDestNamenilWideMsgLength(EditText)*);
  end;
  
  程序代碼如下
  
  unit Unit;
  interface
  uses
  Windows Messages SysUtils Variants Classes Graphics Controls Forms
  Dialogs StdCtrls;
  
  type
  NET_API_STATUS = LongInt;
  function NetMessageBufferSend(servername: LPCWSTR; msgname: LPCWSTR;
  fromname: LPCWSTR; buf: Pointer;
  buflen: DWORD): NET_API_STATUS;
  stdcall;external netapidll;
  type
  TForm = class(TForm)
  Edit: TEdit;
  Label: TLabel;
  Label: TLabel;
  Edit: TEdit;
  Button: TButton;
  procedure ButtonClick(Sender: TObject);
  private
  { Private declarations }
  public
  { Public declarations }
  end;
  
  var
  Form: TForm;
  implementation
  {$R *dfm}
  procedure TFormButtonClick(Sender: TObject);
  var
  WideMsg:PWideChar;
  DestName:PWideChar;
  begin
  DestName:=PWideChar(WideString(EditText));
  WideMsg:=PWideChar(WideString(EditText));
  NetMessageBufferSend(nilDestNamenilWideMsgLength(EditText)*);
  end;
  end
  
  保存文件然後按F鍵運行程序程序運行的初始畫面如圖所示
  
 

  
程序運行的初始畫面

  
  在Computer對應的文本框中輸入目的計算機名在Content對應的文本框中輸入消息內容如圖所示
  
  單擊Send按鈕就會向指定的計算機發送消息在接收消息的計算機上就會顯示一個對話框如圖所示
  
 

  
指定計算機名和消息內容

  

  
程序運行結果

From:http://tw.wingwit.com/Article/program/Delphi/201311/8402.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.