2017-03-07 10 views
0

런타임에 이미 기존 TTabSheet에 컨트롤을 추가하려고하면 이벤트 인 OnShow 내에 추가 될 때 컨트롤이 보이지 않습니다.TabSheet1Show 내 TPageControl에 컨트롤 추가

단계 재현 :

  1. 디자이너에 TPageControlTForm A를 추가
  2. 디자이너이 TPageControl 3 개 TTabSheet 객체를 추가합니다.
  3. 처음으로 TTabSheet을 활성화합니다 (디자인 타임에).

헤더 파일 :

#ifndef Unit1H 
#define Unit1H 

#include <System.Classes.hpp> 
#include <Vcl.Controls.hpp> 
#include <Vcl.StdCtrls.hpp> 
#include <Vcl.Forms.hpp> 
#include <Vcl.ComCtrls.hpp> 
//--------------------------------------------------------------------------- 
class TForm1 : public TForm 
{ 
__published: // Von der IDE verwaltete Komponenten 
    TPageControl *PageControl1; 
    TTabSheet *TabSheet1; 
    TTabSheet *TabSheet2; 
    TTabSheet *TabSheet3; 
    TButton *Button1; 
    void __fastcall TabSheet1Show(TObject *Sender); 
private: // Benutzer-Deklarationen 
    TButton *ButtonConstructor; 
    TButton *ButtonOnTabShow; 
public:  // Benutzer-Deklarationen 
    __fastcall TForm1(TComponent* Owner); 
}; 
//--------------------------------------------------------------------------- 
extern PACKAGE TForm1 *Form1; 
//--------------------------------------------------------------------------- 
#endif 

소스 파일 :

#include <vcl.h> 
#pragma hdrstop 

#include "Unit1.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
    : TForm(Owner) 
{ 
    // Adding a TButton in the Form's constructor works 

    TTabSheet *ts = this->TabSheet1; 

    if (!this->ButtonConstructor) 
    { 
     ButtonConstructor = new TButton(ts); 
     ButtonConstructor->Name = "ButtonConstructor"; 
     ButtonConstructor->Caption = "Construct"; 
     ButtonConstructor->Parent = ts; 
    } 


} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::TabSheet1Show(TObject *Sender) 
{ 
    // Adding a TButton in the OnShow Event of TTabSheet does NOT work: 
    // The button stays invisible 

    TTabSheet *ts = dynamic_cast< TTabSheet * >(Sender); 
    // TTabSheet *ts = this->ButtonOnTabShow; // does not make any difference 

    if (!this->ButtonOnTabShow) 
    { 
     ButtonOnTabShow = new TButton(ts); 
     ButtonOnTabShow->Name = "ButtonOnTabShow"; 
     ButtonOnTabShow->Caption = "Show"; 
     ButtonOnTabShow->Parent = ts; 
     // Button should be placed below the other 
     ButtonOnTabShow->Top = ButtonConstructor->Top + ButtonConstructor->Height + 2; 
    } 

    // The following 2 lines would make the Button visible 
    // PageControl1->ActivePageIndex = 1; 
    // PageControl1->ActivePageIndex = 0; 
} 

결과가 :

0
  • 아래의 코드를 실행
    • ButtonConstructorTabSheet2을 클릭 한 후 TabSheet1에, ButtonOnTabShow도 볼 수 돌아 가면
    • ButtonOnTabShow

    볼 수 없습니다 볼 수 있습니다.

    이 버그는 해결할 수없는 버그입니까, 아니면 누락 되었습니까?

  • 답변

    0

    왜 그런지 정확히 모르겠지만 재현 할 수 있습니다. Win32 API에는 탭 시트에 대한 개념이 없기 때문에 TTabSheet 개체의 표시 여부를 관리하는 방법과 관련이 있습니다 (TTabSheet은 탭으로 된 콘텐츠 관리를 쉽게하기 위해 전체 VCL 발명입니다). 또한

    #define WM_ENSUREBUTTONSHOWN (WM_APP+1) 
    
    void __fastcall TForm1::TabSheet1Show(TObject *Sender) 
    { 
        // ... 
    
        if (!this->ButtonOnTabShow) 
        { 
         ButtonOnTabShow = new TButton(ts); 
         // ... 
         PostMessage(Handle, WM_ENSUREBUTTONSHOWN, 0, 0); 
        } 
    } 
    
    void __fastcall TForm1::WndProc(TMessage &Message) 
    { 
        TForm::WndProc(Message); 
        if ((Message.Msg == WM_ENSUREBUTTONSHOWN) && (this->ButtonOnTabShow)) 
        { 
         this->ButtonOnTabShow->Hide(); 
         this->ButtonOnTabShow->Show(); 
        } 
    } 
    

    :

    #define WM_REFRESHTABSHEET (WM_APP+1) 
    
    void __fastcall TForm1::TabSheet1Show(TObject *Sender) 
    { 
        // ... 
    
        if (!this->ButtonOnTabShow) 
        { 
         ButtonOnTabShow = new TButton(ts); 
         // ... 
         PostMessage(Handle, WM_REFRESHTABSHEET, 0, 0); 
        } 
    } 
    
    void __fastcall TForm1::WndProc(TMessage &Message) 
    { 
        TForm::WndProc(Message); 
        if ((Message.Msg == WM_REFRESHTABSHEET) && (this->TabSheet1->Visible)) 
        { 
         this->TabSheet1->Hide(); 
         this->TabSheet1->Show(); 
        } 
    } 
    
    그러나,이 코드를 사용하여 해결할 용이