C++/Opening a dialog box in the upper center of the page.
Expert: Zlatko - 9/8/2010
QuestionHi Eddie,
I am using Visual C++ 6.
Since it is somewhat tall, I would like to have a dialog box open in the upper-center of the screen. At present, Visual C++ 6's default opens in the center of the screen, leaving the remainder of the dialog box below the screen.
Would you please show me a way to do this?
Thanks in advance for your help,
Neil McCollum
AnswerHello Neil.
I assume you are using MFC. You can position your dialog window using CWnd::SetWindowPlacement in the dialog's OnInitDialog method.
Here is sample code
int screenWidth = GetSystemMetrics(SM_CXSCREEN); // Primary screen width
WINDOWPLACEMENT wp;
wp.length = sizeof(wp);
this->GetWindowPlacement(&wp);
int windowWidth = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
int windowHeight = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
wp.rcNormalPosition.top = 0;
wp.rcNormalPosition.bottom = wp.rcNormalPosition.top + windowHeight;
wp.rcNormalPosition.left = (screenWidth-windowWidth)/2;
wp.rcNormalPosition.right = wp.rcNormalPosition.left + windowWidth;
this->SetWindowPlacement(&wp);
I hope that helps you out.
Best regards
Zlatko