Center Dialog On Screen Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Center Dialog On Screen

Post by Natulux »

Hey guys,

I want my application to send a message to the user on start under a certain condition and I would like to have that message in the center of my screen.
Problem is, that there can (but must not) be a little frame in the corner already. And since this is the first frame created, it is the TopLevelWindow and my message desperately wants to show in the center of that frame (which is in the corner).

First I had

Code: Select all

wxMessageBox(wxString::Format("My Msg"), GetAPP_Name(), 4 | wxICON_EXCLAMATION, NULL);
I then tried several other things, none of which made any difference:

Code: Select all

//with and without wxDIALOG_NO_PARENT, wxCENTRE, wxICON_EXCLAMATION
wxMessageDialog md(NULL, wxString::Format("My Msg"), GetAPP_Name(), wxOK | wxCENTRE | wxDIALOG_NO_PARENT | wxICON_EXCLAMATION);
md.CenterOnScreen(); // or md.Center()
md.ShowModal(); //or md.Show()
It does even ignore a postion, if I provide one:

Code: Select all

wxPoint pt(300, 300);
wxMessageDialog md(NULL, wxString::Format("My Msg"), GetAPP_Name(), wxOK | wxDIALOG_NO_PARENT | wxICON_EXCLAMATION, pt);
//or md.SetPosition(pt);
md.ShowModal();
Anything else I can do to ignore a wxFrame and center a dialog?

Thanks!
Cheers Natu
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Center Dialog On Screen

Post by doublemax »

Under Windows wxMessageDialog is a native control which is always displayed centered on its parent. And if you pass NULL as parent, wx will take the fist toplevel window it finds as parent.

Code: Select all

void wxMessageDialog::DoCentre(int dir)
{
#ifdef wxHAS_MSW_TASKDIALOG
    // Task dialog is always centered on its parent window and trying to center
    // it manually doesn't work because its HWND is not created yet so don't
    // even try as this would only result in (debug) error messages.
    if ( HasNativeTaskDialog() )
        return;
#endif // wxHAS_MSW_TASKDIALOG

    wxMessageDialogBase::DoCentre(dir);
}
The only solution i can think of, is to use wxGenericMessageDialog. But it looks a little different.
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Center Dialog On Screen

Post by Natulux »

doublemax wrote: Fri Mar 15, 2019 11:32 am Under Windows wxMessageDialog is a native control which is always displayed centered on its parent. And if you pass NULL as parent, wx will take the fist toplevel window it finds as parent.
That explains it. It feels a little strange, because the constructor even takes a wxPoint, but then ignores it.
doublemax wrote: Fri Mar 15, 2019 11:32 amThe only solution i can think of, is to use wxGenericMessageDialog. But it looks a little different.
The wxGenericMessageDialog does use the TopWindow as well and also ignores the CenterOnScreen().

Luckily in this case I can workaround by making the frame pointer a member and destroy it before the message is shown. I guess the only other possible solution would be to use a frame to show the message in my case.

Cheers
Natu
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Center Dialog On Screen

Post by doublemax »

The wxGenericMessageDialog does use the TopWindow as well and also ignores the CenterOnScreen().
That's strange. Because i tested it and it worked for me.
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Center Dialog On Screen

Post by Natulux »

doublemax wrote: Fri Mar 15, 2019 12:46 pm
The wxGenericMessageDialog does use the TopWindow as well and also ignores the CenterOnScreen().
That's strange. Because i tested it and it worked for me.
Strange indeed.
I was testing with wxWidgets 3.1.1 in vs2015 with Window10x64 and the little frame is opened without a parent with

Code: Select all

mysplash->ShowWithoutActivating();
Maybe this is just a special behaviour of just this setting.

But for now Im already satisfied with the fact that it does not crash anymore. ;-)
Post Reply