【问题标题】:Minimize the whole application when a child modal form is minimized最小化子模态表单时最小化整个应用程序
【发布时间】:2012-01-25 21:46:15
【问题描述】:

在此附近的另一个问题中,我得到了将模态表单保存在主窗体内的工作区中的答案。 我能做到这一点的方法(再次感谢大卫)是捕捉 WMSizing、WMMoving、WMGetMaxMinInfo 和我的 porpuose WMShowwindow 消息。 我不接受消息处理,我认为这可能是我管理消息的方式导致我没有得到我需要的结果。

我的应用程序中的所有表单都是模态的。但是你可以在同一个执行线程中打开很多。 (主窗体,form1,form2,form3...formN)。 所有 form(1..N) 都在我的主窗体中的工作区内移动。最大化、恢复、调整大小、移动……所有这些都在该工作区的限制范围内。

但是我无法管理如何最小化整个应用程序,然后单击活动模式表单最小化按钮,然后单击任务栏按钮。 该应用程序将在 XP 和 W7 中使用...我正在使用 DelphiXE 进行开发。

项目可以从这里下载(Project files - Mainform、panel、button、SecondaryForm、unit,仅此而已),只是为了看看我在问这里之前找到的所有建议都尝试了。

这是将模态表单保存在工作区中的原始单元的源代码。

unit uFormularios;

interface

uses Classes, SysUtils, Windows, Messages, Forms, DBGrids, StdCtrls, Menus, Graphics, ComCtrls, Math;

type
  TForm_en_ventana = class(TForm)
  private
    inicializada: boolean;
    bCentrada     : boolean;
    bMaximizada   : boolean;
    ancho_original: integer;
    alto_original : integer;
    procedure WMShowWindow(var Message: TWMShowWindow); message WM_SHOWWINDOW;
    procedure WMSizing(var msg: TMessage); message WM_SIZING;
    procedure WMMoving(Var msg: TMessage); message WM_MOVING;
    procedure WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
  public
    constructor Create(AOwner: TComponent); override;
    property centrada: boolean read bCentrada write bCentrada;
    property maximizada: boolean read bMaximizada write bMaximizada;
  end;

procedure MaximizarFormulario(var F; MaximaAltura: integer = 0; MaximoAncho: integer = 0; Centrado: boolean = TRUE);
procedure InicializarVentanaTrabajo(const izq, der, arr, aba: integer);

var
  ESPACIO_DE_TRABAJO, VENTANA_DE_TRABAJO: TRect;

implementation

constructor TForm_en_ventana.Create(AOwner: TComponent);
begin
  inherited;
  centrada     := TRUE;
  maximizada   := false;
  inicializada := false;
end;

Procedure TForm_en_ventana.WMGetMinMaxInfo(Var msg: TWMGetMinMaxInfo);
begin
  inherited;
  with msg.MinMaxInfo^.ptMaxPosition do
    begin
      x := VENTANA_DE_TRABAJO.Left;
      y := VENTANA_DE_TRABAJO.Top;
    end;
  with msg.MinMaxInfo^.ptMaxSize do
    begin
      x := VENTANA_DE_TRABAJO.Right - VENTANA_DE_TRABAJO.Left;
      y := VENTANA_DE_TRABAJO.Bottom - VENTANA_DE_TRABAJO.Top;
    end;
  with msg.MinMaxInfo^.ptMaxTrackSize do
    begin
      x := VENTANA_DE_TRABAJO.Right - VENTANA_DE_TRABAJO.Left;
      y := VENTANA_DE_TRABAJO.Bottom - VENTANA_DE_TRABAJO.Top;
    end;
  with msg.MinMaxInfo^.ptMinTrackSize do
    begin
      x := ancho_original;
      y := alto_original;
    end;
end;

procedure TForm_en_ventana.WMSizing(var msg: TMessage);
var
  R: PRect;
begin
  R        := PRect(msg.LParam);
  R.Left   := Max(R.Left, VENTANA_DE_TRABAJO.Left);
  R.Right  := Min(R.Right, VENTANA_DE_TRABAJO.Right);
  R.Top    := Max(R.Top, VENTANA_DE_TRABAJO.Top);
  R.Bottom := Min(R.Bottom, VENTANA_DE_TRABAJO.Bottom);
  Caption  := 'Ancho: ' + inttostr(ancho_original) + ' - Alto: ' + inttostr(alto_original);
end;

procedure TForm_en_ventana.WMMoving(var msg: TMessage);
var
  R     : PRect;
  dx, dy: integer;
begin
  R  := PRect(msg.LParam);
  dx := 0;
  dy := 0;
  if R.Left < VENTANA_DE_TRABAJO.Left then
    dx := VENTANA_DE_TRABAJO.Left - R.Left;
  if R.Right > VENTANA_DE_TRABAJO.Right then
    dx := VENTANA_DE_TRABAJO.Right - R.Right;
  if R.Top < VENTANA_DE_TRABAJO.Top then
    dy := VENTANA_DE_TRABAJO.Top - R.Top;
  if R.Bottom > VENTANA_DE_TRABAJO.Bottom then
    dy := VENTANA_DE_TRABAJO.Bottom - R.Bottom;
  OffsetRect(R^, dx, dy);
end;

