• 在Win32程序中显示Dos调试窗口,可暂停(AllocConsole,WriteConsole,FreeConsole函数,GetStdHandle函数取得输入句柄)


    在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料。

    用到的API函数如下:

    //创建控制台
    AllocConsole;

    //获取控制台窗口
    GetStdHandle;

    //向控制台输出信息
    WriteConsole;

    //释放控制台
    FreeConsole;

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
        //控制台句柄
        h_Console:THandle;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      p:PChar;
      num:Cardinal;
    begin
      //获取控制台窗口
      h_Console := GetStdHandle(STD_OUTPUT_HANDLE);
      p := PChar(Edit1.Text);
      //向控制台输出信息
      WriteConsole(h_Console,p,Length(Edit1.Text),num,nil);
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      //创建控制台
      AllocConsole;
    end;
    
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      if h_Console = 0 then Exit;
      //释放控制台
      FreeConsole;
    end;
    
    end.

    参考:http://www.cnblogs.com/key-ok/p/3429861.html

    ------------------------------------------------------------------------------------

    function PauseConsole(Prompt: PAnsiChar): boolean;
    var
      hStdIn, hStdOut: THandle;
      dwRd, dwWr, i: Cardinal;
      cBuf: array [0..128] of TInputRecord;
    begin
      result := false;
      hStdIn := GetStdHandle(STD_INPUT_HANDLE);
      hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
      if ((hStdIn <> 0) and (hStdOut <> 0)) then
      begin
         WriteFile(hStdOut,Prompt[0],lstrlenA(Prompt),dwWr,nil);
         while ReadConsoleInput(hStdIn,cBuf[0],128,dwRd) do
         begin
           for i := 0 to dwRd do
           begin
             if ((cBuf[i].EventType = KEY_EVENT) and (cBuf[i].Event.KeyEvent.bKeyDown)) then
             begin
               Result := true;
               exit;
             end;
           end;
         end;
      end;
    end;
    
     
      try
        PauseConsole('Press a key to continue...');
      except
        on E:Exception do
          Writeln(E.Classname, ': ', E.Message);
      end;

    参考:http://www.cnblogs.com/key-ok/p/3380446.html

  • 相关阅读:
    转:SVN Eclipse插件Subclipse安装和配置
    Apache、php、mysql单独安装配置
    HDU 1150:Machine Schedule(二分匹配,匈牙利算法)
    Oracle 数据的导入和导出(SID service.msc)
    swift-数组array
    wxWidgets刚開始学习的人导引(4)——wxWidgets学习资料及利用方法指导
    用php 把数组中偶数,选择出来
    java 异常 之 实战篇(trows 和 try catch Dead Code)
    语言处理程序
    使用Maven构建和部署J2EE应用程序的EAR文件
  • 原文地址:https://www.cnblogs.com/findumars/p/4748525.html
Copyright © 2020-2023  润新知