Example #1
0
		public static void SetPlacement(IntPtr windowHandle, string placementXml)
		{
			if (string.IsNullOrEmpty(placementXml))
			{
				return;
			}

			WINDOWPLACEMENT placement;
			byte[] xmlBytes = encoding.GetBytes(placementXml);

			try
			{
				using (MemoryStream memoryStream = new MemoryStream(xmlBytes))
				{
					placement = (WINDOWPLACEMENT)serializer.Deserialize(memoryStream);
				}

				placement.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
				placement.flags = 0;
				placement.showCmd = (placement.showCmd == SW_SHOWMINIMIZED ? SW_SHOWNORMAL : placement.showCmd);

				//if (placement.showCmd == SW_SHOWNORMAL)
				//{
				//	placement.showCmd = SW_SHOWNOACTIVATE;
				//}

				IntPtr closestMonitorPtr = MonitorFromRect(ref placement.normalPosition, MONITOR_DEFAULTTONEAREST);
				MONITORINFO closestMonitorInfo = new MONITORINFO();
				closestMonitorInfo.cbSize = Marshal.SizeOf(typeof (MONITORINFO));
				bool getInfoSucceeded = GetMonitorInfo(closestMonitorPtr, ref closestMonitorInfo);

				if (getInfoSucceeded && !RectanglesIntersect(placement.normalPosition, closestMonitorInfo.rcMonitor))
				{
					placement.normalPosition = PlaceOnScreen(closestMonitorInfo.rcMonitor, placement.normalPosition);
				}

				SetWindowPlacement(windowHandle, ref placement);
			}
			catch (InvalidOperationException)
			{
				// Parsing placement XML failed. Fail silently.
			}
		}
Example #2
0
		private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);