0

In C++Builder, I noticed that if I open a sub-form using ShowModal() (sub-form's PopupMode is pmAuto, and PopupParent is blank), and if I call Application->Minimize() then close the sub-form by setting its ModalResult, when I restore the main form then it remains unable to be used.

I checked the Enabled property as well as ::IsWindowEnabled() and it says it is. From what I recall, when wanting to prevent a parent window from being able to get focus, you'd disable it and it behaved like a Modal DialogBox was opened over it. But C++Builder must be doing something special?

Is there a way around this issue?

Here is a project that shows it. Using 11.3.

Unit1.h

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.AppEvnts.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TApplicationEvents *ApplicationEvents1;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall ApplicationEvents1Minimize(TObject *Sender);
private:    // User declarations
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Form2=new TForm2(this);
    Form2->ShowModal();
    delete Form2;
    Form2=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Minimize(TObject *Sender)
{
    // close form
    if (Form2) {
        Form2->ModalResult=mrOk;
    }
}
//---------------------------------------------------------------------------

Unit1.dfm

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 442
  ClientWidth = 628
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 15
  object Button1: TButton
    Left = 296
    Top = 272
    Width = 75
    Height = 25
    Caption = 'Open SubForm'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ApplicationEvents1: TApplicationEvents
    OnMinimize = ApplicationEvents1Minimize
    Left = 192
    Top = 192
  end
end

Unit2.h

//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:    // IDE-managed Components
    TLabel *Label1;
    TButton *Button1;
private:    // User declarations
    virtual void __fastcall WndProc(TMessage &Message);

public:     // User declarations
    __fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif

Unit2.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm2::WndProc(TMessage &Message)
{
    if (Message.Msg==WM_SYSCOMMAND) {
        if (Message.WParam==SC_MINIMIZE) {
            Application->Minimize();
        }
    }
    __super::WndProc(Message);
}

Unit2.dfm

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 442
  ClientWidth = 628
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  PopupMode = pmAuto
  TextHeight = 15
  object Label1: TLabel
    Left = 272
    Top = 216
    Width = 69
    Height = 15
    Caption = 'Minimize Me'
  end
  object Button1: TButton
    Left = 272
    Top = 320
    Width = 75
    Height = 25
    Caption = 'Close'
    Default = True
    ModalResult = 1
    TabOrder = 0
  end
end
user3161924
  • 1,849
  • 18
  • 33

1 Answers1

0

You can re-enable a minimized window by sending SC_RESTORE command. Based on this answer.

void __fastcall TForm1::ApplicationEvents1Minimize(TObject *Sender)
{
    // close form
    if (Form2) {
        Form2->ModalResult=mrOk;
        SendMessage(Form1->Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
    }
}
VLL
  • 9,634
  • 1
  • 29
  • 54