画面およびデスクトップサイズの取得

2065 ワード

1.画面サイズの取得
方法I:GetSystemMetrics()の使用
int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeight = GetSystemMetrics(SM_CYSCREEN);

1920*1080を得る
注意:一部のゲームでは解像度が変更されるため、隠していたウィンドウが再現されます.使用方法2をお勧めします.
たとえば、ウィンドウを中央に表示する
//    
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);

//    
CRect rt;
GetWindowRect(&rt);
SetWindowPos(NULL, (cx - rt.Width()) / 2, (cy - rt.Height()) / 2, 0, 0, SWP_NOSIZE);
//   MoveWindow()
MoveWindow((cx - rt.Width())/2, (cy - rt.Height())/2, rt.Width(), rt.Height());

たとえば、動的に作成されたウィンドウが中央に表示されます.
//    
//m_clsInstDlg.DoModal(); //       ,     
m_clsInstDlg.Create(IDD_INSTALL_DIALOG, GetDlgItem(IDD_MENU_DIALOG));
m_hInstDlg = m_clsInstDlg.m_hWnd;

//    
m_clsInstDlg.SetTitle(m_strResCfg.sResName);

//    
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);

//    
m_clsInstDlg.ShowWindow(SW_SHOW);

方法II:GetDesktopWindow()を使用し、GetWindowRect()
GetDesktopWindow()->GetWindowRect(m_DesktopRect);  
int nWidth = m_DesktopRect.Width();
int nHeight = m_DesktopRect.Height();

1920*1080を得る
2.デスクトップサイズの取得
CRect rectWorkArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, SPIF_SENDCHANGE);
int nWidth = rectWorkArea.Width();
int nHeight = rectWorkArea.Height();
は1920*1040(タスクバーの高さを除く)を得た.
例:デスクトップの中央表示を実現する
//    
CRect rectWorkArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, SPIF_SENDCHANGE); //    

//    
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); //    SWP_NOMOVE(       )