⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.pas

📁 基于delphi的远程控制系统,希望对大家有用
💻 PAS
字号:
unit client;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ScktComp, ComCtrls,MMSystem;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    sb: TStatusBar;
    procedure Button1Click(Sender: TObject); //准备与服务器连接
    procedure clsError(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);  //通讯过程中发生错误
    procedure clsConnect(Sender: TObject; Socket: TCustomWinSocket);   //已成功连接上
    procedure clsConnecting(Sender: TObject; Socket: TCustomWinSocket); //连接过程中
    procedure clsRead(Sender: TObject; Socket: TCustomWinSocket); //感应服务器端得命令
    procedure FormClose(Sender: TObject; var Action: TCloseAction);//自定义截屏函数
    Procedure getinfo(info:string);  //读取管理员发来得信息
    procedure clsDisconnect(Sender: TObject; Socket: TCustomWinSocket);  //已成功断开连接
    function  GetOSVersion:integer;//得到操作系统类型
    procedure set2000xp; //对于2000及xp系统关机前要作权限查询
    
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure Tform1.set2000xp;   //执行关机操作
var
  hToken:THandle;
  tkp : TOKEN_PRIVILEGES;
  ReturnLength : DWord;
begin
  OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or  TOKEN_QUERY, hToken);
  LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
  tkp.PrivilegeCount := 1;
  tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;
  ReturnLength :=0;
  AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);
end;

function Tform1.GetOSVersion:integer;  //取得操作系统版本
Var
  osVerInfo : TOSVersionInfo;
begin
  osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  If (GetVersionEx(osVerInfo)) Then
    begin
     case ( osVerInfo.dwPlatformId ) Of
       VER_PLATFORM_WIN32_NT : { Windows NT/2000 }
         begin
           result:=1;//win2k 或 winxp
         end;
       VER_PLATFORM_WIN32_WINDOWS : { Windows 9x/ME }
         begin
            result:=0;//win95--98--98se--me
         end
       else
         result:=0;
     end;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if edit1.text='' then
   begin
    application.MessageBox('服务器地址还没有指定!','系统提醒',mb_iconwarning+mb_ok);
    edit1.SetFocus;
    exit;
   end;
  if edit2.text='' then
   begin
    application.MessageBox('服务器开放本服务的端口还没有指定!','系统提醒',mb_iconwarning+mb_ok);
    edit2.SetFocus;
    exit;
   end;
  cls.Address:=edit1.text;
  cls.Port:=strtoint(edit2.text);
  if button1.Caption='连接' then
    begin
      try
        if cls.Active then cls.Active:=false;//连接前先关闭   ??????
        cls.Active:=true;
        sb.Panels[0].Text:='正在连接到'+edit1.text+'服务器!';
      finally     //总会执行得语句
        edit1.Enabled:=false;
        edit2.Enabled:=false;
        button1.Caption:='断开';
      end;
    end
  else
    begin
      try
        cls.Active:=false;
        sb.Panels[0].Text:='正在断开与服务器的连接...';
      finally
        edit1.Enabled:=true;
        edit2.Enabled:=true;
        sb.Panels[0].Text:='已经断开与服务器的连接!';
        button1.Caption:='连接';
      end;
    end;
end;

procedure TForm1.clsError(Sender: TObject; Socket: TCustomWinSocket;
  ErrorEvent: TErrorEvent; var ErrorCode: Integer);
var
  errorstr:string;
begin
  if ErrorEvent=eeSend then
     errorstr:='发送数据出现错误'
   else
     if ErrorEvent=eeConnect then
      begin
       errorstr:='连接失败,无法连接到指定服务器';
       button1.Caption:='连接';
      end
      else
        if ErrorEvent=eeDisconnect then
         begin
          errorstr:='断开连接失败,暂时无法与服务器断开连接';
          button1.Caption:='断开';
         end
         else
           if ErrorEvent=eeReceive then
             errorstr:='接收数据出现错误'
            else
              if ErrorEvent=eeAccept then
                errorstr:='出现错误,与服务建立的连接没有完成'
               else
                 errorstr:='出现未知错误,请检查网络是否正常';
     sb.Panels[0].Text:=errorstr;
     errorcode:=0; //返回无错,让系统不再有提示
end;

