private unsafe void SetTopmostState(bool isTop) { isTop &= this.AllowTopMost; // Don’t apply state if it’s the same as incoming state if (this.appliedTopMost.HasValue && this.appliedTopMost == isTop) { return; } if (this.Child is null) { return; } var hwndSource = PresentationSource.FromVisual(this.Child) as HwndSource; if (hwndSource is null) { return; } var hwnd = new HWND(hwndSource.Handle); RECT rect; if (!PInvoke.GetWindowRect(hwnd, &rect)) { return; } //Debug.WriteLine("setting z-order " + isTop); const SET_WINDOW_POS_FLAGS SWP_TOPMOST = SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE | SET_WINDOW_POS_FLAGS.SWP_NOOWNERZORDER | SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOMOVE | SET_WINDOW_POS_FLAGS.SWP_NOREDRAW | SET_WINDOW_POS_FLAGS.SWP_NOSENDCHANGING; var left = rect.left; var top = rect.top; var width = rect.GetWidth(); var height = rect.GetHeight(); if (isTop) { PInvoke.SetWindowPos(hwnd, HWND_TOPMOST, left, top, width, height, SWP_TOPMOST); } else { // Z-Order would only get refreshed/reflected if clicking the // the titlebar (as opposed to other parts of the external // window) unless I first set the popup to HWND_BOTTOM // then HWND_TOP before HWND_NOTOPMOST PInvoke.SetWindowPos(hwnd, HWND_BOTTOM, left, top, width, height, SWP_TOPMOST); PInvoke.SetWindowPos(hwnd, HWND_TOP, left, top, width, height, SWP_TOPMOST); PInvoke.SetWindowPos(hwnd, HWND_NOTOPMOST, left, top, width, height, SWP_TOPMOST); } this.appliedTopMost = isTop; }
private static void HideWindowIcon(IntPtr handle) { const SET_WINDOW_POS_FLAGS refreshFrameFlags = SET_WINDOW_POS_FLAGS.SWP_NOMOVE | SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOZORDER | SET_WINDOW_POS_FLAGS.SWP_FRAMECHANGED; var style = (WINDOW_EX_STYLE)GetWindowLong((HWND)handle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); style |= WINDOW_EX_STYLE.WS_EX_DLGMODALFRAME; SetWindowLong((HWND)handle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)style); SetWindowPos((HWND)handle, (HWND)IntPtr.Zero, 0, 0, 0, 0, refreshFrameFlags); SendMessage((HWND)handle, WM_SETICON, 0, 0); SendMessage((HWND)handle, WM_SETICON, 1, 0); }