unit PreviewFormUnit; {$mode ObjFPC}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, LCLType, StdCtrls, LCLIntf, ExtCtrls, Windows; type { TPreviewForm } TPreviewForm = class(TForm) Label1: TLabel; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState); procedure Timer1Timer(Sender: TObject); private FParentHWND: HWND; protected procedure WndProc(var Message: TMessage); override; public procedure EmbedIntoParent(pHWND: HWND); end; var PreviewForm: TPreviewForm; implementation {$R *.lfm} { TPreviewForm } procedure TPreviewForm.FormCreate(Sender: TObject); begin //FParentHWND := 0; BorderStyle := bsNone; Left := 0; Top := 0; Width := 152; Height := 112; Label1.Left := (ClientWidth - Label1.Width) div 2; Label1.Top := (ClientHeight - Label1.Height) div 2; Label1.Font.Color := clBlue; end; // 親ウインドウを指定 procedure TPreviewForm.EmbedIntoParent(pHWND: HWND); var r: TRect; begin FParentHWND := pHWND; if pHWND <> 0 then begin ParentWindow := pHWND; Windows.SetParent(self.Handle, pHWND); SetWindowLong(Self.Handle, GWL_STYLE, GetWindowLong(self.Handle, GWL_STYLE) or WS_CHILD); Windows.GetClientRect(pHWND, r); MoveWindow(Self.Handle, 0, 0, r.Right - r.Left, r.Bottom - r.Top, True); Visible := True; end; end; procedure TPreviewForm.Timer1Timer(Sender: TObject); begin // 一定時間毎に自身が消えるべきかチェックする //Windows.Beep(440, 100); if (FParentHWND <> 0) and (not Windows.IsWindow(FParentHWND)) then begin // 親ウインドウが存在していないので終了 //Windows.Beep(1000, 100); Application.Terminate; end else if not Windows.IsWindowVisible(self.Handle) then begin // 自分が非表示にされているなら終了 //Windows.Beep(1000, 1000); Application.Terminate; end; end; procedure TPreviewForm.WndProc(var Message: TMessage); begin //Windowsから閉じろとメッセージが来ているなら終了 case Message.Msg of WM_CLOSE, WM_DESTROY, WM_NCDESTROY: begin //Windows.Beep(2000, 300); Application.Terminate; end; end; inherited WndProc(Message); end; procedure TPreviewForm.FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState); begin if FParentHWND = 0 then begin // 開発時用。ESCキーで終了 if Key = VK_ESCAPE then Application.Terminate; end; end; //initialization // RegisterClass(TPreviewForm); end.