儿童形式始终保持在主要形式之上

时间:2011-05-09 00:25:54

标签: delphi delphi-2007

我正在使用delphi 2007,每次使用像这样的代码创建新表单

var
  Child : TFrmChild;
begin
  Child:=TFrmChild.Create(Self);
  Child.Show();
end;

显示表单并显示在所有其他表单的上方,这没关系,但是当主表单中的单击时,子表单保持在主表单的上方。所以我有两个问题

  1. 为什么即使在主窗体中点击,孩子表单仍然位于主表单之上?
  2. 当我点击它时,我如何能够使主要形式保留在所有其他形式上?
  3. 由于

    更新

    这是儿童表格的dfm

    object FrmChild: TFrmChild
      Left = 549
      Top = 308
      Caption = 'FrmChild'
      ClientHeight = 228
      ClientWidth = 213
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
    end
    

4 个答案:

答案 0 :(得分:4)

你有两个顶级窗口。主要表单是子表单的所有者。拥有的窗口总是出现在它们的所有者之上,这只是Windows的一个规则。

请注意,所有者我指的是Windows概念而不是Delphi概念。

Windows Features主题解释了规则。关键声明是:

  

拥有的窗口始终位于z订单的所有者之上。

至于如何让你的app表现得与众不同,我不太确定。如果你让你的孩子形成无主,那么它将拥有自己的任务栏按钮,例如,当主窗体最小化时,它不会被最小化。

答案 1 :(得分:3)

已向QualityCentral报告。简单的解决方法是设置

  Application.MainFormOnTaskbar := False;

在项目的.dpr文件中。缺点是失去了一些Vista功能。

还有另一种解决方法,但它很笨拙并且还有其他问题(请参阅QC的链接)。

答案 2 :(得分:1)

迟到的答案 - 我刚刚解决了这个问题。我们想要Aero风格的ALT-TAB和WIN-TAB功能,但MainFormOnTaskBar导致了第三方组件的一些问题。 (LMD停靠 - 如果主窗体上有一个停靠站点,并且孩子上有一个停靠站点,拖动孩子上的停靠项目会将主窗体带到前面)

解决这个问题的方法是:

  • 在CreateParams
  • 中将Params.WndParent设置为0
  • 使用ITaskBarList interface管理TaskBar标签。这也将窗口添加到ALT-TAB和WIN-TAB处理程序,

TaskBarList.pas

unit TaskbarList;

interface

uses
  Windows, Messages,
  CommCtrl,
  ShlObj,
  SysUtils, Classes, ComCtrls;

const
  SID_ITaskbarList                            = '{56FDF342-FD6D-11D0-958A-006097C9A090}';
  SID_ITaskbarList2                           = '{602D4995-B13A-429B-A66E-1935E44F4317}';
  SID_ITaskbarList3                           = '{EA1AFB91-9E28-4B86-90E9-9E9F8A5EEFAF}';
  SID_ITaskbarList4                           = '{C43DC798-95D1-4BEA-9030-BB99E2983A1A}';

  CLSID_TaskbarList: TGUID                            = '{56FDF344-FD6D-11d0-958A-006097C9A090}';

type
  ITaskbarList = interface(IUnknown)
    [SID_ITaskbarList]
    function HrInit: HRESULT; stdcall;
    function AddTab(hwnd: HWND): HRESULT; stdcall;
    function DeleteTab(hwnd: HWND): HRESULT; stdcall;
    function ActivateTab(hwnd: HWND): HRESULT; stdcall;
    function SetActiveAlt(hwnd: HWND): HRESULT; stdcall;
  end;

  ITaskbarList2 = interface(ITaskbarList)
    [SID_ITaskbarList2]
    function MarkFullscreenWindow(hwnd: HWND; fFullscreen: BOOL): HRESULT; stdcall;
  end;

type
  THUMBBUTTON = record
    dwMask: DWORD;
    iId: UINT;
    iBitmap: UINT;
    hIcon: HICON;
    szTip: packed array[0..259] of WCHAR;
    dwFlags: DWORD;
  end;
  tagTHUMBBUTTON = THUMBBUTTON;
  TThumbButton = THUMBBUTTON;
  PThumbButton = ^TThumbButton;

// THUMBBUTTON flags
const
  THBF_ENABLED        =  $0000;
  THBF_DISABLED       =  $0001;
  THBF_DISMISSONCLICK =  $0002;
  THBF_NOBACKGROUND   =  $0004;
  THBF_HIDDEN         =  $0008;
  THBF_NONINTERACTIVE = $10;
// THUMBBUTTON mask
  THB_BITMAP          =  $0001;
  THB_ICON            =  $0002;
  THB_TOOLTIP         =  $0004;
  THB_FLAGS           =  $0008;
  THBN_CLICKED        =  $1800;

