public static Rectangle SetPos(IntPtr hWnd, QueryPosResult pos) { APP_BAR_DATA abd = new APP_BAR_DATA(); abd.cbSize = Marshal.SizeOf(abd); abd.hWnd = hWnd; abd.uEdge = pos.edge; abd.rc.top = pos.rc.Top; abd.rc.bottom = pos.rc.Bottom; abd.rc.left = pos.rc.Left; abd.rc.right = pos.rc.Right; // Pass the final bounding rectangle to the system. SHAppBarMessage(ABM_SETPOS, ref abd); MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, true); return(new Rectangle(abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top)); }
public void RefreshPosition() { QueryPosResult ret = QueryPos(this.Handle, this.idealLocation, this.idealSize); SetPos(this.Handle, ret); }
public static QueryPosResult QueryPos(IntPtr hWnd, Point idealLocation, Size idealSize) { APP_BAR_DATA abd = new APP_BAR_DATA(); abd.cbSize = Marshal.SizeOf(abd); abd.hWnd = hWnd; // saved the proposed rectangle Rectangle R = new Rectangle(idealLocation, idealSize); abd.rc.left = R.Left; abd.rc.right = R.Right; // then figure out which screen that rectangle would be on Screen S = Screen.FromRectangle(R); // make it full height abd.rc.top = S.WorkingArea.Top; abd.rc.bottom = S.WorkingArea.Bottom; if (S.Bounds.Left != 0) // multiple monitors { R.Offset(-S.Bounds.Left, 0); } // see whether we are on the left or right side of the screen int centerX = (R.Left + R.Right) >> 1; int centerScreen = S.Bounds.Width >> 1; if (centerX < 0) // multiple monitors { if (Math.Abs(centerX) < centerScreen) { abd.uEdge = ABE_RIGHT; } else { abd.uEdge = ABE_LEFT; } } else if (centerX > centerScreen) { abd.uEdge = ABE_RIGHT; } else { abd.uEdge = ABE_LEFT; } // once we've decided on an edge, align the rect cleanly to that edge switch (abd.uEdge) { case ABE_LEFT: abd.rc.left = S.WorkingArea.Left; abd.rc.right = abd.rc.left + idealSize.Width; break; case ABE_RIGHT: abd.rc.right = S.WorkingArea.Right; abd.rc.left = abd.rc.right - idealSize.Width; break; } // Query the system for an approved size and position. SHAppBarMessage(ABM_QUERYPOS, ref abd); QueryPosResult ret = new QueryPosResult(); ret.edge = abd.uEdge; ret.rc = new Rectangle(abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top); // return the approved position return(ret); }