Delphi编程实现Ping操作

2016-02-19 18:24 19 1 收藏

下面是个简单易学的Delphi编程实现Ping操作教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

在Delphi中使用TidIcmpClient控件可以非常简单的实现图形界面的Ping!
  新建一个工程,命名为PingGUI.dpr,窗口命名为“frmPing”,加入如下组件:
      lstReplies: TListBox;
      ICMP: TIdIcmpClient;
      Panel1: TPanel;
      btnPing: TButton;
      edtHost: TEdit;
      spnPing: TSpinEdit;
      Label1: TLabel;
  
  完整源代码如下:
  unit Main;

  interface

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  uses
    Windows, Messages, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
    SysUtils, Classes, IdIcmpClient, IdBaseComponent, IdComponent, IdRawBase, IdRawClient,
    Spin;

  
  type
    TfrmPing = class(TForm)
      lstReplies: TListBox;
      ICMP: TIdIcmpClient;
      Panel1: TPanel;
      btnPing: TButton;
      edtHost: TEdit;
      spnPing: TSpinEdit;
      Label1: TLabel;
      procedure btnPingClick(Sender: TObject);
      procedure ICMPReply(ASender: TComponent; const ReplyStatus: TReplyStatus);
  
  private
    public
    end;

  var
    frmPing: TfrmPing;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  implementation
  {$R *.DFM}

  procedure TfrmPing.btnPingClick(Sender: TObject);
  var
    i: integer;
  begin
    ICMP.OnReply := ICMPReply;
    ICMP.ReceiveTimeout := 1000;
    btnPing.Enabled := False; try
      ICMP.Host := edtHost.Text;
      for i := 1 to spnPing.Value do begin
        ICMP.Ping;
        Application.ProcessMessages;
      end;
    finally btnPing.Enabled := True; end;
  end;

  procedure TfrmPing.ICMPReply(ASender: TComponent; const ReplyStatus: TReplyStatus);
  var
    sTime: string;
  begin
    // TODO: check for error on ping reply (ReplyStatus.MsgType?)
    if (ReplyStatus.MsRoundTripTime = 0) then
      sTime := '1'
    else
      sTime := '=';

    lstReplies.Items.Add(Format('%d bytes from %s: icmp_seq=%d ttl=%d time%s%d ms',
      [ReplyStatus.BytesReceived,
      ReplyStatus.FromIpAddress,
      ReplyStatus.SequenceId,
      ReplyStatus.TimeToLive,
      sTime,
      ReplyStatus.MsRoundTripTime]));
  end;

  end.

来源:https://www.tulaoshi.com/n/20160219/1617563.html

延伸阅读
Printers是专门用来控制打印机的,可是在没有安装打印机时,却会提示I/O错误,所以必须有一个检测是否存在打印机的方法,我试过很多方法,可是I/O错误总是比我的判断早出现,所以采用以下的烂招来检测打印机。首先在uses增加Printers,再准备一个列表框ComboBox1,其属性Visible设为FALSE,然后在打印之前执行下列语句,那么就可以检测到是...
标签: Delphi
  用Delphi 6编程实现自动标注汉语拼音 在使用电脑编辑文档的时候,输入汉语拼音再加上它的声调,是一件令人头痛的事情,特别对于那些经常接触拼音的教师、家长和孩子。虽然 Office XP中已经加入了自动标注汉语拼音的功能,不过,Office XP要####.00元哦。对于没有用上Office XP的人来说,难道就没有办法享受到这种便利吗?好在...
标签: ASP
  This article presents a simple way to ping an address and get the results of the ping using ASP. The idea was supplied by Bart Silverstein. First, a .BAT file needs to be created that will be run from the Active Server Page. Let's call this file DoPing.BAT. It will contain only one statement, which will ping ...
如果你想做一个CD播放器的话,那本技巧一定少不了。这是一个控制CD-ROM驱动器门弹出和关闭的方法,首先需要在uses部分加入mmsystem,接着增加两个TButton按钮,在按钮事件里写入下列语句,记住在调用完毕后一定要显示或判断returnstring,要不程序将出错: procedure TForm1.Button1Click(Sender: TObject);var returnstring:pchar;b...
◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername/direction/targer.txt'),false); ◇[DELPHI]产生鼠标拖动效果 通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL: var xpanel,ypanel,xlabel,ylabel:integer; PAN...

经验教程

169

收藏

69
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部