EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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 "DLGCenter"
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

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()
10  };
11 
12  /* CDLGCenterDlg.cpp */
13  CDLGCenterDlg::CDLGCenterDlg(CWnd* pParent /*=NULL*/)
14    : CDialogEx(IDD_DLGCENTER_DIALOG, pParent)
15  {
16  }
17 
18  BEGIN_MESSAGE_MAP(CDLGCenterDlg, CDialogEx)
19  END_MESSAGE_MAP()
20 
21  BOOL CDLGCenterDlg::OnInitDialog()
22  {
23    int screenwidth, screenheight;
24      int dlgwidth, dlgheight, x, y;
25    RECT rect;
26    CDialogEx::OnInitDialog();
27 
28    /* Get Screen width and height */
29    screenwidth = GetSystemMetrics(SM_CXSCREEN);
30    screenheight = GetSystemMetrics(SM_CYSCREEN);
31 
32      /* Get Window rect top, left, right, bottom */
33    this->GetWindowRect(&rect);
34 
35      /* Calculate Window width and height */  
36    dlgwidth = rect.right - rect.left;
37    dlgheight = rect.bottom - rect.top;
38    
39     /* Calculate Window left, top (x,y) */
40      x = (screenwidth - dlgwidth) / 2;
41    y = (screenheight - dlgheight) / 2;
42    
43    /* Reposition Window left, top (x,y) */
44    this->SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE);
45 
46    return TRUE; 
47  }

Output

Center Dialog or Window using SetWindowPos

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Tue 29 Apr/2014 14:43:05(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.