const
  TBPF_NOPROGRESS    = 0;
  TBPF_INDETERMINATE = $1;
  TBPF_NORMAL        = $2;
  TBPF_ERROR         = $4;
  TBPF_PAUSED        = $8;

  TBATF_USEMDITHUMBNAIL   = $1;
  TBATF_USEMDILIVEPREVIEW = $2;

const
  STPF_NONE                       = $00000000;
  STPF_USEAPPTHUMBNAILALWAYS      = $00000001;
  STPF_USEAPPTHUMBNAILWHENACTIVE  = $00000002;
  STPF_USEAPPPEEKALWAYS           = $00000004;
  STPF_USEAPPPEEKWHENACTIVE       = $00000008;


type
  ITaskbarList3 = interface(ITaskbarList2)
    [SID_ITaskbarList3]
    function SetProgressValue(hwnd: HWND; ullCompleted: ULONGLONG;
      ullTotal: ULONGLONG): HRESULT; stdcall;
    function SetProgressState(hwnd: HWND; tbpFlags: Integer): HRESULT; stdcall;
    function RegisterTab(hwndTab: HWND; hwndMDI: HWND): HRESULT; stdcall;
    function UnregisterTab(hwndTab: HWND): HRESULT; stdcall;
    function SetTabOrder(hwndTab: HWND; hwndInsertBefore: HWND): HRESULT; stdcall;
    function SetTabActive(hwndTab: HWND; hwndMDI: HWND;
      tbatFlags: Integer): HRESULT; stdcall;
    function ThumbBarAddButtons(hwnd: HWND; cButtons: UINT;
      pButton: PThumbButton): HRESULT; stdcall;
    function ThumbBarUpdateButtons(hwnd: HWND; cButtons: UINT;
      pButton: PThumbButton): HRESULT; stdcall;
    function ThumbBarSetImageList(hwnd: HWND; himl: HIMAGELIST): HRESULT; stdcall;
    function SetOverlayIcon(hwnd: HWND; hIcon: HICON;
      pszDescription: LPCWSTR): HRESULT; stdcall;
    function SetThumbnailTooltip(hwnd: HWND; pszTip: LPCWSTR): HRESULT; stdcall;
    function SetThumbnailClip(hwnd: HWND; var prcClip: TRect): HRESULT; stdcall;
  end;

  ITaskbarList4 = interface(ITaskbarList3)
    [SID_ITaskbarList4]

    function SetTabProperties (hwnd: HWND; stpFlags: Integer): HRESULT; stdcall;

  end;

implementation

end.

BaseForm.pas:

unit BaseForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,
  TaskBarList;

type
  TBaseForm = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    class var FTaskBarList: ITaskbarList;
  protected
    class function TaskBarList: ITaskbarList;
    procedure DoShow;override;
    procedure DoHide;override;
    procedure Activate;override;
    procedure CreateParams(var Params: TCreateParams); override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  BaseForm: TBaseForm;

implementation

uses
  ComObj;

{$R *.dfm}

{ TBaseForm }

procedure TBaseForm.Activate;
begin
  inherited;
  TaskBarList.ActivateTab(Handle);
end;

procedure TBaseForm.Button1Click(Sender: TObject);
begin
  TBaseForm.Create(Self).Show;
end;

constructor TBaseForm.Create(AOwner: TComponent);
begin
  inherited;
  // remove taskbar button for Application.Handle
  TaskBarList.DeleteTab(Application.Handle);
end;

procedure TBaseForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  if (Parent = nil) and (ParentWindow = 0) then // don't use on docked Windows
    Params.WndParent := 0;
end;

procedure TBaseForm.DoHide;
begin
  inherited;
  TaskBarList.DeleteTab(Handle);
end;

procedure TBaseForm.DoShow;
begin
  inherited;
  TaskBarList.AddTab(Handle);
end;

class function TBaseForm.TaskBarList: ITaskbarList;
var
  pIntF: IInterface;
begin
  if not assigned(FTaskBarList) then
  begin
    pIntF := CreateComObject(CLSID_TaskbarList);
    pIntF.QueryInterface(ITaskBarList, FTaskBarList);

    FTaskBarList.HrInit;
  end;
  Result := FTaskBarList;
end;

end.

注意:我没有使用OleCheck检查过HRESULTS,因为我希望它能在现场默默地失败。

答案 3 :(得分:0)

  1. 您的孩子表格可能始终处于最佳状态
  2. 你可以使用MDI表格(人形)和MDI儿童表格(儿童表格)。或者你可以使用MTI,请查看这篇文章http://delphi.about.com/b/2010/02/23/tabbed-interface-mti-for-your-mdi-delphi-applications.htm