• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

随时随刻知道自己的IP_Delphi

delphi 搞代码 7年前 (2018-08-09) 200次浏览 已收录 0个评论

作者:李淼  

  随着网络的普及,越来越多的人开始过起了网络生涯。网站让你目不暇接,可是有的网站却专门钻IE的空子,当你浏览了它的主页之后,注册表就会被禁止,还会修改你的其他设置,真是害人不浅。还有一招更毒的,你浏览它的主页后,它会下载一个拨号器在你的硬盘,拨号器会断开你当前的连接去拨别的号(想一想,拨一个长途国际电话,一小时多少钱?!),所以,我们这些拨号上网的用户需要一个能随时监测自己IP地址的软件,当IP发生改变时,它会自动的报警;同时,它还应该是透明的,这样即使运行时总在最前面,也不会影响别的窗体。

  废话不多说了,马上开工。首先打开Delphi新建一个工程,添加一个定时器Timer1、一个标签Label1、一个PopupMenu1,并且为PopupMenu1添加一个Exit菜单项。下面就是全部的源代码:

unit Unit1;

interface

http://www.gaodaima.com/?p=65977随时随刻知道自己的IP_Delphi

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, Menus, StdCtrls, ExtCtrls, Winsock; //首先要添加winsock

type

TForm1 = class(TForm)

Timer1: TTimer;

Label1: TLabel;

PopupMenu1: TPopupMenu;

Exit: TMenuItem;

procedure FormCreate(Sender: TObject);

procedure Timer1Timer(Sender: TObject);

procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure ExitClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

oldx,oldy: integer;//添加变量,用做移动窗体

oldIp: string;

implementation

{$R *.dfm}

//下面就是关键所在了

function LIP : string;

type

TaPInAddr = array [0..10] of PInAddr;

PaPInAddr = ^TaPInAddr;

var

phe : PHostEnt;

pptr : PaPInAddr;

Buffer : array [0..63] of char;

I : Integer;

GInitData : TWSADATA;

begin

WSAStartup($101, GInitData);

Result := ”;

GetHostName(Buffer, SizeOf(Buffer));

phe :=GetHostByName(buffer);

if phe = nil then Exit;

pptr := PaPInAddr(Phe^.h_addr_list);

I := 0;

while pptr^[I] <> nil do begin

result:=StrPas(inet_ntoa(pptr^[I]^));

Inc(I);

end;

WSACleanup;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

with Label1 do //定义属性

begin

Caption:=”;

Font.Charset:=ANSI_CHARSET;

Font.Name:=’Arial’;

Font.Size:=10;

Font.Color:=clRed;

Align:=alClient;

PopupMenu:=popupmenu1;

end;

Timer1.Interval:=1000;

Timer1.Enabled:=true;

Label1.Caption:=’IP:’+LIP; //赋值,把Ip赋值给label1

oldIp:=LIP;

BorderStyle:=bsNone;

Alphablend:=true; //呵呵,这个就是让窗口变透明的办法了

Alphablendvalue:=100;

FormStyle:=fsStayOnTop; //让窗体总在最前面

end;

procedure TForm1.Timer1Timer(Sender: TObject);

begin

Label1.Caption :=’IP:’+LIP;

if oldip <> LIP then

Showmessage(‘IP地址已经改变,请检查!’);//提醒用户

end;

procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,

Y: Integer);

begin

if ssleft in shift then //移动窗体Form1

begin

Form1.Left:=Form1.Left+x-oldx;

Form1.Top:=Form1.top+y-oldy;

end;

end;

procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

oldx:=x;

oldy:=y;

end;

procedure TForm1.ExitClick(Sender: TObject);

begin

Close;

end;

end.

  程序比较简单,我只想再说说透明窗体。使窗体透明的方法有好几种,其中一种是我用的这种,方法比较简单。还有一种是调用API函数SetLayeredWindowAttributes,它有4个参数,分别是hwnd、crKey、bAlpha和dwFlags。hwnd指操作的窗口的句柄,crKey是指定要透明的颜色值,是和第四个参数配合使用的(当第四个参数为LWA_COLORKEY),bAlpha是透明参数,当bAlpha为0时窗口全透明,当值为255时为正常的窗口。比如要Form1透明的话,相应的语句是SetLayeredWindowAttributes(form1.Handle, 0, 100, LWA_ALPHA),不过这个API只能在Win2000下用,不支持Win98。

  本程序在Delphi6.0+Win2000下调试通过。

欢迎大家阅读《随时随刻知道自己的IP_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:随时随刻知道自己的IP_Delphi

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址