win7下主副屏切换

extern "C" __declspec(dllexport) bool ToSetPrimaryDisPlay(){

	int XiangYE_width = 1440;
	int XiangYe_Height = 900;
	int Immer_width = 1920;
	int Immer_Height = 1080;

	DEVMODE deviceMode;
	deviceMode.dmSize = sizeof(DEVMODE);
	deviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_POSITION | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS;
	DISPLAY_DEVICE DisplayDevice;
	DISPLAY_DEVICE DisPlay_Info;
	ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
	DisplayDevice.cb = sizeof(DisplayDevice);

	ZeroMemory(&DisPlay_Info, sizeof(DISPLAY_DEVICE));
	DisplayDevice.cb = sizeof(DisPlay_Info);

	int old_Coord_X = 0;
	int old_Coord_Y = 0;

	SetDisplayConfig(0, NULL, 0, NULL, (SDC_APPLY | SDC_TOPOLOGY_EXTEND));

	for (int i = 0; EnumDisplayDevices(NULL, i, &DisplayDevice, 0); i++)
	{
		//EnumDisplayDevices(DisplayDevice.DeviceName, 0, &DisPlay_Info, 0);
		
		if ( (DisplayDevice.StateFlags &DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) )
		{
			EnumDisplaySettings(DisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &deviceMode);
			
			
			DWORD dwFlags = CDS_UPDATEREGISTRY | CDS_NORESET;

			if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)){

				old_Coord_X = deviceMode.dmPosition.x;
				old_Coord_Y = deviceMode.dmPosition.y;
				deviceMode.dmPosition.x = 0;
				deviceMode.dmPosition.y = 0;
				if (deviceMode.dmPelsWidth == XiangYE_width){
					dwFlags |= CDS_SET_PRIMARY;
					if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, dwFlags, NULL))
					{
						return false;
					}
					break;
				}
				if (deviceMode.dmPelsWidth == Immer_Height){
					deviceMode.dmDisplayOrientation = DMDO_90;
					int temp = deviceMode.dmPelsHeight;
					deviceMode.dmPelsHeight = deviceMode.dmPelsWidth;
					deviceMode.dmPelsWidth = temp;
					if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, 0, NULL)){
						return false;
					}
				}
			}
			
		}
	}

	for (int i = 0; EnumDisplayDevices(NULL, i, &DisplayDevice, 0); i++)
	{
		if ((DisplayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP))
		{
			EnumDisplaySettings(DisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &deviceMode);
			DWORD dwFlags = CDS_UPDATEREGISTRY | CDS_NORESET;

			if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE){
				deviceMode.dmPosition.x -= old_Coord_X;
				deviceMode.dmPosition.y -=old_Coord_Y ;

				if (deviceMode.dmPelsWidth == Immer_Height){
					deviceMode.dmDisplayOrientation = DMDO_90;
					int temp = deviceMode.dmPelsHeight;
					deviceMode.dmPelsHeight = deviceMode.dmPelsWidth;
					deviceMode.dmPelsWidth = temp;

					if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, 0, NULL)){
						return false;
					}
					break;
				}

				if (deviceMode.dmPelsWidth == Immer_width){
					//这种情况比较特殊了
					if (DISP_CHANGE_SUCCESSFUL == ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, 0, NULL)){
						return true;
					}
				}

				//if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &deviceMode, NULL, dwFlags, NULL))
				//{
				//	return false;
				//}
			}
		}
	}
需要注意一点的是,在交换主副屏时,需要同时交换屏幕左上角的顶点位置

你可能感兴趣的:(winAPI编程)