连接服务器设置

在窗体中添加二个Panel,三个Label,三个Edit,一个BitBtn,二个SpeedButton,一个StatusBar1控件,还添加一个ScoketConnection1控件(在DataSnap控件类中)如图:

unit U_Connection;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, ComCtrls, ImgList, ExtCtrls, Registry, Spin, Db,
  DBClient, variants;

type
  TfrmLjsz = class(TForm)
    Panel1: TPanel;
    StatusBar1: TStatusBar;
    Panel2: TPanel;
    Label1: TLabel;
    edtAppHost: TEdit;
    Label2: TLabel;
    edtAppIP: TEdit;
    btnAppName: TBitBtn;
    Label3: TLabel;
    ScoketConnection1:ScoketConneciton;
    edtPort: TEdit;
    btnOK: TSpeedButton;
    btnCancel: TSpeedButton;
    procedure btnOKClick(Sender: TObject);
    procedure edtAppHostExit(Sender: TObject);
    procedure edtAppIPExit(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnAppNameClick(Sender: TObject);
    procedure edtAppHostKeyPress(Sender: TObject; var Key: Char);
    procedure edtAppIPKeyPress(Sender: TObject; var Key: Char);
    procedure edtPortKeyPress(Sender: TObject; var Key: Char);
  private
    //向注册表写入应用配置信息
    procedure WriteToReg(const bAppHost, bAppIP, aPort: string);
    { Private declarations }
  public
    { Public declarations }
  end;

//打开配置窗口,并测试是否连接成功
function LjszExecute(aFormClass: TFormClass): boolean;

var
  frmLjsz: TfrmLjsz;
  Connected: boolean= false; //代表是否连接成功 。
implementation
uses U_NET;
{$R *.DFM}

//打开配置窗口,并测试是否连接成功
function LjszExecute(aFormClass: TFormClass): boolean;
begin
  with aFormClass.Create(Application) do
  begin
    Connected := False;
    try
      showModal;
    finally
      free;
    end;
    result := Connected;
  end;
end;

//向注册表里写入应用服务器的配置信息
procedure TfrmConnection.WriteToReg(const bAppHost, bAppIP, aPort: string);
var
  Reg: Tregistry;
begin
  reg := Tregistry.create;
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  if not Reg.OpenKey('/Software/物资管理', False) then
  begin
    reg.createkey('/Software/物资管理');
    Reg.OpenKey('/Software/物资管理', False);
  end;
  Reg.WriteString('Host', bAppHost);
  Reg.WriteString('Address', bAppIP);
  Reg.WriteString('Port', aPort);
  reg.free;
end;

procedure TfrmConnection.btnOKClick(Sender: TObject);
begin
  if ((edtAppHost.text = null) or (edtAppHost.text = '')) and ((edtAppIP.text = null) or (edtAPPIP.text = '')) then
  begin
    Application.MessageBox('请输入应用服务器名字或IP!', '输入错误..', mb_iconinformation + mb_defbutton1);
    exit;
  end;
  try
    strtoint(edtPort.text);
  except
    Application.MessageBox('请输入正确的端口号', '输入错误..', mb_iconinformation + mb_defbutton1);
    exit;
  end;
  statusbar1.panels[0].text := '正在连接应用程序服务器,请稍候..';
  statusbar1.refresh;
  Screen.cursor := crHourGlass;
  SocketConnection.Connected := false;
  SocketConnection.Host := edtAppHost.text;
  SocketConnection.Address := edtAppIP.text;
  SocketConnection.Port := strtoint(edtPort.text);
  try
    SocketConnection.Connected := true;
    screen.Cursor := crDefault;
    application.MessageBox('连接成功', '物资管理系统', mb_iconinformation + mb_defbutton1);
    Connected := True;
  except
    screen.cursor := crdefault;
    application.MessageBox('连接失败', '物资管理系统', mb_iconinformation + mb_defbutton1);
    statusbar1.panels[0].text := '连接失败,请重试..';
    statusbar1.refresh;
    exit;
  end;
  //连接成功向注册表里写入应用服务器配置信息
  WriteToReg(edtAppHost.Text, edtAppIP.Text, edtPort.Text);
  close;
end;

//关闭窗口
procedure TfrmConnection.btnCancelClick(Sender: TObject);
begin
  close;
end;

//利用可视化窗体来选择计算机
procedure TfrmConnection.btnAppNameClick(Sender: TObject);
begin
  edtAppIP.clear;
  //NetExecute方法打开TfrmNet窗口,返回选定的计算机名
  edtAppHost.text := NetExecute(TFrmNet);
end;

//当应用服务器名输入后,自动将应用服务器IP地址输入框置空
procedure TfrmConnection.edtAppHostExit(Sender: TObject);
begin
  if edtAppHost.Text <> '' then
    edtAppIP.Text := '';
end;

//当应用服务器IP地址输入后,自动将应用服务器名输入框置空
procedure TfrmConnection.edtAppIPExit(Sender: TObject);
begin
  if edtAppIP.Text <> '' then
    edtAppHost.Text := '';
end;

//应用服务器名输入框按键事件,当按下“回车”键后,将输入焦点转移到输入端口号输入框
procedure TfrmConnection.edtAppHostKeyPress(Sender: TObject; var Key: Char);
begin
  edtAppIP.clear;
  if key = #13 then
    edtPort.SetFocus;
end;

//应用服务器IP地址输入框按键事件,当按下“回车”键后,将输入焦点转移到输入端口号输入框
procedure TfrmConnection.edtAppIPKeyPress(Sender: TObject; var Key: Char);
begin
  edtAppHost.clear;
  if key = #13 then
    edtPort.SetFocus;
end;

//端口号输入框按键事件,当按下“回车”键后,调用“确定”按钮的Click方法
procedure TfrmConnection.edtPortKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    btnOK.click;
end;

end.

U_NET单元见下一编列举网络资源;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值