-
Notifications
You must be signed in to change notification settings - Fork 2
/
uGetOwnIPAddress.pas
186 lines (153 loc) · 4.45 KB
/
uGetOwnIPAddress.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
unit uGetOwnIPAddress;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdUDPClient, IdStack, IdUDPBase, Vcl.ExtCtrls, Vcl.AppEvnts,
Vcl.ComCtrls, IdIPWatch;
type
TForm1 = class(TForm)
Memo1: TMemo;
IdUDPClient1: TIdUDPClient;
Timer1: TTimer;
TrayIcon1: TTrayIcon;
ApplicationEvents1: TApplicationEvents;
Button1: TButton;
IdIPWatch1: TIdIPWatch;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure ApplicationEvents1Minimize(Sender: TObject);
procedure TrayIcon1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure IdIPWatch1StatusChanged(Sender: TObject);
procedure IdIPWatch1Status(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: string);
private
LocalAddresses: TStringList;
LocalAddresses_Prev: TStringList;
function GetLocalAddress: TStringList;
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetLocalAddress():TStringList;
var
LList: TIdStackLocalAddressList;
LAddr: TIdStackLocalAddress;
I: Integer;
LocalAddresses: TStringList;
begin
LocalAddresses := TStringList.Create;
LList := TIdStackLocalAddressList.Create;
try
GStack.GetLocalAddressList(LList);
for I := 0 to LList.Count - 1 do
begin
LAddr := LList[I];
// IPv4 アドレスを取得
if LAddr is TIdStackLocalAddressIPv4 then
begin
LocalAddresses.Add('IPv4: ' + TIdStackLocalAddressIPv4(LAddr).IPaddress);
end;
// IPv6 アドレスを取得
if LAddr is TIdStackLocalAddressIPv6 then
begin
LocalAddresses.Add('IPv6: ' + TIdStackLocalAddressIPv6(LAddr).IPaddress);
end;
end;
finally
LList.Free;
end;
// 結果をソートして返す。
LocalAddresses.Sort;
Result := LocalAddresses;
end;
procedure TForm1.IdIPWatch1Status(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: string);
begin
// Memo1.Lines.Insert(0,AStatusText);
end;
procedure TForm1.IdIPWatch1StatusChanged(Sender: TObject);
begin
// Memo1.Lines.Insert(0,IdIPWatch1.CurrentIP);
// Memo1.Lines.Insert(0,IdIPWatch1.LocalIP);
end;
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
// 最小化時はトレイアイコン化する
Hide();
WindowState := wsMinimized;
TrayIcon1.Visible := True;
TrayIcon1.Animate := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
// Button1Click という名前を付けているが、
// これは試験実装時の名残り。
var
I: Integer;
LocalAddress: String;
ComputerName: Array[0..256] of char;
Size: DWORD;
begin
Memo1.Lines.Clear;
// 現在のIPアドレス一覧を取得する。
LocalAddresses := GetLocalAddress;
LocalAddresses_Prev := LocalAddresses;
// コンピュータ名を取得する
Size := 256;
GetComputerName(ComputerName, Size);
Memo1.Lines.Add('ComputerName: ' + ComputerName);
// Memo1 の内容をクリアして、IPアドレス情報を書き出す。
for i := 0 to LocalAddresses.Count - 1 do
begin
Memo1.Lines.Add(LocalAddresses[i]);
LocalAddress := LocalAddresses[i];
end;
if ( LocalAddresses.Count = 0 ) then
Memo1.Lines.Add('no network connection.');
end;
// フォームが表示されるときにIPアドレス情報を必ず更新する。
procedure TForm1.FormCreate(Sender: TObject);
begin
LocalAddresses_Prev := TStringList.Create;
LocalAddresses_Prev.Add('dummy');
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Button1Click(Sender);
end;
// 一定時間ごとにIPアドレス情報を更新する。
procedure TForm1.Timer1Timer(Sender: TObject);
var
LocalAddressPrevString, LocalAddressString: String;
i: Integer;
begin
LocalAddresses := GetLocalAddress;
for i := 0 to LocalAddresses.Count - 1 do
begin
LocalAddressString := LocalAddressString + LocalAddresses[i];
end;
for i := 0 to LocalAddresses_Prev.Count - 1 do
begin
LocalAddressPrevString := LocalAddressPrevString + LocalAddresses_Prev[i];
end;
if (LocalAddressPrevString = LocalAddressString) then
exit;
TrayIcon1.BalloonTitle := 'ATTENTION';
TrayIcon1.BalloonHint := 'IP addresses is changed.';
TrayIcon1.ShowBalloonHint;
Button1Click(Sender);
end;
procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
TrayIcon1.Visible := False;
Show();
WindowState := wsNormal;
Application.BringToFront();
end;
end.