3

我有一个以文档浏览器为主要窗体的 Delphi 应用程序。当用户打开一个文档时,我们会打开一个编辑器窗口。我们希望每个编辑器在任务栏以及主窗体上都有一个按钮。我已经应用了普通代码来执行此操作(如下),但是当我在使用编辑器窗口后单击主窗体时,编辑器被留在顶部,而焦点在主窗体上。我无法弄清楚是什么导致了这种行为。

舞台设置:我打开主窗体和一个文档窗体。

  1. 单击另一个应用程序,单击主窗体,主窗体保持焦点。(行为符合预期。)

  2. 单击文档表单,单击主表单,文档表单回到前面,但显示为非活动状态。(图片显示结果)

替代文字 http://www.matthew-jones.com/temp_xfer/titlebarfailure.jpg

第一步,这是Delphi 2007,我在项目中有:

Application.MainFormOnTaskBar := True;

对于主窗体,我没有额外的代码。

对于文件形式,我有

procedure TCommonEditForm.CreateParams(var params: TCreateParams);
begin
  inherited;
  params.WndParent := 0; // GetDeskTopWindow; no diff
end;

我试图弄清楚是否有消息导致这种情况发生,但找不到任何合适的东西。我已经在代码中搜索了与“激活”有关的任何内容。欢迎提供线索!

4

3 回答 3

6

我的应用程序按照您描述的方式工作。这是我采取的方法。我本来想找到一种更简单的方法,但从来没有。

我从阅读这些文章开始。这第一个是彼得下面的一篇很棒的文章:

http://groups-beta.google.com/group/borland.public.delphi.winapi/msg/e9f75ff48ce960eb?hl=en

其他信息也在这里找到,但这并没有证明是一个有效的解决方案:供我使用:http: //blogs.teamb.com/DeepakShenoy/archive/2005/04/26/4050.aspx

最终这就是我的结局。

我的启动画面兼作应用程序主窗体。主窗体与应用程序对象有特殊的联系。使用所有辅助形式可以让我获得我正在寻找的行为。

在任务栏上我想要的每个表单中,我都会覆盖 CreateParams。我在我的编辑表单上执行此操作,用户将其视为“主要表单”

procedure TUaarSalesMain.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
  Params.WndParent := GetDesktopWindow;
end;

就 Delphi 而言,我的“主”表单在其 Activitate 函数中加载真正的主表单。我使用一个成员变量来跟踪第一次激活。然后在函数结束时隐藏启动表单,但不要关闭它。这对我来说很重要,因为如果用户正在编辑文档并关闭主窗体,我不希望同时强制关闭编辑屏幕。通过这种方式,所有可见形式都被视为相同。

    if FFirstActivate = false then
      exit;

    FFristActivate := false;

    /* 
       Main Load code here 
       Update Splash label, repaint
       Application.CreateForm
       etc.
    */


    // I can't change visible here but I can change the size of the window
    Self.Height := 0;
    Self.Width := 0;
    Self.Enabled := false;

    //  It is tempting to set Self.Visible := false here but that is not
    // possible because you can't change the Visible status inside this
    // function.  So we need to send a message instead.
    ShowWindow(Self.Handle, SW_HIDE);

  end;

但是还有一个问题。当所有其他表单关闭时,您需要关闭主/启动窗口。我在关闭例程中对 Parent <> nil 进行了额外检查,因为我使用表单作为插件(形成我的目的,它们比框架更有效)。

我不太喜欢使用 Idle 事件,但我没有注意到这会拖累 CPU。

{
  TApplicationManager.ApplicationEventsIdle
  ---------------------------------------------------------------------------
}
procedure TApplicationManager.ApplicationEventsIdle(Sender: TObject;
  var Done: Boolean);
begin

  if Screen.FormCount < 2 then
    Close;
end;

{
  TApplicationManager.FormCloseQuery
  ---------------------------------------------------------------------------
}
procedure TApplicationManager.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
var
  i: integer;
begin

  for i := 0 to Screen.FormCount - 1 do
  begin
    if Screen.Forms[i] <> self then
    begin
      // Forms that have a parent will be cleaned up by that parent so
      // ignore them here and only attempt to close the parent forms
      if Screen.Forms[i].Parent = nil then
      begin
        if Screen.Forms[i].CloseQuery = false then
        begin
          CanClose := false;
          break;
        end;
      end;
    end;
  end;

end;

{
  TApplicationManager.FormClose
  ---------------------------------------------------------------------------
}
procedure TApplicationManager.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
  i: integer;
begin

  for i := Screen.FormCount - 1 downto 0 do
  begin
    if Screen.Forms[i] <> self then
    begin
      // Forms that have a parent will be cleaned up by that parent so
      // ignore them here and only attempt to close the parent forms
      if Screen.Forms[i].Parent = nil then
      begin
        Screen.Forms[i].Close;
      end;
    end;
  end;

end;

到目前为止,这对我很有帮助。我确实为 Vista 做了一个小改动,因为我的“主/启动”屏幕的图标仍在显示。我不记得那是什么了。我可能不需要在初始屏幕上设置宽度、高度、启用和发送隐藏消息。我只是想确保它没有出现:-)。

处理关闭事件是必要的。如果我没记错的话,Windows 发送关闭消息时需要这样做。我认为只有主表单才能收到该信息。

于 2009-04-29T14:48:26.517 回答
0

抱歉,如果这真的很愚蠢,但是您没有将 formstyle 设置为 fsStayOnTop 吗?这可以解释这种行为。

于 2009-04-29T13:08:27.660 回答
0

也许在 createparams 中添加这个

Params.ExStyle := Params.ExStyle OR WS_EX_APPWINDOW;

或者在代码中的任何地方尝试这个。我在表单 .OnCreate 事件中使用它。

SetWindowLong(Wnd, GWL_EXSTYLE, 
  GetWindowLong(Wnd, GWL_EXSTYLE) or WS_EX_APPWINDOW) ;

这样做的缺点是,如果主窗体被最小化,其他窗体也会隐藏,但在主窗体出现时恢复。

于 2009-04-29T14:08:11.697 回答