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

📄 conapp.pas

📁 BaiduMp3 search baidu mp3
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Author:       Fran鏾is PIETTE
Creation:     May 01, 2003
Version:      1.00 ALPHA
Description:  A console mode application object for ICS
EMail:        francois.piette@overbyte.be  http://www.overbyte.be
              francois.piette@rtfm.be      http://www.rtfm.be/fpiette
              francois.piette@pophost.eunet.be
Support:      Use the mailing list twsocket@elists.org
              Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1996-2005 by Fran鏾is PIETTE
              Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
              <francois.piette@overbyte.be>

              This software is provided 'as-is', without any express or
              implied warranty.  In no event will the author be held liable
              for any  damages arising from the use of this software.

              Permission is granted to anyone to use this software for any
              purpose, including commercial applications, and to alter it
              and redistribute it freely, subject to the following
              restrictions:

              1. The origin of this software must not be misrepresented,
                 you must not claim that you wrote the original software.
                 If you use this software in a product, an acknowledgment
                 in the product documentation would be appreciated but is
                 not required.

              2. Altered source versions must be plainly marked as such, and
                 must not be misrepresented as being the original software.

              3. This notice may not be removed or altered from any source
                 distribution.

              4. You must register this software by sending a picture postcard
                 to the author. Use a nice stamp and mention your name, street
                 address, EMail address and any comment you like to say.

History:


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit ConApp;

{$I ICSDEFS.INC}

interface

uses
    Windows, SysUtils, Classes, Messages;

const
    WM_STARTUP   = WM_USER + 789;   { Must be unique in each application }

type
    TKeyboardThread = class (TThread)
    protected
        FEvent : THandle;
    public
        ConAppThreadID : Integer;
        constructor Create(Suspended : Boolean);
        destructor  Destroy; override;
        procedure   Execute; override;
        procedure   Terminate;
    end;

    TConApplicationClass = class of TConApplication;

    TConApplication = class(TComponent)
    private
        FThreadID           : Integer;
        FTerminated         : Boolean;
        FKbdThread          : TKeyboardThread;
        FLineMode           : Boolean;
        FLineBuffer         : String;
        FLineEcho           : Boolean;
        FIsInputRedirected  : Boolean;
        FIsOutputRedirected : Boolean;
    protected
        procedure   Terminate; virtual;
        procedure   MessageLoop; virtual;
        function    ProcessMessage: Boolean; virtual;
        procedure   ProcessMessages; virtual;
        procedure   WndProc(var Msg: TMsg); virtual;
        procedure   WMKeyDown(var MsgRec : TMsg); virtual;
        procedure   WMKeyUp(var MsgRec : TMsg); virtual;
        function    GetExeName: String;
    public
        class procedure CreateInstance(AppClass : TConApplicationClass); virtual;
        class procedure Run; virtual;
        class procedure Done; virtual;
        constructor Create(AOwner : TComponent); override;
        destructor  Destroy; override;
        procedure   Execute; virtual;
        procedure   ConsoleWrite(const S : String); virtual;
        procedure   ConsoleWriteLn(const S : String); virtual;
        procedure   DoLineReceived(const Line : String); virtual;
        procedure   DoCharReceived(Ch : Char); virtual;
        property ThreadID   : Integer  read  FThreadID   write FThreadID;
        property Terminated : Boolean  read  FTerminated write FTerminated;
        property LineMode   : Boolean  read  FLineMode   write FLineMode;
        property LineEcho   : Boolean  read  FLineEcho   write FLineEcho;
        property IsInputRedirected  : Boolean  read  FIsInputRedirected;
        property IsOutputRedirected : Boolean  read  FIsOutputRedirected;
        property ExeName            : String   read  GetExeName;
    end;

var
    ConApplication : TConApplication;

implementation

function  CtrlHandlerRoutine(CtrlType : DWORD) : DWORD; stdcall; forward;

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
constructor TConApplication.Create(AOwner: TComponent);
begin
    FLineMode := TRUE;
    FLineEcho := TRUE;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
destructor TConApplication.Destroy;
begin
    FreeAndNil(FKbdThread);
    inherited;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
class procedure TConApplication.CreateInstance(
    AppClass: TConApplicationClass);
var
    CMode : DWORD;
begin
    FreeAndNil(ConApplication);
    ConApplication                     := AppClass.Create(nil);
    ConApplication.FIsInputRedirected  := not GetConsoleMode(GetStdHandle(
                                                  STD_INPUT_HANDLE), CMode);
    ConApplication.FIsOutputRedirected := not GetConsoleMode(GetStdHandle(
                                                  STD_OUTPUT_HANDLE), CMode);
    ConApplication.FThreadID           := GetCurrentThreadID;
    ConApplication.FKbdThread          := TKeyboardThread.Create(TRUE);
    ConApplication.FKbdThread.ConAppThreadID := ConApplication.FThreadID;
    SetConsoleCtrlHandler(@CtrlHandlerRoutine, TRUE);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TConApplication.Execute;
begin
    ConsoleWriteLn('Error: You must override TConApplication.Execute !');
    Terminate;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TConApplication.Terminate;
begin
    PostThreadMessage(FThreadID, WM_QUIT, 0, 0);
    FTerminated := TRUE;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TConApplication.MessageLoop;
var
    Msg : TMsg;
begin
    // If GetMessage retrieves the WM_QUIT, the return value is FALSE and
    // the message loop is broken.
    while (not FTerminated) and GetMessage(Msg, 0, 0, 0) do
        WndProc(Msg);
    FTerminated := TRUE;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TConApplication.ProcessMessage: Boolean;
var
    Msg    : TMsg;
begin
    Result := FALSE;
    if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin
        Result := TRUE;
        if Msg.Message = WM_QUIT then
            FTerminated := TRUE
        else
            WndProc(Msg);
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TConApplication.ProcessMessages;
begin
    while Self.ProcessMessage do
        { loop };
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
class procedure TConApplication.Run;
begin
    ConApplication.ProcessMessage;         // This will create message queue
    PostThreadMessage(ConApplication.FThreadID, WM_STARTUP, 0, 0);
    ConApplication.FKbdThread.Resume;
    ConApplication.MessageLoop;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
class procedure TConApplication.Done;
begin
    SetConsoleCtrlHandler(nil, FALSE);
    FreeAndNil(ConApplication);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TConApplication.WndProc(var Msg: TMsg);
begin
    if Msg.hwnd = 0 then begin        // We process thread's messages
        case Msg.message of
        WM_STARTUP:

⌨️ 快捷键说明

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