DS_CENTER

DS_CENTER is the style attribute of dialog resource to center the dialog box in the working area of the monitor that contains the owner window. If no owner window is specified, the dialog box is centered in the working area of a monitor determined by the system. The working area is the area not obscured by the taskbar or any appbars. Below example shows this attribute in a static resource file however this style parameter can be used in runtime with CreateWindow() call.


/////////////////////////////////////////////////////////////////////////////
//
// Dialog.rc
//

IDD_DLGCENTER_DIALOG DIALOGEX 100, 100, 265, 200
STYLE DS_SETFONT | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION \
| WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFC Dialog in Middle of Screen"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,152,179,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,208,179,50,14
    CTEXT           "Center of desktop",IDC_STATIC,8,96,250,8
END

Center Dialog resource

Using SetWindowPos function

A dialog or top level window can be centered in the desktop using SetWindowPos. We use the position parameters to do this operation. However SetWindowPos can be used to change size and layer of ordering also.

SetWindowPos function

Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.

Syntax

BOOL WINAPI SetWindowPos(
  _In_     HWND hWnd,
  _In_opt_ HWND hWndInsertAfter,
  _In_     int  X,
  _In_     int  Y,
  _In_     int  cx,
  _In_     int  cy,
  _In_     UINT uFlags
);

Application using SetWindowPos

/* CDLGCenterDlg.h */
class CDLGCenterDlg : public CDialogEx
{
public :
  CDLGCenterDlg(CWnd* pParent = NULL);
  enum { IDD = IDD_DLGCENTER_DIALOG };
protected :
  virtual BOOL OnInitDialog();
  DECLARE_MESSAGE_MAP()
};

/* CDLGCenterDlg.cpp */
CDLGCenterDlg::CDLGCenterDlg(CWnd* pParent /*=NULL*/)
  : CDialogEx(IDD_DLGCENTER_DIALOG, pParent)
{
}

BEGIN_MESSAGE_MAP(CDLGCenterDlg, CDialogEx)
END_MESSAGE_MAP()

BOOL CDLGCenterDlg::OnInitDialog()
{
  int screenwidth, screenheight;
    int dlgwidth, dlgheight, x, y;
  RECT rect;
  CDialogEx::OnInitDialog();

  /* Get Screen width and height */
  screenwidth = GetSystemMetrics(SM_CXSCREEN);
  screenheight = GetSystemMetrics(SM_CYSCREEN);

    /* Get Window rect top, left, right, bottom */
  this->GetWindowRect(&rect);

    /* Calculate Window width and height */  
  dlgwidth = rect.right - rect.left;
  dlgheight = rect.bottom - rect.top;
  
   /* Calculate Window left, top (x,y) */
    x = (screenwidth - dlgwidth) / 2;
  y = (screenheight - dlgheight) / 2;
  
  /* Reposition Window left, top (x,y) */
  this->SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE);

  return TRUE; 
}

Output

Center Dialog or Window using SetWindowPos

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#