Get screen and desktop size

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.