1. Get the screen size
Method I: Using GetSystemMetrics ()
int nwidth = GetSystemMetrics (sm_cxscreen); int nheight = GetSystemMetrics (Sm_cyscreen);
Get 1920*1080
Example: Implement Window Center display
Screen size int cx = GetSystemMetrics (sm_cxscreen); int cy = GetSystemMetrics (sm_cyscreen);//Window center CRect RT; GetWindowRect (&RT); SetWindowPos (NULL, (CX-RT). Width ())/2, (Cy-rt. Height ())/2, 0, 0, swp_nosize);//or with MoveWindow () MoveWindow (cx-rt. Width ())/2, (Cy-rt. Height ())/2, Rt. Width (), Rt. Height ());
For example: a dynamically created window and centered display:
Create window//m_clsinstdlg.domodal (); Do not use modal windows, will block the program M_clsinstdlg.create (Idd_install_dialog, GetDlgItem (Idd_menu_dialog)); M_hinstdlg = M_clsinstdlg.m_ hwnd;//set title M_clsinstdlg.settitle (m_strrescfg.sresname);//window Center int cx = getsystemmetrics (sm_cxscreen); int cy = GetSystemMetrics (Sm_cyscreen); CRect Rt;m_clsinstdlg.getwindowrect (&RT); M_clsinstdlg.setwindowpos (NULL, (cx-rt. Width ())/2, (Cy-rt. Height ())/2, 0, 0, swp_nosize);//Display window M_clsinstdlg.showwindow (sw_show);
Method II: Using GetDesktopWindow (), GetWindowRect ()
</pre><pre name= "code" class= "CPP" >getdesktopwindow ()->getwindowrect (m_desktoprect); int nwidth = m_desktoprect.width () int nheight = M_desktoprect.height ();
2. Get the desktop size
CRect Rectworkarea; SystemParametersInfo (Spi_getworkarea, 0, &rectworkarea, spif_sendchange); int nwidth = Rectworkarea.width (); int nheight = Rectworkarea.height ();
Get 1920*1040 (excluding the height of the taskbar)
Example: Implement Desktop Center display
Desktop size CRect Rectworkarea; SystemParametersInfo (Spi_getworkarea, 0, &rectworkarea, spif_sendchange); Desktop size//window center CRect RT; GetWindowRect (&RT); int NX = (rectworkarea.width ()-RT. Width ())/2;int NY = (rectworkarea.height ()-rt.nheight ())/2; SetWindowPos (NULL, NX, NY, 0, 0, swp_nosize | Swp_nozorder | Swp_noredraw); Do not add swp_nomove (the coordinate setting is ignored)
Get screen and desktop size