关闭最小化的窗体会阻止主窗体接受鼠标/键盘事件

问题描述 投票:0回答:1

在C ++ Builder中,我注意到如果我使用

ShowModal()
打开子窗体(子窗体的
PopupMode
pmAuto
,并且
PopupParent
是空白),并且如果我调用
Application->Minimize()
然后关闭子表单通过设置其
ModalResult
,当我恢复主表单时,它仍然无法使用。

我检查了

Enabled
属性以及
::IsWindowEnabled()
,它说是的。据我记得,当想要阻止父窗口获得焦点时,您可以禁用它,它的行为就像在它上面打开一个模态对话框一样。但 C++Builder 一定在做一些特别的事情吗?

有办法解决这个问题吗?

这是一个展示它的项目。使用11.3。

单元1.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

单元1.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;
    }
}
//---------------------------------------------------------------------------

单元1.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

单元2.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

单元2.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
c++builder vcl
1个回答
0
投票

您可以通过发送

SC_RESTORE
命令重新启用最小化窗口。 基于此答案。

void __fastcall TForm1::ApplicationEvents1Minimize(TObject *Sender)
{
    // close form
    if (Form2) {
        Form2->ModalResult=mrOk;
        SendMessage(Form1->Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.