procedure TForm1.clsConnect(Sender: TObject; Socket: TCustomWinSocket);
begin          //成功连接上
   sb.Panels[0].Text:='成功连接到'+edit1.text+'服务器!';
end;

procedure TForm1.clsConnecting(Sender: TObject; Socket: TCustomWinSocket);
begin          //正在连接过程中
  sb.Panels[0].Text:='正在连接'+edit1.text+'服务器';
end;

procedure TForm1.clsRead(Sender: TObject; Socket: TCustomWinSocket);
var
  ostr,sbstr:string;
  cmd:integer;
  rect:TRect; //定义一个矩形区域
  hDesktop:THandle; //一个窗口句柄
begin
  ostr:=socket.ReceiveText;
  if length(ostr)=2 then //普通命令
  begin
    cmd:=strtoint(ostr);
    case cmd of
      11 :
        begin
          sbstr:='弹开CD-ROM仓门';
          mcisendstring('set cdaudio door open',nil,0,0);
        end;
      10 :
        begin
          sbstr:='关闭CD-ROM仓门';
          mcisendstring('set cdaudio door closed',nil,0,0);
        end;
      21 :
        begin
          rect.Left:=screen.Width div 2;rect.right:=screen.Width div 2;
          rect.top:=screen.height div 2;rect.bottom:=screen.height div 2;
          ClipCursor(@rect); {将鼠标锁定在屏幕中央}
          sbstr:='鼠标已经被管理员锁定';
        end;
      20 :
        begin
          sbstr:='管理员已经解开对鼠标的锁定';
          rect.Left:=0;rect.right:=screen.Width;
          rect.top:=0;rect.bottom:=screen.height;
          ClipCursor(@rect); {恢复鼠标活动范围}
        end;
      31 :
        begin
          sbstr:='系统暂被管理员锁定';
          hDesktop:= FindWindow('Progman', nil);//获取桌面窗口句柄
          ShowWindow(hDesktop, SW_HIDE);{隐藏桌面}
          hDesktop:= FindWindow('Shell_TrayWnd', nil);//获取任务栏句柄
          ShowWindow(hDesktop, SW_HIDE);{隐藏任务栏}
        end;
      30 :
        begin
          sbstr:='管理员已经将系统解锁';
          hDesktop:= FindWindow('Progman', nil);//获取桌面窗口句柄
          ShowWindow(hDesktop, SW_SHOW);{显示桌面}
          hDesktop:= FindWindow('Shell_TrayWnd', nil);//获取任务栏句柄
          ShowWindow(hDesktop, SW_SHOW);{显示任务栏}
        end;
      41 :
        begin
          sbstr:='管理员准备将本系统重新启动';
          if getosversion=1 then
            set2000xp;        //关机
          if ExitWindowsEx(EWX_REBOOT,2) then//reboot,2为重新启动计算机
            application.Terminate;
        end;
      40 :
        begin
          sbstr:='管理员准备将本系统关闭';
          if getosversion=1 then
            set2000xp;
          if ExitWindowsEx(EWX_SHUTDOWN,8) then//shutdown,8为关闭计算机
            application.Terminate;
        end;
    end;//end of case
  end
  else  //发送的文字信息
    begin
      sbstr:='管理员发来文字信息';
      getinfo(ostr);
    end;
  sb.Panels[1].Text:=sbstr;//在状态栏显示服务器发来的例子令解释
end;

Procedure Tform1.getinfo(info:string);
var
  str:string;
  ch:char;
  prom:integer;//显示窗口类型
begin
  str:=info;
  ch:=str[1];
  delete(str,1,1);//去掉前面对话框标志,只留下文本信息
  if str='shutdown' then
  begin
    cls.Active:=false;
    button1.Caption:='连接';
  end
  else
  begin
    case ch of
      's','S' :
           prom:=mb_iconstop;
      'w','W' :
           prom:=mb_iconwarning;
      'q','Q' :
           prom:=mb_iconquestion;
      else
           prom:=mb_iconinformation;
    end;
    application.MessageBox(pchar(str),'系统信息',prom+mb_ok);
    exit;
  end;
 // close;//关闭窗口
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if cls.Active then
    application.MessageBox('请先断开与服务器的连接后再关闭本程序','系统提醒',mb_iconstop+mb_ok);
end;

procedure TForm1.clsDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
  sb.Panels[0].Text:='已经断开与服务器的连接!';
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -