功能描述
彈出一個窗口, 彈出時沒有獲得輸入焦點, 直到用戶主動切換到本窗口。可以繼承該類, 建立提示窗口, 提供豐富信息, 而不是不限於Tooltip提供的簡單文字信息。 例如類似Office 助手精靈提示窗口。純Window API 調用實現的Window, 但是不能利用Delphi Form 窗口資源. 本類繼承自TForm, 增加少許代碼,就可以利用能夠Delphi Form, 建立各種提示窗口了.
技術點
1, 繼承自TForm類
2, 增加對Delphi自定義消息CM_SHOWINGCHANGED的處理
3. 增加函數SetZOrder處理 BringToFront的調用
調用方法
可以 show(), showmodal(), visible := true/false
參考 procedure TCustomForm.CMShowingChanged(var Message: TMessage);
演示代碼
TKInfoBase = class(TForm)
private
FMyShowed: Boolean;
FFocusWhenShow: Boolean;
FFocusWhenShowModal: Boolean;
procedure SetFocusWhenShow(const Value: Boolean);
procedure SetFocusWhenShowModal(const Value: Boolean);
protected
procedure SetZOrder(TopMost: Boolean); override;
procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
public
property FocusWhenShowModal:Boolean read FFocusWhenShowModal write SetFocusWhenShowModal;
property FocusWhenShow:Boolean read FFocusWhenShow write SetFocusWhenShow;
constructor Create(AOwner:TComponent); override;
end;
//
procedure TKInfoBase.CMShowingChanged(var Message: TMessage);
begin
if FMyShowed then
begin
inherited;
if not Visible then
FMyShowed := False;
end
else
begin
FMyShowed := True;
ShowWindow(Handle, SW_SHOWNOACTIVATE);
end;
end;
constructor TKInfoBase.Create(AOwner: TComponent);
begin
// 窗体屬性Visible = false
inherited;
FMyShowed := False; // 內部控制變量
FormStyle := fsStayOnTop; // 窗口一直處於最上層
FFocusWhenShow := false; // 窗口Show時,不獲取焦點
FocusWhenShowModal := true; // 調用ShowModal時,窗口獲取焦點
end;
procedure TKInfoBase.SetFocusWhenShow(const Value: Boolean);
begin
FFocusWhenShow := Value;
end;
procedure TKInfoBase.SetFocusWhenShowModal(const Value: Boolean);
begin
FFocusWhenShowModal := Value;
end;
procedure TKInfoBase.SetZOrder(TopMost: Boolean);
begin
if (fsModal in FormState) then
if FFocusWhenShowModal then inherited
else
else
if FFocusWhenShow then inherited;
end;