DraftSight 2015 / ARES commander 2015 コマンドライン監視サンプル

※ARES は廉価版でも COM API (ActiveX) が使えます。そちらを使ったほうがスマートです。

■概要
 DraftSight / ARES では、コマンドラインの文字列が取得できないため、
 画面キャプチャーで、コマンド終了待ちを行うためのサンプルです。
 姑息な方法ながら、結構使えそうです。
 ※キャプチャ位置が合わないときは、exe を右クリックし、プロパティーの「互換性」で、
  「高解像度DPI...」にチェックを付けてみて下さい。



■ダウンロード
 DcadReady.zip (exe 本体のみ)


■サンプルコード
 ※DcadCtrl2015.pas の中身は、他のページを参考にして下さい。

unit DcadReadyUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DcadCtrl2015, Vcl.ExtCtrls, Vcl.StdCtrls,
  System.Types, Vcl.Buttons;

type
  TForm2 = class(TForm)
    Image1: TImage;
    Edit1: TEdit;
    Timer1: TTimer;
    Edit2: TEdit;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
  Timer1.Interval := 100;
  Timer1.Enabled := True;
end;

procedure TForm2.Timer1Timer(Sender: TObject);
var
  ARect : TRect;
  h, h2 : HWND;
  Bmp : TBitmap;
  co : Integer;
  i, j : integer;
  Flag : boolean;
  pByteAry :PByteArray;
begin
  // メインウィンドウを取得
  GetDcadMainWinHandle;
  // コマンド入力ウィンドウを取得
  GetDcadCommandWinHandle;
  // InputWindow が 無効であれば、終了
  if not IsWindowEnabled(DcadInputWinHandle) then  begin
    Edit1.Text := '';
    Edit2.Text := '';
    if Assigned(Image1.Picture) then
      Image1.Picture := nil;
    Exit;
  end;

  // コマンド入力ウィンドウの位置と矩形範囲を取得
  GetWindowRect(DcadInputWinHandle, ARect);
  // 左上、右下の座標からウィンドウハンドルを取得
  h  := WindowFromPoint(ARect.TopLeft);
  h2 := WindowFromPoint(Point(ARect.left+118,ARect.Bottom -1));

  Edit2.Text := GetWindowString(h);

  // 左上と右下を確認
  if (h <> DcadInputWinHandle) or (h2 <> DcadInputWinHandle) then begin
    Edit1.Text := '';
    if Assigned(Image1.Picture) then
      Image1.Picture := nil;
  end
  // ウィンドウハンドルが、InputWindow
  else begin
    // ビットマップ生成
    Bmp := TBitmap.Create;
    try
      Bmp.Width := 100;
      Bmp.Height := 20;
      Bmp.PixelFormat :=pf24bit;
      // キャプチャ
      CaptureToBmp(Arect.Left + 15, ARect.Bottom-20, Bmp.Width, Bmp.Height, bmp);
      // Bitmapを表示
      Image1.Picture.Assign(Bmp);

      // Bitmapの左上のピクセルのRGB値を取得
      pByteAry := Bmp.ScanLine[0];
      co := pByteAry[0];
      // Bitmap内を検索
      Flag := True;
      for i := 1 to Bmp.Height - 1 do begin
        pByteAry := Bmp.ScanLine[i];
        for j := 0 to Bmp.Width -1 do begin
          // 左上の色と違う = BUSY
          if co <> pByteAry[j] then begin
            Flag := False;
            Break;
          end;
        end;
      end;
      if not Flag then begin
        Edit1.Text := 'BUSY';
        Edit1.Font.Color := clRed;
      end
      else begin
        Edit1.Text := 'READY';
        Edit1.Font.Color := clBlue;
      end;
    finally
      // ビットマップ破棄
      Bmp.Free;
    end;
  end;
end;

end.