admin管理员组

文章数量:1296905

I have a difficulty with Form minimizing, I can't find the source of the problem.

I have 2 forms, first one is the main form, and its name is Form1, which I want to minimize. Its minimize/restore functionality at first works good (from the Taskbar or Border minimize button, both are working and the Form does minimize/restore). But, after showing a second form (Form2), Form1 minimizes only one time, and after restore it just stops, as if nothing happens when I press to minimize button.

Form1 itself works normal, I even put a TMemo on it to illustrate that it's not frozen! I checked every solution variant I can find on the Internet.

Form2 has StayOnTop functionality. I need this, so is there any solution without touching Form2's StayOnTop features? Maybe I'm wrong, but I think that StayOnTop codes of Form2 are stopping Form1 from minimizing.

Unit1:

type
  TForm1 = class(TForm)
    Button1: TButton;
    ApplicationEvents1: TApplicationEvents;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ApplicationEvents1Minimize(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
  //ShowWindow(Form1.Handle, SW_MINIMIZE);
  //SendMessage(Form1.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  If Form2.Visible then SendMessage(Form2.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);   // for keeping Form2 not going to be minimize
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //
end;

Unit2:

type
  TForm2 = class(TForm)
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Function Application_Hook(var Message: TMessage): Boolean;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function TForm2.Application_Hook(var Message: TMessage): Boolean;
begin
  Result := Message.Msg = WM_ACTIVATEAPP;
  if Result then
  begin
    Message.Result := 0;
    if TWMActivateApp(Message).Active then PostMessage(Application.Handle, CM_ACTIVATE, 0, 0)
                                      else PostMessage(Application.Handle, CM_DEACTIVATE, 0, 0);
  end;
end;

procedure TForm2.FormActivate(Sender: TObject);
begin
  // Functionality for Stay On Top
  SetWindowPos(Form2.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or  SWP_NOSIZE or SWP_NOACTIVATE);
  SetWindowLong(Form2.Handle, GWL_EXSTYLE, GetWindowLong(Form2.Handle, GWL_EXSTYLE) OR WS_EX_TOPMOST);
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  Application.HookMainWindow(Application_Hook);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  Application.UnhookMainWindow(Application_Hook);
end;

本文标签: winapiDelphi Form minimize problem after second form with StayOnTop features showedStack Overflow