procedure TForm_en_ventana.WMShowWindow(var Message: TWMShowWindow);
begin
  if inicializada then
    Exit;
  inicializada          := TRUE;
  ancho_original        := Width;
  alto_original         := Height;
  Constraints.MinHeight := Height;
  Constraints.MinWidth  := Width;
  if centrada then
    begin
      Left := (((VENTANA_DE_TRABAJO.Right - VENTANA_DE_TRABAJO.Left) - Width) div 2) + VENTANA_DE_TRABAJO.Left;
      Top  := (((VENTANA_DE_TRABAJO.Bottom - VENTANA_DE_TRABAJO.Top) - Height) div 2) + VENTANA_DE_TRABAJO.Top;
    end;
  if maximizada then
    SendMessage(Handle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
end;

procedure InicializarVentanaTrabajo(const izq, der, arr, aba: integer);
begin
  VENTANA_DE_TRABAJO.Left   := izq;
  VENTANA_DE_TRABAJO.Right  := der;
  VENTANA_DE_TRABAJO.Top    := arr;
  VENTANA_DE_TRABAJO.Bottom := aba;
end;

procedure MaximizarFormulario(var F; MaximaAltura: integer = 0; MaximoAncho: integer = 0; Centrado: boolean = TRUE);
begin
  LockWindowUpdate(TForm(F).Handle);
  TForm(F).Left := ESPACIO_DE_TRABAJO.Left;
  if MaximoAncho = 0 then
    TForm(F).Width := ESPACIO_DE_TRABAJO.Right
  else
    begin
      if ESPACIO_DE_TRABAJO.Right < MaximoAncho then
        TForm(F).Width := ESPACIO_DE_TRABAJO.Right
      else
        TForm(F).Width := MaximoAncho;
    end;
  TForm(F).Top := ESPACIO_DE_TRABAJO.Top;
  if MaximaAltura = 0 then
    TForm(F).Height := ESPACIO_DE_TRABAJO.Bottom
  else
    begin
      if ESPACIO_DE_TRABAJO.Bottom < MaximaAltura then
        TForm(F).Height := ESPACIO_DE_TRABAJO.Bottom
      else
        TForm(F).Height := MaximaAltura;
    end;
  if ((MaximoAncho <> 0) or (MaximaAltura <> 0)) and (Centrado) then
    begin
      TForm(F).Left := (ESPACIO_DE_TRABAJO.Right - TForm(F).Width) div 2;
      TForm(F).Top  := (ESPACIO_DE_TRABAJO.Bottom - TForm(F).Height) div 2;
    end;
  LockWindowUpdate(0);
end;

initialization

SystemParametersInfo(SPI_GETWORKAREA, 0, @ESPACIO_DE_TRABAJO, 0);
VENTANA_DE_TRABAJO := ESPACIO_DE_TRABAJO;

end.

感谢任何可以帮助我的人!

【问题讨论】:

  • 如果你有一个模态表单,我认为这是不可避免的。主窗体已禁用,无法通过 UI 操作最小化。我必须问你想在这里制作什么样的用户界面。您的问题似乎都源于您与系统作斗争的事实。你不应该让窗口表现得像这样。
  • 当用户尝试最小化活动表单时,我需要最小化整个应用程序(应用程序和所有打开的表单,不管它们是否处于活动状态)。我试着按照你说的来捕捉 WMSIZE 消息以捕获 SIZE_MINIMIZING。
  • 也许,管理一个跟踪打开表单(main、form1、form2、form3、form_active)的全局列表。并隐藏、显示...捕获 wmshowwindow 消息(在 sc_minimize anf sc_restore 上)?这是满足我需求的“好”方式吗?
  • 您选择 UI 的动机是什么?你为什么不做一些标准的事情?
  • @DavidHeffernan “你为什么不做一些标准的事情?” - 史蒂夫乔布斯或爱迪生会怎么想你的话...... jeje。

标签: forms delphi delphi-xe minimize


【解决方案1】:

只需在模态表单中捕获最小化和恢复消息并执行此操作...

procedure TTheModalForm.WMSysCommand(var Msg: TWMSysCommand);
begin
  if (fsModal in FormState) or not Application.MainForm.Visible then
  begin
    case Msg.CmdType of
      SC_MINIMIZE:
      begin
        ShowWindow(Application.Handle, SW_SHOWMINNOACTIVE);
      end;

      SC_RESTORE:
      begin
        ShowWindow(Application.Handle, SW_SHOWNORMAL);
        inherited;
      end;

    else
      inherited;
    end;
  end
  else
    inherited;
end;

【讨论】:

    【解决方案2】:

    我也需要这个,我尝试了另一个答案,但它不起作用。幸运的是,我设法使它工作,如下所示:

    procedure TFoodsForm.WMSysCommand(var Msg: TWMSysCommand);
    begin
     if (fsModal in FormState) and (Msg.CmdType and $FFF0 = SC_MINIMIZE)
      then Application.MainForm.WindowState:= wsMinimized
      else inherited;
    end;
    

    【讨论】:

      【解决方案3】:

      谢谢你,JFGravel!这对我来说非常有用,但我永远无法让 SC_RESTORE 被抓到这里,但如果没有,恢复工作正常,所以这是我的简短版本:

        if (Message.CmdType and $FFF0) = SC_MINIMIZE then
          ShowWindow(Application.Handle, SW_SHOWMINNOACTIVE)
        else
          inherited;
      

      【讨论】:

        猜你喜欢
        • 2016-03-11
        • 2011-09-11
        • 2019-10-20
        • 2018-08-24
        • 1970-01-01
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多