private void _InitializeCaptionButtonLocation() { if (!Utility.IsOSVistaOrNewer || !Standard.NativeMethods.IsThemeActive()) { this._LegacyInitializeCaptionButtonLocation(); return; } TITLEBARINFOEX tITLEBARINFOEX = new TITLEBARINFOEX() { cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX)) }; TITLEBARINFOEX structure = tITLEBARINFOEX; IntPtr intPtr = Marshal.AllocHGlobal(structure.cbSize); try { Marshal.StructureToPtr(structure, intPtr, false); Standard.NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.SHOWNA); Standard.NativeMethods.SendMessage(this._messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, intPtr); structure = (TITLEBARINFOEX)Marshal.PtrToStructure(intPtr, typeof(TITLEBARINFOEX)); } finally { Standard.NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.HIDE); Utility.SafeFreeHGlobal(ref intPtr); } RECT rECT = RECT.Union(structure.rgrect_CloseButton, structure.rgrect_MinimizeButton); RECT windowRect = Standard.NativeMethods.GetWindowRect(this._messageHwnd.Handle); Rect rect = new Rect((double)(rECT.Left - windowRect.Width - windowRect.Left), (double)(rECT.Top - windowRect.Top), (double)rECT.Width, (double)rECT.Height); this.WindowCaptionButtonsLocation = DpiHelper.DeviceRectToLogical(rect); }
private void _InitializeCaptionButtonLocation() { if (!Utility.IsOSVistaOrNewer || !NativeMethods.IsThemeActive()) { this._LegacyInitializeCaptionButtonLocation(); return; } TITLEBARINFOEX titlebarinfoex = new TITLEBARINFOEX { cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX)) }; IntPtr intPtr = Marshal.AllocHGlobal(titlebarinfoex.cbSize); try { Marshal.StructureToPtr(titlebarinfoex, intPtr, false); NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.SHOW); NativeMethods.SendMessage(this._messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, intPtr); titlebarinfoex = (TITLEBARINFOEX)Marshal.PtrToStructure(intPtr, typeof(TITLEBARINFOEX)); } finally { NativeMethods.ShowWindow(this._messageHwnd.Handle, SW.HIDE); Utility.SafeFreeHGlobal(ref intPtr); } RECT rect = RECT.Union(titlebarinfoex.rgrect_CloseButton, titlebarinfoex.rgrect_MinimizeButton); RECT windowRect = NativeMethods.GetWindowRect(this._messageHwnd.Handle); Rect deviceRectangle = new Rect((double)(rect.Left - windowRect.Width - windowRect.Left), (double)(rect.Top - windowRect.Top), (double)rect.Width, (double)rect.Height); Rect windowCaptionButtonsLocation = DpiHelper.DeviceRectToLogical(deviceRectangle); this.WindowCaptionButtonsLocation = windowCaptionButtonsLocation; }
private void _InitializeCaptionButtonLocation() { // There is a completely different way to do this on XP. if (!Utility.IsOSVistaOrNewer || !NativeMethods.IsThemeActive()) { _LegacyInitializeCaptionButtonLocation(); return; } var tbix = new TITLEBARINFOEX { cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX)) }; IntPtr lParam = Marshal.AllocHGlobal(tbix.cbSize); try { Marshal.StructureToPtr(tbix, lParam, false); // This might flash a window in the taskbar while being calculated. // WM_GETTITLEBARINFOEX doesn't work correctly unless the window is visible while processing. // use SW.SHOWNA instead SW.SHOW to avoid some brief flashing when launched the window NativeMethods.ShowWindow(_messageHwnd.Handle, SW.SHOWNA); NativeMethods.SendMessage(_messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, lParam); tbix = (TITLEBARINFOEX)Marshal.PtrToStructure(lParam, typeof(TITLEBARINFOEX)); } finally { NativeMethods.ShowWindow(_messageHwnd.Handle, SW.HIDE); Utility.SafeFreeHGlobal(ref lParam); } // TITLEBARINFOEX has information relative to the screen. We need to convert the containing rect // to instead be relative to the top-right corner of the window. RECT rcAllCaptionButtons = RECT.Union(tbix.rgrect_CloseButton, tbix.rgrect_MinimizeButton); // For all known themes, the RECT for the maximize box shouldn't add anything to the union of the minimize and close boxes. Assert.AreEqual(rcAllCaptionButtons, RECT.Union(rcAllCaptionButtons, tbix.rgrect_MaximizeButton)); RECT rcWindow = NativeMethods.GetWindowRect(_messageHwnd.Handle); // Reorient the Top/Right to be relative to the top right edge of the Window. var deviceCaptionLocation = new Rect( rcAllCaptionButtons.Left - rcWindow.Width - rcWindow.Left, rcAllCaptionButtons.Top - rcWindow.Top, rcAllCaptionButtons.Width, rcAllCaptionButtons.Height); Rect logicalCaptionLocation = DpiHelper.DeviceRectToLogical(deviceCaptionLocation); WindowCaptionButtonsLocation = logicalCaptionLocation; }
internal static TITLEBARINFOEX GetTitleBarInfoEx(IntPtr hwnd) { var tbi = new TITLEBARINFOEX(); tbi.cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX)); var ptr = Marshal.AllocHGlobal(tbi.cbSize); try { Marshal.StructureToPtr(tbi, ptr, false); if (SendMessage(hwnd, (int)WindowMessages.WM_GETTITLEBARINFOEX, IntPtr.Zero, ptr) == IntPtr.Zero) { throw new NotSupportedException(); } return((TITLEBARINFOEX)Marshal.PtrToStructure(ptr, tbi.GetType())); } finally { Marshal.DestroyStructure(ptr, typeof(TITLEBARINFOEX)); Marshal.FreeHGlobal(ptr); } }
private void _InitializeCaptionButtonLocation() { if (!Utility.IsOSVistaOrNewer || !NativeMethods.IsThemeActive()) { _LegacyInitializeCaptionButtonLocation(); return; } var tbix = new TITLEBARINFOEX { cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX)) }; var lParam = Marshal.AllocHGlobal(tbix.cbSize); try { Marshal.StructureToPtr(tbix, lParam, false); NativeMethods.ShowWindow(_messageHwnd.Handle, SW.SHOW); NativeMethods.SendMessage(_messageHwnd.Handle, WM.GETTITLEBARINFOEX, IntPtr.Zero, lParam); tbix = (TITLEBARINFOEX)Marshal.PtrToStructure(lParam, typeof(TITLEBARINFOEX)); } finally { NativeMethods.ShowWindow(_messageHwnd.Handle, SW.HIDE); Utility.SafeFreeHGlobal(ref lParam); } var rcAllCaptionButtons = RECT.Union(tbix.rgrect_CloseButton, tbix.rgrect_MinimizeButton); Assert.AreEqual(rcAllCaptionButtons, RECT.Union(rcAllCaptionButtons, tbix.rgrect_MaximizeButton)); var rcWindow = NativeMethods.GetWindowRect(_messageHwnd.Handle); var deviceCaptionLocation = new Rect(rcAllCaptionButtons.Left - rcWindow.Width - rcWindow.Left, rcAllCaptionButtons.Top - rcWindow.Top, rcAllCaptionButtons.Width, rcAllCaptionButtons.Height); var logicalCaptionLocation = DpiHelper.DeviceRectToLogical(deviceCaptionLocation); WindowCaptionButtonsLocation = logicalCaptionLocation; }
internal static extern uint SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TITLEBARINFOEX lParam);
internal static extern uint SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TITLEBARINFOEX lParam);