Search code examples
c++c++buildervcltformowlnext

TForm is hiding behind Main window


I have a main Form created with OwlNext that calls a Dialog which is created in vcl.

MyOtherFormClass Form2=new Form2();
void MyMainForm::ShowForm2(void) {
    Form2->ShowDialog(this);
}
class MyOtherFormClass {
    [...]
    TForm *myForm;

  void ShowDialog(TWindow* parent){

    TForm* tf = new TForm((HWND)NULL);
    tf->ParentWindow=parent->Handle;

    myForm= new TForm(tf);
    myForm->Parent=tf->Parent;
    myForm->Caption = "Form2";
    myForm->Height = 950;
    myForm->Width = 1350;
    myForm->BorderIcons << biMinimize << biMaximize << biSystemMenu;
    myForm->BorderStyle = bsSizeable;
    myForm->Show();
}
}

Now I have both windows with a new taskbar-entry.
When I click the Main-WIndow there, it get in front of Form2.
But when I click Form2, it stays behind the Main.
Also, when Form2 makes an alert, closing the alert will focus Main instead of Form2.

MessageBox(NULL, "An Alert!", "!", MB_OKCANCEL)

Can you tell me what I did wrong here?


Solution

  • Why are you creating 2 TForm objects? You only need 1.

    tf->Parent is NULL since tf->ParentWindow is used (BTW, you can pass parent->Handle to the TForm(HWND) constructor), so myForm has no parent window assigned, just an Owner that you are not keeping track of. Owner and Parent are two different things. You probably want the MainForm to be the parent of the Form2 window.

    As for your MessageBox() call, you are not giving it an owner window. You need to do that so it knows which window to stay in front of, and more importantly which window to focus when it closes.

    Try this instead:

    MyOtherFormClass *Form2 = new MyOtherFormClass();
    
    void MyMainForm::ShowForm2(void) {
        Form2->ShowDialog(this);
    }
    
    class MyOtherFormClass {
        //...
        TForm *myForm;
    
        void ShowDialog(TWindow* parent) {
            myForm = new TForm(parent->Handle);
            myForm->Caption = "Form2";
            myForm->Height = 950;
            myForm->Width = 1350;
            myForm->BorderIcons = TBorderIcons() << biMinimize << biMaximize << biSystemMenu;
            myForm->BorderStyle = bsSizeable;
            myForm->Show();
        }
    };
    

    MessageBox(myForm->Handle, "An Alert!", "!", MB_OKCANCEL);