实现双屏显示-将主显示器扩展到辅助显示设备

 

 

BOOL AddUnattachedDisplayDeviceToDesktop()
{
  DWORD DispNum = 0;
  DISPLAY_DEVICE DisplayDevice;
  DEVMODE defaultMode;
  HDC hdc;
  int nWidth;
  BOOL bFoundSecondary = FALSE;

  hdc = ::GetDC(0);
  nWidth = ::GetDeviceCaps(hdc,HORZRES);
  ::ReleaseDC(0,hdc);

  // Initialize DisplayDevice.
  ::ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
  DisplayDevice.cb = sizeof(DisplayDevice);

  // Get display devices.
  while ((::EnumDisplayDevices(NULL, DispNum, &DisplayDevice, 0)) &&
  (bFoundSecondary == FALSE))
  { 
  ::ZeroMemory(&defaultMode, sizeof(DEVMODE));
  defaultMode.dmSize = sizeof(DEVMODE);
  if (!::EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
  ENUM_REGISTRY_SETTINGS, &defaultMode))
  return FALSE; // Store default failed

  if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE))
  {
  //Found the first secondary device.
  bFoundSecondary = TRUE;
  defaultMode.dmPosition.x += nWidth;
  defaultMode.dmFields = DM_POSITION; 
  ::ChangeDisplaySettingsEx((LPSTR)DisplayDevice.DeviceName, 
  &defaultMode, NULL, CDS_NORESET|CDS_UPDATEREGISTRY, NULL); 

  // A second call to ChangeDisplaySettings updates the monitor.
  ::ChangeDisplaySettings(NULL, 0); 
  } 

  // Reinitialize DisplayDevice. 
  ::ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
  DisplayDevice.cb = sizeof(DisplayDevice);
  DispNum++;
  } // End while the display devices. 
  return TRUE;
}
void CMonitorTestDlg::OnBnClickedButton5()
{
 DetachDisplay();
  //TODO: 在此添加控件通知处理程序代码
}

你可能感兴趣的:(实现双屏显示-将主显示器扩展到辅助显示设备)