From 4973b41f3d7e12ffb271cfc7b4eb7d4aa00250f0 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Thu, 25 Jul 2019 12:32:48 +0100 Subject: [PATCH] Cleanup HWND related interop --- .../src/DpiHelper.DpiAwarenessContext.cs | 30 +- src/Common/src/DpiHelper.cs | 29 +- .../Interop/Kernel32/Interop.CloseHandle.cs | 22 ++ .../Kernel32/Interop.GetCurrentProcess.cs | 15 + .../Kernel32/Interop.GetCurrentProcessId.cs | 15 + .../Kernel32/Interop.GetCurrentThread.cs | 15 + .../Kernel32/Interop.GetCurrentThreadId.cs | 15 + .../Kernel32/Interop.GetExitCodeThread.cs | 15 + .../Kernel32/Interop.GetThreadLocale.cs | 15 + .../Interop/Kernel32/Interop.GetTickCount.cs | 14 + .../Kernel32/Interop.SetThreadLocale.cs | 15 + .../Shell32/Interop.SHBrowseForFolder.cs | 2 +- .../Shell32/Interop.SHGetKnownFolderPath.cs | 2 +- .../Interop.SHGetPathFromIDListLongPath.cs | 8 +- .../Interop.SHGetSpecialFolderLocation.cs | 2 +- .../Interop/Shell32/Interop.ShellExecute.cs | 15 + .../Interop.AreDpiAwarenessContextsEqual.cs | 31 ++ .../Interop/User32/Interop.CallNextHookEx.cs | 22 ++ .../Interop/User32/Interop.CallWindowProc.cs | 15 + .../src/Interop/User32/Interop.ClassLong.cs | 14 + .../src/Interop/User32/Interop.ClassStyle.cs | 16 + .../Interop/User32/Interop.CreateWindowEx.cs | 27 ++ .../Interop/User32/Interop.DefFrameProc.cs | 15 + .../Interop/User32/Interop.DefMDIChildProc.cs | 15 + .../Interop/User32/Interop.DefWindowProc.cs | 15 + .../Interop/User32/Interop.DestroyWindow.cs | 22 ++ .../User32/Interop.DpiAwarenessContext.cs | 19 ++ .../Interop/User32/Interop.EnableWindow.cs | 22 ++ .../User32/Interop.EnumThreadWindows.cs | 24 ++ .../src/Interop/User32/Interop.EnumWindows.cs | 17 + .../Interop/User32/Interop.GetActiveWindow.cs | 15 + .../src/Interop/User32/Interop.GetAncestor.cs | 28 ++ .../src/Interop/User32/Interop.GetCapture.cs | 15 + .../Interop/User32/Interop.GetClassInfo.cs | 15 + .../Interop/User32/Interop.GetClassName.cs | 24 ++ ...Interop.GetDpiAwarenessContextForWindow.cs | 60 ++++ .../src/Interop/User32/Interop.GetFocus.cs | 15 + .../User32/Interop.GetForegroundWindow.cs | 15 + .../src/Interop/User32/Interop.GetParent.cs | 22 ++ .../Interop.GetThreadDpiAwarenessContext.cs | 34 ++ .../src/Interop/User32/Interop.GetWindow.cs | 29 ++ .../Interop/User32/Interop.GetWindowLong.cs | 45 +++ .../User32/Interop.GetWindowPlacement.cs | 31 ++ .../Interop/User32/Interop.GetWindowRect.cs | 22 ++ .../Interop.GetWindowThreadProcessId.cs | 22 ++ .../src/Interop/User32/Interop.IsChild.cs | 37 +++ .../src/Interop/User32/Interop.IsWindow.cs | 22 ++ .../Interop/User32/Interop.IsWindowEnabled.cs | 22 ++ .../Interop/User32/Interop.IsWindowUnicode.cs | 15 + .../Interop/User32/Interop.IsWindowVisible.cs | 22 ++ .../src/Interop/User32/Interop.IsZoomed.cs | 15 + .../Interop/User32/Interop.RegisterClass.cs | 14 + .../User32/Interop.RegisterWindowMessage.cs | 14 + .../Interop/User32/Interop.ReleaseCapture.cs | 15 + .../Interop/User32/Interop.SetActiveWindow.cs | 22 ++ .../src/Interop/User32/Interop.SetCapture.cs | 15 + .../Interop/User32/Interop.SetClassLong.cs | 34 ++ .../src/Interop/User32/Interop.SetFocus.cs | 22 ++ .../src/Interop/User32/Interop.SetParent.cs | 37 +++ .../Interop.SetThreadDpiAwarenessContext.cs | 36 +++ .../Interop/User32/Interop.SetWindowLong.cs | 72 +++++ .../User32/Interop.SetWindowPlacement.cs | 14 + .../Interop/User32/Interop.SetWindowPos.cs | 57 ++++ .../Interop/User32/Interop.SetWindowRgn.cs | 30 ++ .../User32/Interop.SetWindowsHookEx.cs | 24 ++ .../src/Interop/User32/Interop.ShowWindow.cs | 22 ++ .../User32/Interop.ShowWindowCommand.cs | 26 ++ .../User32/Interop.UnhookWindowsHookEx.cs | 22 ++ .../Interop/User32/Interop.WINDOWPLACEMENT.cs | 27 ++ .../src/Interop/User32/Interop.WNDCLASS.cs | 27 ++ .../src/Interop/User32/Interop.WindowLong.cs | 21 ++ .../Interop/User32/Interop.WindowPosition.cs | 24 ++ .../src/Interop/User32/Interop.WindowStyle.cs | 52 +++ .../src/Interop/User32/Interop.WndProc.cs | 13 + src/Common/src/NativeMethods.cs | 170 ++-------- src/Common/src/SafeNativeMethods.cs | 70 ---- src/Common/src/ScreenDC.cs | 6 +- src/Common/src/UnsafeNativeMethods.cs | 193 ----------- .../InternalUtilitiesForTests.csproj | 7 + .../src/PlatformDetection.Windows.cs | 14 +- .../src/Misc/NativeMethods.cs | 52 +-- .../src/Misc/UnsafeNativeMethods.cs | 11 - ...System.Windows.Forms.Design.Editors.csproj | 13 +- .../ComponentModel/Design/CollectionEditor.cs | 8 +- .../src/System/Drawing/Design/ColorEditor.cs | 12 +- .../src/System/Drawing/Design/FontEditor.cs | 7 +- .../src/System/Drawing/Design/ImageEditor.cs | 6 +- .../Windows/Forms/Design/FolderNameEditor.cs | 2 +- .../Forms/Design/ImageListImageEditor.cs | 6 +- .../src/System.Windows.Forms.Design.csproj | 29 +- .../Design/DesignerActionPanel.cs | 33 +- .../Forms/Design/Behavior/BehaviorService.cs | 33 +- .../Windows/Forms/Design/ControlDesigner.cs | 20 +- .../Windows/Forms/Design/DesignerActionUI.cs | 26 +- .../Windows/Forms/Design/DesignerFrame.cs | 12 +- .../Forms/Design/FormDocumentDesigner.cs | 2 +- .../Forms/Design/SelectionUIService.cs | 2 +- .../Design/ToolStripAdornerWindowService.cs | 4 +- .../src/System.Windows.Forms.csproj | 1 - .../src/System/Windows/Forms/Application.cs | 187 +++++------ .../src/System/Windows/Forms/AxHost.cs | 47 +-- .../src/System/Windows/Forms/ButtonBase.cs | 2 +- .../System/Windows/Forms/CheckedListBox.cs | 4 +- .../src/System/Windows/Forms/ColorDialog.cs | 2 +- .../src/System/Windows/Forms/ComboBox.cs | 68 ++-- .../src/System/Windows/Forms/CommonDialog.cs | 33 +- .../Windows/Forms/ComponentManagerBroker.cs | 7 +- .../COM2PropertyBuilderUITypeEditor.cs | 3 +- .../COM2PropertyPageUITypeConverter.cs | 3 +- .../System/Windows/Forms/ContainerControl.cs | 8 +- .../Windows/Forms/Control.ActiveXImpl.cs | 24 +- .../src/System/Windows/Forms/Control.Ime.cs | 7 +- .../src/System/Windows/Forms/Control.cs | 303 ++++++++---------- .../System/Windows/Forms/DataGridToolTip.cs | 5 +- .../System/Windows/Forms/DataGridViewCell.cs | 3 +- .../System/Windows/Forms/DateTimePicker.cs | 14 +- .../Forms/Design/ComponentEditorForm.cs | 2 +- .../Forms/Design/ComponentEditorPage.cs | 4 +- .../src/System/Windows/Forms/ErrorProvider.cs | 44 +-- .../src/System/Windows/Forms/FileDialog.cs | 12 +- .../src/System/Windows/Forms/FontDialog.cs | 6 +- .../src/System/Windows/Forms/Form.cs | 303 +++++++++--------- .../src/System/Windows/Forms/GroupBox.cs | 2 +- .../src/System/Windows/Forms/Help.cs | 7 +- .../Forms/KeyboardToolTipStateMachine.cs | 2 +- .../src/System/Windows/Forms/Label.cs | 4 +- .../src/System/Windows/Forms/LinkLabel.cs | 4 +- .../src/System/Windows/Forms/ListBox.cs | 10 +- .../src/System/Windows/Forms/ListView.cs | 35 +- .../src/System/Windows/Forms/MDIClient.cs | 22 +- .../src/System/Windows/Forms/MaskedTextBox.cs | 2 +- .../src/System/Windows/Forms/MessageBox.cs | 2 +- .../src/System/Windows/Forms/MonthCalendar.cs | 4 +- .../src/System/Windows/Forms/NativeWindow.cs | 84 ++--- .../src/System/Windows/Forms/NotifyIcon.cs | 2 +- .../src/System/Windows/Forms/Panel.cs | 11 +- .../src/System/Windows/Forms/PictureBox.cs | 5 +- .../Windows/Forms/Printing/PageSetupDialog.cs | 3 +- .../Windows/Forms/Printing/PrintDialog.cs | 5 +- .../Forms/Printing/PrintPreviewControl.cs | 4 +- .../src/System/Windows/Forms/ProgressBar.cs | 5 +- .../src/System/Windows/Forms/PropertyGrid.cs | 21 +- .../Forms/PropertyGridInternal/GridToolTip.cs | 8 +- .../PropertyGridInternal/PropertyGridView.cs | 66 ++-- .../src/System/Windows/Forms/RichTextBox.cs | 10 +- .../src/System/Windows/Forms/ScrollBar.cs | 4 +- .../System/Windows/Forms/ScrollableControl.cs | 8 +- .../src/System/Windows/Forms/SendKeys.cs | 38 ++- .../System/Windows/Forms/SplitContainer.cs | 4 +- .../src/System/Windows/Forms/Splitter.cs | 8 +- .../src/System/Windows/Forms/StatusBar.cs | 8 +- .../src/System/Windows/Forms/StatusStrip.cs | 12 +- .../src/System/Windows/Forms/StringSorter.cs | 7 +- .../src/System/Windows/Forms/TabControl.cs | 15 +- .../src/System/Windows/Forms/TextBox.cs | 6 +- .../src/System/Windows/Forms/TextBoxBase.cs | 8 +- .../src/System/Windows/Forms/Timer.cs | 8 +- .../src/System/Windows/Forms/ToolBar.cs | 22 +- .../src/System/Windows/Forms/ToolStrip.cs | 16 +- .../System/Windows/Forms/ToolStripDropDown.cs | 55 ++-- .../System/Windows/Forms/ToolStripManager.cs | 32 +- .../src/System/Windows/Forms/ToolTip.cs | 70 ++-- .../src/System/Windows/Forms/TrackBar.cs | 4 +- .../src/System/Windows/Forms/TreeView.cs | 49 ++- .../src/System/Windows/Forms/UpDownBase.cs | 9 +- .../src/System/Windows/Forms/UserControl.cs | 14 +- .../src/System/Windows/Forms/WebBrowser.cs | 15 +- .../System/Windows/Forms/WebBrowserBase.cs | 6 +- .../System/Windows/Forms/WebBrowserHelper.cs | 2 +- .../Windows/Forms/WebBrowserSiteBase.cs | 2 +- .../src/System/Windows/Forms/WinFormsUtils.cs | 2 +- .../WinFormsControlsClassicTests.csproj | 2 +- 172 files changed, 2677 insertions(+), 1565 deletions(-) create mode 100644 src/Common/src/Interop/Kernel32/Interop.CloseHandle.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetCurrentProcess.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetCurrentProcessId.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetCurrentThread.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetCurrentThreadId.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetExitCodeThread.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetThreadLocale.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.GetTickCount.cs create mode 100644 src/Common/src/Interop/Kernel32/Interop.SetThreadLocale.cs create mode 100644 src/Common/src/Interop/Shell32/Interop.ShellExecute.cs create mode 100644 src/Common/src/Interop/User32/Interop.AreDpiAwarenessContextsEqual.cs create mode 100644 src/Common/src/Interop/User32/Interop.CallNextHookEx.cs create mode 100644 src/Common/src/Interop/User32/Interop.CallWindowProc.cs create mode 100644 src/Common/src/Interop/User32/Interop.ClassLong.cs create mode 100644 src/Common/src/Interop/User32/Interop.ClassStyle.cs create mode 100644 src/Common/src/Interop/User32/Interop.CreateWindowEx.cs create mode 100644 src/Common/src/Interop/User32/Interop.DefFrameProc.cs create mode 100644 src/Common/src/Interop/User32/Interop.DefMDIChildProc.cs create mode 100644 src/Common/src/Interop/User32/Interop.DefWindowProc.cs create mode 100644 src/Common/src/Interop/User32/Interop.DestroyWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.DpiAwarenessContext.cs create mode 100644 src/Common/src/Interop/User32/Interop.EnableWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.EnumThreadWindows.cs create mode 100644 src/Common/src/Interop/User32/Interop.EnumWindows.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetActiveWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetAncestor.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetCapture.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetClassInfo.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetClassName.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetDpiAwarenessContextForWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetFocus.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetForegroundWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetParent.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetThreadDpiAwarenessContext.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetWindowLong.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetWindowPlacement.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetWindowRect.cs create mode 100644 src/Common/src/Interop/User32/Interop.GetWindowThreadProcessId.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsChild.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsWindowEnabled.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsWindowUnicode.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsWindowVisible.cs create mode 100644 src/Common/src/Interop/User32/Interop.IsZoomed.cs create mode 100644 src/Common/src/Interop/User32/Interop.RegisterClass.cs create mode 100644 src/Common/src/Interop/User32/Interop.RegisterWindowMessage.cs create mode 100644 src/Common/src/Interop/User32/Interop.ReleaseCapture.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetActiveWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetCapture.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetClassLong.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetFocus.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetParent.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetThreadDpiAwarenessContext.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetWindowLong.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetWindowPlacement.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetWindowPos.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetWindowRgn.cs create mode 100644 src/Common/src/Interop/User32/Interop.SetWindowsHookEx.cs create mode 100644 src/Common/src/Interop/User32/Interop.ShowWindow.cs create mode 100644 src/Common/src/Interop/User32/Interop.ShowWindowCommand.cs create mode 100644 src/Common/src/Interop/User32/Interop.UnhookWindowsHookEx.cs create mode 100644 src/Common/src/Interop/User32/Interop.WINDOWPLACEMENT.cs create mode 100644 src/Common/src/Interop/User32/Interop.WNDCLASS.cs create mode 100644 src/Common/src/Interop/User32/Interop.WindowLong.cs create mode 100644 src/Common/src/Interop/User32/Interop.WindowPosition.cs create mode 100644 src/Common/src/Interop/User32/Interop.WindowStyle.cs create mode 100644 src/Common/src/Interop/User32/Interop.WndProc.cs diff --git a/src/Common/src/DpiHelper.DpiAwarenessContext.cs b/src/Common/src/DpiHelper.DpiAwarenessContext.cs index 74368a092cc..81a0105ab53 100644 --- a/src/Common/src/DpiHelper.DpiAwarenessContext.cs +++ b/src/Common/src/DpiHelper.DpiAwarenessContext.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using static Interop; + namespace System.Windows.Forms { /// @@ -16,7 +18,7 @@ internal static partial class DpiHelper /// The new DPI awareness for the current thread /// An object that, when disposed, will reset the current thread's /// DPI awareness to the value it had when the object was created. - public static IDisposable EnterDpiAwarenessScope(DpiAwarenessContext awareness) + public static IDisposable EnterDpiAwarenessScope(User32.DpiAwarenessContext awareness) { return new DpiAwarenessScope(awareness); } @@ -30,38 +32,36 @@ public static IDisposable EnterDpiAwarenessScope(DpiAwarenessContext awareness) /// returns object created in system aware mode public static T CreateInstanceInSystemAwareContext(Func createInstance) { - using (EnterDpiAwarenessScope(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) + using (EnterDpiAwarenessScope(User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) { return createInstance(); } } - #region Scoping DpiAwareness context helper class - /// /// Class that help setting Dpi awareness context scope /// private class DpiAwarenessScope : IDisposable { private bool dpiAwarenessScopeIsSet = false; - private readonly DpiAwarenessContext originalAwareness = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; + private readonly User32.DpiAwarenessContext originalAwareness = User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; /// /// Enters given Dpi awareness scope /// - public DpiAwarenessScope(DpiAwarenessContext awareness) + public DpiAwarenessScope(User32.DpiAwarenessContext awareness) { try { - if (!CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(awareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED)) + if (!User32.DpiAwarenessContextsEquals(awareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED)) { - originalAwareness = CommonUnsafeNativeMethods.GetThreadDpiAwarenessContext(); + originalAwareness = User32.GetCurrentThreadDpiAwarenessContext(); // If current process dpiawareness is SYSTEM_UNAWARE or SYSTEM_AWARE (must be equal to awareness), calling this method will be a no-op. - if (!CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(originalAwareness, awareness) && - !CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(originalAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE)) + if (!User32.DpiAwarenessContextsEquals(originalAwareness, awareness) && + !User32.DpiAwarenessContextsEquals(originalAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE)) { - originalAwareness = CommonUnsafeNativeMethods.SetThreadDpiAwarenessContext(awareness); + originalAwareness = User32.SetCurrentThreadDpiAwarenessContext(awareness); dpiAwarenessScopeIsSet = true; } } @@ -75,10 +75,7 @@ public DpiAwarenessScope(DpiAwarenessContext awareness) /// /// Dispose object and resources /// - public void Dispose() - { - ResetDpiAwarenessContextChanges(); - } + public void Dispose() => ResetDpiAwarenessContextChanges(); /// /// resetting dpiawareness of the thread. @@ -87,11 +84,10 @@ private void ResetDpiAwarenessContextChanges() { if (dpiAwarenessScopeIsSet) { - CommonUnsafeNativeMethods.TrySetThreadDpiAwarenessContext(originalAwareness); + User32.SetCurrentThreadDpiAwarenessContext(originalAwareness); dpiAwarenessScopeIsSet = false; } } } - #endregion } } diff --git a/src/Common/src/DpiHelper.cs b/src/Common/src/DpiHelper.cs index 6250e777661..ebbe23fd9f9 100644 --- a/src/Common/src/DpiHelper.cs +++ b/src/Common/src/DpiHelper.cs @@ -7,6 +7,7 @@ using System.Runtime.InteropServices; using CAPS = System.Windows.Forms.NativeMethods; +using static Interop; namespace System.Windows.Forms { @@ -55,7 +56,7 @@ internal static void InitializeDpiHelperForWinforms() if (OsVersion.IsWindows10_1607OrGreater) { // We are on Windows 10/1603 or greater, but we could still be DpiUnaware or SystemAware, so let's find that out... - var currentProcessId = SafeNativeMethods.GetCurrentProcessId(); + int currentProcessId = Kernel32.GetCurrentProcessId(); IntPtr hProcess = SafeNativeMethods.OpenProcess(SafeNativeMethods.PROCESS_QUERY_INFORMATION, false, currentProcessId); SafeNativeMethods.GetProcessDpiAwareness(hProcess, out CAPS.PROCESS_DPI_AWARENESS processDpiAwareness); @@ -90,8 +91,8 @@ internal static bool IsPerMonitorV2Awareness { // We can't cache this value because different top level windows can have different DPI awareness context // for mixed mode applications. - DpiAwarenessContext dpiAwareness = CommonUnsafeNativeMethods.GetThreadDpiAwarenessContext(); - return CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + User32.DpiAwarenessContext dpiAwareness = User32.GetCurrentThreadDpiAwarenessContext(); + return User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); } else { @@ -319,29 +320,29 @@ internal static HighDpiMode GetWinformsApplicationDpiAwareness() // For Windows 10 RS2 and above if (OsVersion.IsWindows10_1607OrGreater) { - DpiAwarenessContext dpiAwareness = CommonUnsafeNativeMethods.GetThreadDpiAwarenessContext(); + User32.DpiAwarenessContext dpiAwareness = User32.GetCurrentThreadDpiAwarenessContext(); - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) + if (User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)) { return HighDpiMode.SystemAware; } - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE)) + if (User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE)) { return HighDpiMode.DpiUnaware; } - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) + if (User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) { return HighDpiMode.PerMonitorV2; } - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) + if (User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) { return HighDpiMode.PerMonitor; } - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(dpiAwareness, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED)) + if (User32.DpiAwarenessContextsEquals(dpiAwareness, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED)) { return HighDpiMode.DpiUnawareGdiScaled; } @@ -459,14 +460,4 @@ internal static bool SetWinformsApplicationDpiAwareness(HighDpiMode highDpiMode) return false; } } - - internal enum DpiAwarenessContext - { - DPI_AWARENESS_CONTEXT_UNSPECIFIED = 0, - DPI_AWARENESS_CONTEXT_UNAWARE = -1, - DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = -2, - DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = -3, - DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4, - DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = -5 - } } diff --git a/src/Common/src/Interop/Kernel32/Interop.CloseHandle.cs b/src/Common/src/Interop/Kernel32/Interop.CloseHandle.cs new file mode 100644 index 00000000000..a389f29f884 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.CloseHandle.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern bool CloseHandle(IntPtr handle); + + public static bool CloseHandle(HandleRef handle) + { + bool result = CloseHandle(handle.Handle); + GC.KeepAlive(handle.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcess.cs b/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcess.cs new file mode 100644 index 00000000000..6d6edbd8093 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcess.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern IntPtr GetCurrentProcess(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcessId.cs b/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcessId.cs new file mode 100644 index 00000000000..7ba7fec7b86 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetCurrentProcessId.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern int GetCurrentProcessId(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetCurrentThread.cs b/src/Common/src/Interop/Kernel32/Interop.GetCurrentThread.cs new file mode 100644 index 00000000000..449339c4f3c --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetCurrentThread.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern IntPtr GetCurrentThread(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetCurrentThreadId.cs b/src/Common/src/Interop/Kernel32/Interop.GetCurrentThreadId.cs new file mode 100644 index 00000000000..67dc9c4bf32 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetCurrentThreadId.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern int GetCurrentThreadId(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetExitCodeThread.cs b/src/Common/src/Interop/Kernel32/Interop.GetExitCodeThread.cs new file mode 100644 index 00000000000..74a3c90f558 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetExitCodeThread.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern bool GetExitCodeThread(IntPtr hWnd, out uint lpdwExitCode); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetThreadLocale.cs b/src/Common/src/Interop/Kernel32/Interop.GetThreadLocale.cs new file mode 100644 index 00000000000..34f896441ea --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetThreadLocale.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern int GetThreadLocale(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.GetTickCount.cs b/src/Common/src/Interop/Kernel32/Interop.GetTickCount.cs new file mode 100644 index 00000000000..f5800f236fc --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.GetTickCount.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern int GetTickCount(); + } +} diff --git a/src/Common/src/Interop/Kernel32/Interop.SetThreadLocale.cs b/src/Common/src/Interop/Kernel32/Interop.SetThreadLocale.cs new file mode 100644 index 00000000000..f95e1e20ff0 --- /dev/null +++ b/src/Common/src/Interop/Kernel32/Interop.SetThreadLocale.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public extern static bool SetThreadLocale(int Locale); + } +} diff --git a/src/Common/src/Interop/Shell32/Interop.SHBrowseForFolder.cs b/src/Common/src/Interop/Shell32/Interop.SHBrowseForFolder.cs index 6da50a021fb..bf5302b144d 100644 --- a/src/Common/src/Interop/Shell32/Interop.SHBrowseForFolder.cs +++ b/src/Common/src/Interop/Shell32/Interop.SHBrowseForFolder.cs @@ -8,7 +8,7 @@ internal static partial class Interop { - public static partial class Shell32 + internal static partial class Shell32 { [DllImport(Libraries.Shell32, ExactSpelling = true)] public static extern CoTaskMemSafeHandle SHBrowseForFolderW(ref BROWSEINFO lpbi); diff --git a/src/Common/src/Interop/Shell32/Interop.SHGetKnownFolderPath.cs b/src/Common/src/Interop/Shell32/Interop.SHGetKnownFolderPath.cs index 92acf23b86f..b25cf59e857 100644 --- a/src/Common/src/Interop/Shell32/Interop.SHGetKnownFolderPath.cs +++ b/src/Common/src/Interop/Shell32/Interop.SHGetKnownFolderPath.cs @@ -7,7 +7,7 @@ internal static partial class Interop { - public static partial class Shell32 + internal static partial class Shell32 { [DllImport(Libraries.Shell32, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)] public static extern int SHGetKnownFolderPath(ref Guid rfid, uint dwFlags, IntPtr hToken, out string pszPath); diff --git a/src/Common/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs b/src/Common/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs index 5ee5a208c14..fe91cb1d1aa 100644 --- a/src/Common/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs +++ b/src/Common/src/Interop/Shell32/Interop.SHGetPathFromIDListLongPath.cs @@ -7,15 +7,15 @@ internal static partial class Interop { - public static partial class Shell32 + internal static partial class Shell32 { - [DllImport(ExternDll.Shell32, EntryPoint = "SHGetPathFromIDListEx", ExactSpelling = true)] + [DllImport(Libraries.Shell32, ExactSpelling = true)] private static extern bool SHGetPathFromIDListEx(IntPtr pidl, IntPtr pszPath, int cchPath, int flags); public static bool SHGetPathFromIDListLongPath(IntPtr pidl, out string path) { - IntPtr pszPath = Marshal.AllocHGlobal((Interop.Kernel32.MAX_PATH + 1) * sizeof(char)); - int length = Interop.Kernel32.MAX_PATH; + IntPtr pszPath = Marshal.AllocHGlobal((Kernel32.MAX_PATH + 1) * sizeof(char)); + int length = Kernel32.MAX_PATH; try { if (!SHGetPathFromIDListLongPath(pidl, ref pszPath, length)) diff --git a/src/Common/src/Interop/Shell32/Interop.SHGetSpecialFolderLocation.cs b/src/Common/src/Interop/Shell32/Interop.SHGetSpecialFolderLocation.cs index 5cd8a0f6d4f..1ea0e06d5cd 100644 --- a/src/Common/src/Interop/Shell32/Interop.SHGetSpecialFolderLocation.cs +++ b/src/Common/src/Interop/Shell32/Interop.SHGetSpecialFolderLocation.cs @@ -8,7 +8,7 @@ internal static partial class Interop { - public static partial class Shell32 + internal static partial class Shell32 { [DllImport(Libraries.Shell32, ExactSpelling = true)] public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, out CoTaskMemSafeHandle ppidl); diff --git a/src/Common/src/Interop/Shell32/Interop.ShellExecute.cs b/src/Common/src/Interop/Shell32/Interop.ShellExecute.cs new file mode 100644 index 00000000000..628e77133bb --- /dev/null +++ b/src/Common/src/Interop/Shell32/Interop.ShellExecute.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Shell32 + { + [DllImport(Libraries.Shell32, ExactSpelling = true, CharSet = CharSet.Ansi, BestFitMapping = false)] + public static extern IntPtr ShellExecuteA(HandleRef hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, User32.ShowWindowCommand nShowCmd); + } +} diff --git a/src/Common/src/Interop/User32/Interop.AreDpiAwarenessContextsEqual.cs b/src/Common/src/Interop/User32/Interop.AreDpiAwarenessContextsEqual.cs new file mode 100644 index 00000000000..d2dba310b2e --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.AreDpiAwarenessContextsEqual.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + private static extern bool AreDpiAwarenessContextsEqual(DpiAwarenessContext dpiContextA, DpiAwarenessContext dpiContextB); + + /// + /// Tries to compare two DPIawareness context values. Return true if they were equal. + /// Return false when they are not equal or underlying OS does not support this API. + /// + /// true/false + public static bool DpiAwarenessContextsEquals(DpiAwarenessContext dpiContextA, DpiAwarenessContext dpiContextB) + { + if (OsVersion.IsWindows10_1607OrGreater) + { + return AreDpiAwarenessContextsEqual(dpiContextA, dpiContextB); + } + + return false; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.CallNextHookEx.cs b/src/Common/src/Interop/User32/Interop.CallNextHookEx.cs new file mode 100644 index 00000000000..a47f4198e18 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.CallNextHookEx.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam); + + public static IntPtr CallNextHookEx(HandleRef hhook, int code, IntPtr wparam, IntPtr lparam) + { + IntPtr result = CallNextHookEx(hhook.Handle, code, wparam, lparam); + GC.KeepAlive(hhook.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.CallWindowProc.cs b/src/Common/src/Interop/User32/Interop.CallWindowProc.cs new file mode 100644 index 00000000000..b3ba81036fb --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.CallWindowProc.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr CallWindowProcW(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + } +} diff --git a/src/Common/src/Interop/User32/Interop.ClassLong.cs b/src/Common/src/Interop/User32/Interop.ClassLong.cs new file mode 100644 index 00000000000..daeb32d3075 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.ClassLong.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum ClassLong : int + { + GCL_WNDPROC = -24, + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.ClassStyle.cs b/src/Common/src/Interop/User32/Interop.ClassStyle.cs new file mode 100644 index 00000000000..ca831cb952e --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.ClassStyle.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum ClassStyle : uint + { + CS_DBLCLKS = 0x0008, + CS_DROPSHADOW = 0x00020000, + CS_SAVEBITS = 0x0800 + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.CreateWindowEx.cs b/src/Common/src/Interop/User32/Interop.CreateWindowEx.cs new file mode 100644 index 00000000000..c87cb2fa6db --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.CreateWindowEx.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] + public static extern IntPtr CreateWindowExW( + int dwExStyle, + string lpClassName, + string lpWindowName, + int dwStyle, + int X, + int Y, + int nWidth, + int nHeight, + HandleRef hWndParent, + IntPtr hMenu, + IntPtr hInst, + [MarshalAs(UnmanagedType.AsAny)] object lpParam); + } +} diff --git a/src/Common/src/Interop/User32/Interop.DefFrameProc.cs b/src/Common/src/Interop/User32/Interop.DefFrameProc.cs new file mode 100644 index 00000000000..cab0c9c6ad7 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.DefFrameProc.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr DefFrameProcW(IntPtr hWnd, IntPtr hWndClient, int msg, IntPtr wParam, IntPtr lParam); + } +} diff --git a/src/Common/src/Interop/User32/Interop.DefMDIChildProc.cs b/src/Common/src/Interop/User32/Interop.DefMDIChildProc.cs new file mode 100644 index 00000000000..dd60813bf33 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.DefMDIChildProc.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr DefMDIChildProcW(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + } +} diff --git a/src/Common/src/Interop/User32/Interop.DefWindowProc.cs b/src/Common/src/Interop/User32/Interop.DefWindowProc.cs new file mode 100644 index 00000000000..0b6858171fc --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.DefWindowProc.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr DefWindowProcW(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + } +} diff --git a/src/Common/src/Interop/User32/Interop.DestroyWindow.cs b/src/Common/src/Interop/User32/Interop.DestroyWindow.cs new file mode 100644 index 00000000000..9feb37e8174 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.DestroyWindow.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool DestroyWindow(IntPtr hWnd); + + public static bool DestroyWindow(HandleRef hWnd) + { + bool result = DestroyWindow(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.DpiAwarenessContext.cs b/src/Common/src/Interop/User32/Interop.DpiAwarenessContext.cs new file mode 100644 index 00000000000..eedfbcfb8d9 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.DpiAwarenessContext.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum DpiAwarenessContext + { + DPI_AWARENESS_CONTEXT_UNSPECIFIED = 0, + DPI_AWARENESS_CONTEXT_UNAWARE = -1, + DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = -2, + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = -3, + DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4, + DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = -5 + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.EnableWindow.cs b/src/Common/src/Interop/User32/Interop.EnableWindow.cs new file mode 100644 index 00000000000..5a79e3a1a39 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.EnableWindow.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool EnableWindow(IntPtr hWnd, bool enable); + + public static bool EnableWindow(HandleRef hWnd, bool enable) + { + bool result = EnableWindow(hWnd.Handle, enable); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.EnumThreadWindows.cs b/src/Common/src/Interop/User32/Interop.EnumThreadWindows.cs new file mode 100644 index 00000000000..63b0e54c3ae --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.EnumThreadWindows.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); + + [DllImport(Libraries.User32, ExactSpelling = true)] + public extern static bool EnumThreadWindows(int dwThreadId, EnumThreadWindowsCallback lpfn, IntPtr lParam); + + public static bool EnumThreadWindows(int dwThreadId, EnumThreadWindowsCallback lpfn, HandleRef lParam) + { + bool result = EnumThreadWindows(dwThreadId, lpfn, lParam.Handle); + GC.KeepAlive(lParam.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.EnumWindows.cs b/src/Common/src/Interop/User32/Interop.EnumWindows.cs new file mode 100644 index 00000000000..17953e3eb06 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.EnumWindows.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public delegate bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam); + + [DllImport(Libraries.User32, ExactSpelling = true, SetLastError = true)] + public static extern bool EnumWindows(EnumWindowsCallback callback, IntPtr extraData); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetActiveWindow.cs b/src/Common/src/Interop/User32/Interop.GetActiveWindow.cs new file mode 100644 index 00000000000..df520240daa --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetActiveWindow.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetActiveWindow(); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetAncestor.cs b/src/Common/src/Interop/User32/Interop.GetAncestor.cs new file mode 100644 index 00000000000..564d5a61953 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetAncestor.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum GetAncestorFlag : uint + { + GA_PARENT = 1, + GA_ROOT = 2 + } + + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetAncestor(IntPtr hWnd, GetAncestorFlag flags); + + public static IntPtr GetAncestor(HandleRef hWnd, GetAncestorFlag flags) + { + IntPtr result = GetAncestor(hWnd.Handle, flags); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetCapture.cs b/src/Common/src/Interop/User32/Interop.GetCapture.cs new file mode 100644 index 00000000000..e4918329589 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetCapture.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetCapture(); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetClassInfo.cs b/src/Common/src/Interop/User32/Interop.GetClassInfo.cs new file mode 100644 index 00000000000..78207db24c0 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetClassInfo.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode)] + public static extern bool GetClassInfoW(IntPtr hInstance, string lpClassName, ref WNDCLASS lpWndClass); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetClassName.cs b/src/Common/src/Interop/User32/Interop.GetClassName.cs new file mode 100644 index 00000000000..bae1181624e --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetClassName.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode)] + private static unsafe extern int GetClassNameW(IntPtr hWnd, char*lpClassName, int nMaxCount); + + private const int MaxClassNameLength = 256; + + public static unsafe string GetClassName(IntPtr hWnd) + { + char* className = stackalloc char[MaxClassNameLength]; + int count = GetClassNameW(hWnd, className, MaxClassNameLength); + return new string(className, 0, count); + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetDpiAwarenessContextForWindow.cs b/src/Common/src/Interop/User32/Interop.GetDpiAwarenessContextForWindow.cs new file mode 100644 index 00000000000..2e61c5a9f4a --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetDpiAwarenessContextForWindow.cs @@ -0,0 +1,60 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + private static extern IntPtr GetWindowDpiAwarenessContext(IntPtr hWnd); + +#pragma warning disable CA1712 // Do not prefix enum values with type name + private enum DPI_AWARENESS + { + DPI_AWARENESS_INVALID = -1, + DPI_AWARENESS_UNAWARE = 0, + DPI_AWARENESS_SYSTEM_AWARE = 1, + DPI_AWARENESS_PER_MONITOR_AWARE = 2 + } +#pragma warning restore CA1712 // Do not prefix enum values with type name + + [DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)] + private static extern DPI_AWARENESS GetAwarenessFromDpiAwarenessContext(IntPtr dpiAwarenessContext); + + public static DpiAwarenessContext GetDpiAwarenessContextForWindow(IntPtr hWnd) + { + if (OsVersion.IsWindows10_1607OrGreater) + { + // Works only >= Windows 10/1607 + IntPtr awarenessContext = GetWindowDpiAwarenessContext(hWnd); + DPI_AWARENESS awareness = GetAwarenessFromDpiAwarenessContext(awarenessContext); + return ConvertToDpiAwarenessContext(awareness); + } + + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; + } + + private static DpiAwarenessContext ConvertToDpiAwarenessContext(DPI_AWARENESS dpiAwareness) + { + switch (dpiAwareness) + { + case DPI_AWARENESS.DPI_AWARENESS_UNAWARE: + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE; + + case DPI_AWARENESS.DPI_AWARENESS_SYSTEM_AWARE: + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE; + + case DPI_AWARENESS.DPI_AWARENESS_PER_MONITOR_AWARE: + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2; + + default: + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE; + } + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetFocus.cs b/src/Common/src/Interop/User32/Interop.GetFocus.cs new file mode 100644 index 00000000000..d7fea9a238f --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetFocus.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetFocus(); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetForegroundWindow.cs b/src/Common/src/Interop/User32/Interop.GetForegroundWindow.cs new file mode 100644 index 00000000000..439e732a404 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetForegroundWindow.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetForegroundWindow(); + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetParent.cs b/src/Common/src/Interop/User32/Interop.GetParent.cs new file mode 100644 index 00000000000..345ef9b7887 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetParent.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetParent(IntPtr hWnd); + + public static IntPtr GetParent(HandleRef hWnd) + { + IntPtr result = GetParent(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetThreadDpiAwarenessContext.cs b/src/Common/src/Interop/User32/Interop.GetThreadDpiAwarenessContext.cs new file mode 100644 index 00000000000..fa8d76241d7 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetThreadDpiAwarenessContext.cs @@ -0,0 +1,34 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +internal static partial class Interop +{ + internal static partial class User32 + { + /// + /// These APIs are available starting Windows 10, version 1607 only. + /// + [DllImport(ExternDll.User32, ExactSpelling = true)] + private static extern DpiAwarenessContext GetThreadDpiAwarenessContext(); + + /// + /// Tries to get thread dpi awareness context + /// + /// returns thread dpi awareness context if API is available in this version of OS. otherwise, return IntPtr.Zero. + public static DpiAwarenessContext GetCurrentThreadDpiAwarenessContext() + { + if (OsVersion.IsWindows10_1607OrGreater) + { + return GetThreadDpiAwarenessContext(); + } + + // legacy OS that does not have this API available. + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetWindow.cs b/src/Common/src/Interop/User32/Interop.GetWindow.cs new file mode 100644 index 00000000000..ff7d69df078 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetWindow.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum GetWindowOption : uint + { + GW_HWNDNEXT = 2, + GW_HWNDPREV = 3, + GW_CHILD = 5, + } + + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowOption uCmd); + + public static IntPtr GetWindow(HandleRef hWnd, GetWindowOption uCmd) + { + IntPtr result = GetWindow(hWnd.Handle, uCmd); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetWindowLong.cs b/src/Common/src/Interop/User32/Interop.GetWindowLong.cs new file mode 100644 index 00000000000..8e94474c574 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetWindowLong.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "GetWindowLongW")] + private static extern IntPtr GetWindowLong32(IntPtr hWnd, WindowLong nIndex); + + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "GetWindowLongPtrW")] + private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, WindowLong nIndex); + + /// + /// Retrieves information about the specified window. The function also retrieves the value at a + /// specified offset into the extra window memory. + /// + /// + /// GetWindowLong won't work correctly for 64-bit: we should use GetWindowLongPtr instead. On + /// 32-bit, GetWindowLongPtr is just #defined as GetWindowLong. GetWindowLong really should + /// take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit + /// it'll be OK. + /// + public static IntPtr GetWindowLong(IntPtr hWnd, WindowLong nIndex) + { + if (IntPtr.Size == 4) + { + return GetWindowLong32(hWnd, nIndex); + } + + return GetWindowLongPtr64(hWnd, nIndex); + } + + public static IntPtr GetWindowLong(HandleRef hWnd, WindowLong nIndex) + { + IntPtr result = GetWindowLong(hWnd.Handle, nIndex); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetWindowPlacement.cs b/src/Common/src/Interop/User32/Interop.GetWindowPlacement.cs new file mode 100644 index 00000000000..951e8f3976d --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetWindowPlacement.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "GetWindowPlacement")] + private static extern int GetWindowPlacementInternal(IntPtr hWnd, ref WINDOWPLACEMENT placement); + + public static int GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT placement) + { + placement = new WINDOWPLACEMENT + { + length = Marshal.SizeOf() + }; + return GetWindowPlacementInternal(hWnd, ref placement); + } + + public static int GetWindowPlacement(HandleRef hWnd, out WINDOWPLACEMENT placement) + { + int result = GetWindowPlacement(hWnd.Handle, out placement); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetWindowRect.cs b/src/Common/src/Interop/User32/Interop.GetWindowRect.cs new file mode 100644 index 00000000000..0124d77c1fb --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetWindowRect.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool GetWindowRect(IntPtr hWnd, ref Interop.RECT rect); + + public static bool GetWindowRect(HandleRef hWnd, ref Interop.RECT rect) + { + bool result = GetWindowRect(hWnd.Handle, ref rect); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.GetWindowThreadProcessId.cs b/src/Common/src/Interop/User32/Interop.GetWindowThreadProcessId.cs new file mode 100644 index 00000000000..ca482849819 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.GetWindowThreadProcessId.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); + + public static int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId) + { + int result = GetWindowThreadProcessId(hWnd.Handle, out lpdwProcessId); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsChild.cs b/src/Common/src/Interop/User32/Interop.IsChild.cs new file mode 100644 index 00000000000..947235e1583 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsChild.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public extern static bool IsChild(IntPtr parent, IntPtr child); + + public static bool IsChild(IntPtr parent, HandleRef child) + { + bool result = IsChild(parent, child.Handle); + GC.KeepAlive(child.Handle); + return result; + } + + public static bool IsChild(HandleRef parent, IntPtr child) + { + bool result = IsChild(parent.Handle, child); + GC.KeepAlive(parent.Handle); + return result; + } + + public static bool IsChild(HandleRef parent, HandleRef child) + { + bool result = IsChild(parent.Handle, child.Handle); + GC.KeepAlive(parent.Handle); + GC.KeepAlive(child.Handle); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsWindow.cs b/src/Common/src/Interop/User32/Interop.IsWindow.cs new file mode 100644 index 00000000000..00f870025c0 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsWindow.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Auto)] + public static extern bool IsWindow(IntPtr hWnd); + + public static bool IsWindow(HandleRef hWnd) + { + bool result = IsWindow(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsWindowEnabled.cs b/src/Common/src/Interop/User32/Interop.IsWindowEnabled.cs new file mode 100644 index 00000000000..43690dc2dea --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsWindowEnabled.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool IsWindowEnabled(IntPtr hWnd); + + public static bool IsWindowEnabled(HandleRef hWnd) + { + bool result = IsWindowEnabled(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsWindowUnicode.cs b/src/Common/src/Interop/User32/Interop.IsWindowUnicode.cs new file mode 100644 index 00000000000..0094acd7720 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsWindowUnicode.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool IsWindowUnicode(IntPtr hWnd); + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsWindowVisible.cs b/src/Common/src/Interop/User32/Interop.IsWindowVisible.cs new file mode 100644 index 00000000000..b13d45798e0 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsWindowVisible.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool IsWindowVisible(IntPtr hWnd); + + public static bool IsWindowVisible(HandleRef hWnd) + { + bool result = IsWindowVisible(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.IsZoomed.cs b/src/Common/src/Interop/User32/Interop.IsZoomed.cs new file mode 100644 index 00000000000..781e12d8bec --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.IsZoomed.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool IsZoomed(HandleRef hWnd); + } +} diff --git a/src/Common/src/Interop/User32/Interop.RegisterClass.cs b/src/Common/src/Interop/User32/Interop.RegisterClass.cs new file mode 100644 index 00000000000..16234b26b7a --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.RegisterClass.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] + public static extern ushort RegisterClassW(ref WNDCLASS lpWndClass); + } +} diff --git a/src/Common/src/Interop/User32/Interop.RegisterWindowMessage.cs b/src/Common/src/Interop/User32/Interop.RegisterWindowMessage.cs new file mode 100644 index 00000000000..32aba5549f0 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.RegisterWindowMessage.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode)] + public static extern int RegisterWindowMessageW(string msg); + } +} diff --git a/src/Common/src/Interop/User32/Interop.ReleaseCapture.cs b/src/Common/src/Interop/User32/Interop.ReleaseCapture.cs new file mode 100644 index 00000000000..a4c1300da3a --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.ReleaseCapture.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool ReleaseCapture(); + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetActiveWindow.cs b/src/Common/src/Interop/User32/Interop.SetActiveWindow.cs new file mode 100644 index 00000000000..61ac32cb19a --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetActiveWindow.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr SetActiveWindow(IntPtr hWnd); + + public static IntPtr SetActiveWindow(HandleRef hWnd) + { + IntPtr result = SetActiveWindow(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetCapture.cs b/src/Common/src/Interop/User32/Interop.SetCapture.cs new file mode 100644 index 00000000000..3c8a6116a50 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetCapture.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr SetCapture(HandleRef hwnd); + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetClassLong.cs b/src/Common/src/Interop/User32/Interop.SetClassLong.cs new file mode 100644 index 00000000000..3b2a5383194 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetClassLong.cs @@ -0,0 +1,34 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetClassLongW")] + private static extern IntPtr SetClassLongPtr32(HandleRef hwnd, ClassLong nIndex, IntPtr dwNewLong); + + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetClassLongPtrW")] + private static extern IntPtr SetClassLongPtr64(HandleRef hwnd, ClassLong nIndex, IntPtr dwNewLong); + + /// + /// SetClassLong won't work correctly for 64-bit: we should use SetClassLongPtr instead. On + /// 32-bit, SetClassLongPtr is just #defined as SetClassLong. SetClassLong really should + /// take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit + /// it'll be OK. + /// + public static IntPtr SetClassLong(HandleRef hWnd, ClassLong nIndex, IntPtr dwNewLong) + { + if (IntPtr.Size == 4) + { + return SetClassLongPtr32(hWnd, nIndex, dwNewLong); + } + + return SetClassLongPtr64(hWnd, nIndex, dwNewLong); + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetFocus.cs b/src/Common/src/Interop/User32/Interop.SetFocus.cs new file mode 100644 index 00000000000..f313831036d --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetFocus.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr SetFocus(IntPtr hWnd); + + public static IntPtr SetFocus(HandleRef hWnd) + { + IntPtr result = SetFocus(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetParent.cs b/src/Common/src/Interop/User32/Interop.SetParent.cs new file mode 100644 index 00000000000..57ae23e8ece --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetParent.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent); + + public static IntPtr SetParent(IntPtr hWnd, HandleRef hWndParent) + { + IntPtr result = SetParent(hWnd, hWndParent.Handle); + GC.KeepAlive(hWndParent.Handle); + return result; + } + + public static IntPtr SetParent(HandleRef hWnd, IntPtr hWndParent) + { + IntPtr result = SetParent(hWnd.Handle, hWndParent); + GC.KeepAlive(hWnd.Handle); + return result; + } + + public static IntPtr SetParent(HandleRef hWnd, HandleRef hWndParent) + { + IntPtr result = SetParent(hWnd.Handle, hWndParent.Handle); + GC.KeepAlive(hWnd.Handle); + GC.KeepAlive(hWndParent.Handle); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetThreadDpiAwarenessContext.cs b/src/Common/src/Interop/User32/Interop.SetThreadDpiAwarenessContext.cs new file mode 100644 index 00000000000..5048657fec9 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetThreadDpiAwarenessContext.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + private static extern DpiAwarenessContext SetThreadDpiAwarenessContext(DpiAwarenessContext dpiContext); + + /// + /// Tries to set thread dpi awareness context + /// + /// returns old thread dpi awareness context if API is available in this version of OS. otherwise, return IntPtr.Zero. + public static DpiAwarenessContext SetCurrentThreadDpiAwarenessContext(DpiAwarenessContext dpiContext) + { + if (OsVersion.IsWindows10_1607OrGreater) + { + if (dpiContext == DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) + { + throw new ArgumentException(nameof(dpiContext), dpiContext.ToString()); + } + + return SetThreadDpiAwarenessContext(dpiContext); + } + + // legacy OS that does not have this API available. + return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetWindowLong.cs b/src/Common/src/Interop/User32/Interop.SetWindowLong.cs new file mode 100644 index 00000000000..ae8e5d61a90 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetWindowLong.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetWindowLongW")] + private static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, WindowLong nIndex, IntPtr dwNewLong); + + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetWindowLongPtrW")] + private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, WindowLong nIndex, IntPtr dwNewLong); + + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetWindowLongW")] + private static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, WindowLong nIndex, WndProc wndproc); + + [DllImport(Libraries.User32, ExactSpelling = true, EntryPoint = "SetWindowLongPtrW")] + private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, WindowLong nIndex, WndProc wndproc); + + /// + /// SetWindowLong won't work correctly for 64-bit: we should use SetWindowLongPtr instead. On + /// 32-bit, SetWindowLongPtr is just #defined as SetWindowLong. SetWindowLong really should + /// take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit + /// it'll be OK. + /// + public static IntPtr SetWindowLong(IntPtr hWnd, WindowLong nIndex, IntPtr dwNewLong) + { + if (IntPtr.Size == 4) + { + return SetWindowLongPtr32(hWnd, nIndex, dwNewLong); + } + + return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); + } + + public static IntPtr SetWindowLong(IntPtr hWnd, WindowLong nIndex, HandleRef dwNewLong) + { + IntPtr result = SetWindowLong(hWnd, nIndex, dwNewLong.Handle); + GC.KeepAlive(dwNewLong.Wrapper); + return result; + } + + public static IntPtr SetWindowLong(HandleRef hWnd, WindowLong nIndex, IntPtr dwNewLong) + { + IntPtr result = SetWindowLong(hWnd.Handle, nIndex, dwNewLong); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + + public static IntPtr SetWindowLong(HandleRef hWnd, WindowLong nIndex, HandleRef dwNewLong) + { + IntPtr result = SetWindowLong(hWnd.Handle, nIndex, dwNewLong.Handle); + GC.KeepAlive(hWnd.Wrapper); + GC.KeepAlive(dwNewLong.Wrapper); + return result; + } + + public static IntPtr SetWindowLong(HandleRef hWnd, WindowLong nIndex, WndProc wndproc) + { + if (IntPtr.Size == 4) + { + return SetWindowLongPtr32(hWnd, nIndex, wndproc); + } + + return SetWindowLongPtr64(hWnd, nIndex, wndproc); + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetWindowPlacement.cs b/src/Common/src/Interop/User32/Interop.SetWindowPlacement.cs new file mode 100644 index 00000000000..44255b3af2b --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetWindowPlacement.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool SetWindowPlacement(HandleRef hWnd, ref WINDOWPLACEMENT placement); + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetWindowPos.cs b/src/Common/src/Interop/User32/Interop.SetWindowPos.cs new file mode 100644 index 00000000000..85cb8738a21 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetWindowPos.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public static IntPtr HWND_TOP = (IntPtr)0; + public static IntPtr HWND_BOTTOM = (IntPtr)1; + public static IntPtr HWND_TOPMOST = new IntPtr(-1); + public static IntPtr HWND_NOTOPMOST = new IntPtr(-2); + public static IntPtr HWND_MESSAGE = new IntPtr(-3); + + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool SetWindowPos( + IntPtr hWnd, + IntPtr hWndInsertAfter, + int x = 0, + int y = 0, + int cx = 0, + int cy = 0, + WindowPosition flags = (WindowPosition)0); + + public static bool SetWindowPos( + HandleRef hWnd, + IntPtr hWndInsertAfter, + int x = 0, + int y = 0, + int cx = 0, + int cy = 0, + WindowPosition flags = (WindowPosition)0) + { + bool result = SetWindowPos(hWnd.Handle, hWndInsertAfter, x, y, cx, cy, flags); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + + public static bool SetWindowPos( + HandleRef hWnd, + HandleRef hWndInsertAfter, + int x = 0, + int y = 0, + int cx = 0, + int cy = 0, + WindowPosition flags = (WindowPosition)0) + { + bool result = SetWindowPos(hWnd.Handle, hWndInsertAfter.Handle, x, y, cx, cy, flags); + GC.KeepAlive(hWnd.Wrapper); + GC.KeepAlive(hWndInsertAfter.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetWindowRgn.cs b/src/Common/src/Interop/User32/Interop.SetWindowRgn.cs new file mode 100644 index 00000000000..32009ec502e --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetWindowRgn.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern int SetWindowRgn(IntPtr hwnd, IntPtr hrgn, bool fRedraw); + + public static int SetWindowRgn(HandleRef hwnd, IntPtr hrgn, bool fRedraw) + { + int result = SetWindowRgn(hwnd.Handle, hrgn, fRedraw); + GC.KeepAlive(hwnd.Wrapper); + return result; + } + + public static int SetWindowRgn(HandleRef hwnd, HandleRef hrgn, bool fRedraw) + { + int result = SetWindowRgn(hwnd.Handle, hrgn.Handle, fRedraw); + GC.KeepAlive(hwnd.Wrapper); + GC.KeepAlive(hrgn.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.SetWindowsHookEx.cs b/src/Common/src/Interop/User32/Interop.SetWindowsHookEx.cs new file mode 100644 index 00000000000..c36ff79851c --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.SetWindowsHookEx.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum WindowHookProcedure : int + { + WH_JOURNALPLAYBACK = 1, + WH_GETMESSAGE = 3, + WH_MOUSE = 7, + } + + public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode)] + public static extern IntPtr SetWindowsHookExW(WindowHookProcedure idHook, HookProc lpfn, IntPtr hmod, int dwThreadId); + } +} diff --git a/src/Common/src/Interop/User32/Interop.ShowWindow.cs b/src/Common/src/Interop/User32/Interop.ShowWindow.cs new file mode 100644 index 00000000000..1a5e863b433 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.ShowWindow.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow); + + public static bool ShowWindow(HandleRef hWnd, ShowWindowCommand nCmdShow) + { + bool result = ShowWindow(hWnd.Handle, nCmdShow); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.ShowWindowCommand.cs b/src/Common/src/Interop/User32/Interop.ShowWindowCommand.cs new file mode 100644 index 00000000000..feebe650784 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.ShowWindowCommand.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum ShowWindowCommand : int + { + SW_HIDE = 0, + SW_NORMAL = 1, + SW_SHOWMINIMIZED = 2, + SW_SHOWMAXIMIZED = 3, + SW_MAXIMIZE = 3, + SW_SHOWNOACTIVATE = 4, + SW_SHOW = 5, + SW_MINIMIZE = 6, + SW_SHOWMINNOACTIVE = 7, + SW_SHOWNA = 8, + SW_RESTORE = 9, + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.UnhookWindowsHookEx.cs b/src/Common/src/Interop/User32/Interop.UnhookWindowsHookEx.cs new file mode 100644 index 00000000000..c399000b0ec --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.UnhookWindowsHookEx.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [DllImport(Libraries.User32, ExactSpelling = true)] + public static extern bool UnhookWindowsHookEx(IntPtr hhook); + + public static bool UnhookWindowsHookEx(HandleRef hhook) + { + bool result = UnhookWindowsHookEx(hhook.Handle); + GC.KeepAlive(hhook.Wrapper); + return result; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WINDOWPLACEMENT.cs b/src/Common/src/Interop/User32/Interop.WINDOWPLACEMENT.cs new file mode 100644 index 00000000000..787c7e42542 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WINDOWPLACEMENT.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Drawing; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [StructLayout(LayoutKind.Sequential)] + public struct WINDOWPLACEMENT + { + public int length; + public int flags; + public ShowWindowCommand showCmd; + public Point ptMinPosition; + public Point ptMaxPosition; + // rcNormalPosition was a by-value RECT structure + public int rcNormalPosition_left; + public int rcNormalPosition_top; + public int rcNormalPosition_right; + public int rcNormalPosition_bottom; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WNDCLASS.cs b/src/Common/src/Interop/User32/Interop.WNDCLASS.cs new file mode 100644 index 00000000000..41d8278d5ee --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WNDCLASS.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public unsafe struct WNDCLASS + { + public ClassStyle style; + public IntPtr lpfnWndProc; + public int cbClsExtra; + public int cbWndExtra; + public IntPtr hInstance; + public IntPtr hIcon; + public IntPtr hCursor; + public IntPtr hbrBackground; + public char* lpszMenuName; + public char* lpszClassName; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WindowLong.cs b/src/Common/src/Interop/User32/Interop.WindowLong.cs new file mode 100644 index 00000000000..8883f30bbbe --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WindowLong.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum WindowLong : int + { + GWL_WNDPROC = -4, + GWL_HWNDPARENT = -8, + GWL_ID = -12, + GWL_STYLE = -16, + GWL_EXSTYLE = -20, + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WindowPosition.cs b/src/Common/src/Interop/User32/Interop.WindowPosition.cs new file mode 100644 index 00000000000..eb16c7a95c9 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WindowPosition.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public enum WindowPosition : uint + { + SWP_NOSIZE = 0x0001, + SWP_NOMOVE = 0x0002, + SWP_NOZORDER = 0x0004, + SWP_NOACTIVATE = 0x0010, + SWP_SHOWWINDOW = 0x0040, + SWP_HIDEWINDOW = 0x0080, + SWP_DRAWFRAME = 0x0020, + SWP_NOOWNERZORDER = 0x0200, + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WindowStyle.cs b/src/Common/src/Interop/User32/Interop.WindowStyle.cs new file mode 100644 index 00000000000..66baf3f3bea --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WindowStyle.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + public static class WindowStyle + { + public const int WS_OVERLAPPED = 0x00000000; + public const int WS_POPUP = unchecked((int)0x80000000); + public const int WS_CHILD = 0x40000000; + public const int WS_MINIMIZE = 0x20000000; + public const int WS_VISIBLE = 0x10000000; + public const int WS_DISABLED = 0x08000000; + public const int WS_CLIPSIBLINGS = 0x04000000; + public const int WS_CLIPCHILDREN = 0x02000000; + public const int WS_MAXIMIZE = 0x01000000; + public const int WS_CAPTION = 0x00C00000; + public const int WS_BORDER = 0x00800000; + public const int WS_DLGFRAME = 0x00400000; + public const int WS_VSCROLL = 0x00200000; + public const int WS_HSCROLL = 0x00100000; + public const int WS_SYSMENU = 0x00080000; + public const int WS_THICKFRAME = 0x00040000; + public const int WS_TABSTOP = 0x00010000; + public const int WS_MINIMIZEBOX = 0x00020000; + public const int WS_MAXIMIZEBOX = 0x00010000; + public const int WS_EX_DLGMODALFRAME = 0x00000001; + public const int WS_EX_MDICHILD = 0x00000040; + public const int WS_EX_TOOLWINDOW = 0x00000080; + public const int WS_EX_CLIENTEDGE = 0x00000200; + public const int WS_EX_CONTEXTHELP = 0x00000400; + public const int WS_EX_RIGHT = 0x00001000; + public const int WS_EX_LEFT = 0x00000000; + public const int WS_EX_RTLREADING = 0x00002000; + public const int WS_EX_LEFTSCROLLBAR = 0x00004000; + public const int WS_EX_CONTROLPARENT = 0x00010000; + public const int WS_EX_STATICEDGE = 0x00020000; + public const int WS_EX_APPWINDOW = 0x00040000; + public const int WS_EX_LAYERED = 0x00080000; + public const int WS_EX_TOPMOST = 0x00000008; + public const int WS_EX_LAYOUTRTL = 0x00400000; + public const int WS_EX_NOINHERITLAYOUT = 0x00100000; + public const int WS_EX_TRANSPARENT = 0x00000020; + } + } +} diff --git a/src/Common/src/Interop/User32/Interop.WndProc.cs b/src/Common/src/Interop/User32/Interop.WndProc.cs new file mode 100644 index 00000000000..ebe44b2f418 --- /dev/null +++ b/src/Common/src/Interop/User32/Interop.WndProc.cs @@ -0,0 +1,13 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal static partial class Interop +{ + internal static partial class User32 + { + public delegate IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + } +} diff --git a/src/Common/src/NativeMethods.cs b/src/Common/src/NativeMethods.cs index 666ede8e213..cc1d70d7e4b 100644 --- a/src/Common/src/NativeMethods.cs +++ b/src/Common/src/NativeMethods.cs @@ -149,13 +149,6 @@ public const int public const int cmb4 = 0x0473; - public enum ClassStyle : uint - { - CS_DBLCLKS = 0x0008, - CS_DROPSHADOW = 0x00020000, - CS_SAVEBITS = 0x0800 - } - public const int CF_TEXT = 1, CF_BITMAP = 2, CF_METAFILEPICT = 3, @@ -391,26 +384,13 @@ public enum ClassStyle : uint GMEM_INVALID_HANDLE = 0x8000, GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT), GPTR = (GMEM_FIXED | GMEM_ZEROINIT), - GCL_WNDPROC = (-24), - GWL_WNDPROC = (-4), - GWL_HWNDPARENT = (-8), - GWL_STYLE = (-16), - GWL_EXSTYLE = (-20), - GWL_ID = (-12), - GW_HWNDFIRST = 0, - GW_HWNDLAST = 1, - GW_HWNDNEXT = 2, - GW_HWNDPREV = 3, - GW_CHILD = 5, GMR_VISIBLE = 0, GMR_DAYSTATE = 1, GDI_ERROR = (unchecked((int)0xFFFFFFFF)), GDTR_MIN = 0x0001, GDTR_MAX = 0x0002, GDT_VALID = 0, - GDT_NONE = 1, - GA_PARENT = 1, - GA_ROOT = 2; + GDT_NONE = 1; // ImmGetCompostionString index. public const int @@ -482,12 +462,6 @@ public const int HBMMENU_POPUP_MAXIMIZE = 10, HBMMENU_POPUP_MINIMIZE = 11; - public static HandleRef HWND_TOP = new HandleRef(null, (IntPtr)0); - public static HandleRef HWND_BOTTOM = new HandleRef(null, (IntPtr)1); - public static HandleRef HWND_TOPMOST = new HandleRef(null, new IntPtr(-1)); - public static HandleRef HWND_NOTOPMOST = new HandleRef(null, new IntPtr(-2)); - public static HandleRef HWND_MESSAGE = new HandleRef(null, new IntPtr(-3)); - public const int IME_CMODE_NATIVE = 0x0001, IME_CMODE_KATAKANA = 0x0002, IME_CMODE_FULLSHAPE = 0x0008, @@ -1098,27 +1072,7 @@ public static int MAKELCID(int lgid, int sort) ESB_ENABLE_BOTH = 0x0000, ESB_DISABLE_BOTH = 0x0003, SORT_DEFAULT = 0x0, - SUBLANG_DEFAULT = 0x01, - SW_HIDE = 0, - SW_NORMAL = 1, - SW_SHOWMINIMIZED = 2, - SW_SHOWMAXIMIZED = 3, - SW_MAXIMIZE = 3, - SW_SHOWNOACTIVATE = 4, - SW_SHOW = 5, - SW_MINIMIZE = 6, - SW_SHOWMINNOACTIVE = 7, - SW_SHOWNA = 8, - SW_RESTORE = 9, - SW_MAX = 10, - SWP_NOSIZE = 0x0001, - SWP_NOMOVE = 0x0002, - SWP_NOZORDER = 0x0004, - SWP_NOACTIVATE = 0x0010, - SWP_SHOWWINDOW = 0x0040, - SWP_HIDEWINDOW = 0x0080, - SWP_DRAWFRAME = 0x0020, - SWP_NOOWNERZORDER = 0x0200; + SUBLANG_DEFAULT = 0x01; public const int HLP_FILE = 1, HLP_KEYWORD = 2, @@ -1468,50 +1422,11 @@ public static bool Failed(int hr) VK_INSERT = 0x002D, VK_DELETE = 0x002E; - public const int WH_JOURNALPLAYBACK = 1; - public const int WH_GETMESSAGE = 3; - public const int WH_MOUSE = 7; public const int WSF_VISIBLE = 0x0001; public const int WA_INACTIVE = 0; public const int WA_ACTIVE = 1; public const int WA_CLICKACTIVE = 2; - - public const int WS_OVERLAPPED = 0x00000000; - public const int WS_POPUP = unchecked((int)0x80000000); - public const int WS_CHILD = 0x40000000; - public const int WS_MINIMIZE = 0x20000000; - public const int WS_VISIBLE = 0x10000000; - public const int WS_DISABLED = 0x08000000; - public const int WS_CLIPSIBLINGS = 0x04000000; - public const int WS_CLIPCHILDREN = 0x02000000; - public const int WS_MAXIMIZE = 0x01000000; - public const int WS_CAPTION = 0x00C00000; - public const int WS_BORDER = 0x00800000; - public const int WS_DLGFRAME = 0x00400000; - public const int WS_VSCROLL = 0x00200000; - public const int WS_HSCROLL = 0x00100000; - public const int WS_SYSMENU = 0x00080000; - public const int WS_THICKFRAME = 0x00040000; - public const int WS_TABSTOP = 0x00010000; - public const int WS_MINIMIZEBOX = 0x00020000; - public const int WS_MAXIMIZEBOX = 0x00010000; - public const int WS_EX_DLGMODALFRAME = 0x00000001; - public const int WS_EX_MDICHILD = 0x00000040; - public const int WS_EX_TOOLWINDOW = 0x00000080; - public const int WS_EX_CLIENTEDGE = 0x00000200; - public const int WS_EX_CONTEXTHELP = 0x00000400; - public const int WS_EX_RIGHT = 0x00001000; - public const int WS_EX_LEFT = 0x00000000; - public const int WS_EX_RTLREADING = 0x00002000; - public const int WS_EX_LEFTSCROLLBAR = 0x00004000; - public const int WS_EX_CONTROLPARENT = 0x00010000; - public const int WS_EX_STATICEDGE = 0x00020000; - public const int WS_EX_APPWINDOW = 0x00040000; - public const int WS_EX_LAYERED = 0x00080000; - public const int WS_EX_TOPMOST = 0x00000008; - public const int WS_EX_LAYOUTRTL = 0x00400000; - public const int WS_EX_NOINHERITLAYOUT = 0x00100000; public const int WPF_SETMINPOSITION = 0x0001; public const int WHEEL_DELTA = 120; @@ -1548,7 +1463,7 @@ public static int WM_MOUSEENTER { if (wmMouseEnterMessage == -1) { - wmMouseEnterMessage = SafeNativeMethods.RegisterWindowMessage("WinFormsMouseEnter"); + wmMouseEnterMessage = Interop.User32.RegisterWindowMessageW("WinFormsMouseEnter"); } return wmMouseEnterMessage; } @@ -1561,7 +1476,7 @@ public static int WM_UIUNSUBCLASS { if (wmUnSubclass == -1) { - wmUnSubclass = SafeNativeMethods.RegisterWindowMessage("WinFormsUnSubclass"); + wmUnSubclass = Interop.User32.RegisterWindowMessageW("WinFormsUnSubclass"); } return wmUnSubclass; } @@ -2012,8 +1927,6 @@ public class TRACKMOUSEEVENT public int dwHoverTime = 100; // Never set this to field ZERO, or to HOVER_DEFAULT, ever! } - public delegate IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); - [StructLayout(LayoutKind.Sequential)] public struct MARGINS { @@ -2110,21 +2023,6 @@ public class TPMPARAMS public int rcExclude_bottom; } - [StructLayout(LayoutKind.Sequential)] - public struct WINDOWPLACEMENT - { - public int length; - public int flags; - public int showCmd; - public Point ptMinPosition; - public Point ptMaxPosition; - // rcNormalPosition was a by-value RECT structure - public int rcNormalPosition_left; - public int rcNormalPosition_top; - public int rcNormalPosition_right; - public int rcNormalPosition_bottom; - } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class STARTUPINFO_I { @@ -2172,8 +2070,8 @@ public class PAGESETUPDLG public IntPtr hInstance = IntPtr.Zero; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnPageSetupHook = null; - public WndProc lpfnPagePaintHook = null; + public Interop.User32.WndProc lpfnPageSetupHook = null; + public Interop.User32.WndProc lpfnPagePaintHook = null; public string lpPageSetupTemplateName = null; public IntPtr hPageSetupTemplate = IntPtr.Zero; } @@ -2199,8 +2097,8 @@ public interface PRINTDLG IntPtr hInstance { get; set; } IntPtr lCustData { get; set; } - WndProc lpfnPrintHook { get; set; } - WndProc lpfnSetupHook { get; set; } + Interop.User32.WndProc lpfnPrintHook { get; set; } + Interop.User32.WndProc lpfnSetupHook { get; set; } string lpPrintTemplateName { get; set; } string lpSetupTemplateName { get; set; } @@ -2232,8 +2130,8 @@ public class PRINTDLG_32 : PRINTDLG IntPtr m_hInstance; IntPtr m_lCustData; - WndProc m_lpfnPrintHook; - WndProc m_lpfnSetupHook; + Interop.User32.WndProc m_lpfnPrintHook; + Interop.User32.WndProc m_lpfnSetupHook; string m_lpPrintTemplateName; string m_lpSetupTemplateName; @@ -2259,8 +2157,8 @@ public class PRINTDLG_32 : PRINTDLG public IntPtr hInstance { get { return m_hInstance; } set { m_hInstance = value; } } public IntPtr lCustData { get { return m_lCustData; } set { m_lCustData = value; } } - public WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } - public WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } + public Interop.User32.WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } + public Interop.User32.WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } public string lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } public string lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } @@ -2292,8 +2190,8 @@ public class PRINTDLG_64 : PRINTDLG IntPtr m_hInstance; IntPtr m_lCustData; - WndProc m_lpfnPrintHook; - WndProc m_lpfnSetupHook; + Interop.User32.WndProc m_lpfnPrintHook; + Interop.User32.WndProc m_lpfnSetupHook; string m_lpPrintTemplateName; string m_lpSetupTemplateName; @@ -2319,8 +2217,8 @@ public class PRINTDLG_64 : PRINTDLG public IntPtr hInstance { get { return m_hInstance; } set { m_hInstance = value; } } public IntPtr lCustData { get { return m_lCustData; } set { m_lCustData = value; } } - public WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } - public WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } + public Interop.User32.WndProc lpfnPrintHook { get { return m_lpfnPrintHook; } set { m_lpfnPrintHook = value; } } + public Interop.User32.WndProc lpfnSetupHook { get { return m_lpfnSetupHook; } set { m_lpfnSetupHook = value; } } public string lpPrintTemplateName { get { return m_lpPrintTemplateName; } set { m_lpPrintTemplateName = value; } } public string lpSetupTemplateName { get { return m_lpSetupTemplateName; } set { m_lpSetupTemplateName = value; } } @@ -2357,7 +2255,7 @@ public class PRINTDLGEX [MarshalAs(UnmanagedType.LPStr)] public string lpPrintTemplateName; - public WndProc lpCallback = null; + public Interop.User32.WndProc lpCallback = null; public int nPropertyPages; @@ -2447,12 +2345,10 @@ public class CHOOSECOLOR public IntPtr lpCustColors; public int Flags; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnHook; + public Interop.User32.WndProc lpfnHook; public string lpTemplateName = null; } - public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); - [StructLayout(LayoutKind.Sequential)] public class BITMAP { @@ -2628,8 +2524,6 @@ public MSAAMENUINFO(string text) } } - public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OPENFILENAME_I { @@ -2651,7 +2545,7 @@ public class OPENFILENAME_I public short nFileExtension = 0; public string lpstrDefExt; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnHook; + public Interop.User32.WndProc lpfnHook; public string lpTemplateName = null; public IntPtr pvReserved = IntPtr.Zero; public int dwReserved = 0; @@ -2669,7 +2563,7 @@ public class CHOOSEFONT public int Flags; public int rgbColors; public IntPtr lCustData = IntPtr.Zero; - public WndProc lpfnHook; + public Interop.User32.WndProc lpfnHook; public string lpTemplateName = null; public IntPtr hInstance; public string lpszStyle = null; @@ -3204,21 +3098,6 @@ public enum tagVT VT_TYPEMASK = 4095 } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public unsafe struct WNDCLASS - { - public ClassStyle style; - public IntPtr lpfnWndProc; - public int cbClsExtra; - public int cbWndExtra; - public IntPtr hInstance; - public IntPtr hIcon; - public IntPtr hCursor; - public IntPtr hbrBackground; - public char* lpszMenuName; - public char* lpszClassName; - } - public class MSOCM { // MSO Component registration flags @@ -3541,7 +3420,7 @@ public struct WINDOWPOS public int y; public int cx; public int cy; - public int flags; + public Interop.User32.WindowPosition flags; } [StructLayout(LayoutKind.Sequential)] @@ -4868,8 +4747,6 @@ public enum MONTCALENDAR_VIEW_MODE public const int PS_GEOMETRIC = 0x00010000; public const int PS_ENDCAP_SQUARE = 0x00000100; - public const int WS_EX_TRANSPARENT = 0x00000020; - public const int NULL_BRUSH = 5; public const int MM_HIMETRIC = 3; @@ -5214,10 +5091,5 @@ public UiaRect(Drawing.Rectangle r) [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out Point pt); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent); } } - - diff --git a/src/Common/src/SafeNativeMethods.cs b/src/Common/src/SafeNativeMethods.cs index ce4756513e5..38cad8d5ecc 100644 --- a/src/Common/src/SafeNativeMethods.cs +++ b/src/Common/src/SafeNativeMethods.cs @@ -107,9 +107,6 @@ public static extern bool Rectangle( [DllImport(ExternDll.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)] public static extern bool ChooseColor([In, Out] NativeMethods.CHOOSECOLOR cc); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern int RegisterWindowMessage(string msg); - [DllImport(ExternDll.Oleaut32, EntryPoint = "OleCreateFontIndirect", ExactSpelling = true, PreserveSig = false)] public static extern IFontDisp OleCreateIFontDispIndirect(NativeMethods.FONTDESC fd, ref Guid iid); @@ -327,9 +324,6 @@ public static IntPtr ImageList_Read(IStream pstm) [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetSysColorBrush(int nIndex); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool EnableWindow(HandleRef hWnd, bool enable); - [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern bool GetClientRect(HandleRef hWnd, ref Interop.RECT rect); @@ -385,46 +379,6 @@ public static unsafe bool SetViewportOrgEx(HandleRef hdc, int x, int y, Point *l [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int SetMapMode(HandleRef hDC, int nMapMode); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsWindowEnabled(HandleRef hWnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsWindowVisible(HandleRef hWnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool ReleaseCapture(); - - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetCurrentThreadId(); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, SetLastError = true)] - public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); - - internal delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId); - - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool GetExitCodeThread(HandleRef hWnd, out int lpdwExitCode); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool ShowWindow(HandleRef hWnd, int nCmdShow); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool SetWindowPos( - HandleRef hWnd, - HandleRef hWndInsertAfter, - int x = 0, - int y = 0, - int cx = 0, - int cy = 0, - int flags = 0); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, - int x, int y, int cx, int cy, int flags); - // this is a wrapper that comctl exposes for the NT function since it doesn't exist natively on 95. [DllImport(ExternDll.Comctl32, ExactSpelling = true)] @@ -457,24 +411,15 @@ public static bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme) [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool UpdateWindow(HandleRef hWnd); - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetCurrentProcessId(); - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] public static extern int ScrollWindowEx(HandleRef hWnd, int nXAmount, int nYAmount, NativeMethods.COMRECT rectScrollRegion, ref Interop.RECT rectClip, HandleRef hrgnUpdate, ref Interop.RECT prcUpdate, int flags); - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetThreadLocale(); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool MessageBeep(int type); [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool DrawMenuBar(HandleRef hWnd); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public extern static bool IsChild(HandleRef parent, HandleRef child); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr SetTimer(HandleRef hWnd, int nIDEvent, int uElapse, IntPtr lpTimerFunc); @@ -484,24 +429,9 @@ public static bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme) [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type); - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetTickCount(); - [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern bool ScrollWindow(HandleRef hWnd, int nXAmount, int nYAmount, ref Interop.RECT rectScrollRegion, ref Interop.RECT rectClip); - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetCurrentProcess(); - - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetCurrentThread(); - - [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)] - public extern static bool SetThreadLocale(int Locale); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsWindowUnicode(HandleRef hWnd); - [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern bool DrawEdge(HandleRef hDC, ref Interop.RECT rect, int edge, int flags); diff --git a/src/Common/src/ScreenDC.cs b/src/Common/src/ScreenDC.cs index 1c129616954..0df3557c91c 100644 --- a/src/Common/src/ScreenDC.cs +++ b/src/Common/src/ScreenDC.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using static Interop; + namespace System.Windows.Forms { /// @@ -17,7 +19,7 @@ internal ref struct ScreenDC private IntPtr _handle; public static ScreenDC Create() - => new ScreenDC { _handle = Interop.User32.GetDC(IntPtr.Zero) }; + => new ScreenDC { _handle = User32.GetDC(IntPtr.Zero) }; public static implicit operator IntPtr(ScreenDC screenDC) => screenDC._handle; @@ -25,7 +27,7 @@ public void Dispose() { if (_handle != IntPtr.Zero) { - Interop.User32.ReleaseDC(IntPtr.Zero, _handle); + User32.ReleaseDC(IntPtr.Zero, _handle); } } } diff --git a/src/Common/src/UnsafeNativeMethods.cs b/src/Common/src/UnsafeNativeMethods.cs index bcd7a32c855..b71806d1c5f 100644 --- a/src/Common/src/UnsafeNativeMethods.cs +++ b/src/Common/src/UnsafeNativeMethods.cs @@ -22,28 +22,6 @@ internal static class UnsafeNativeMethods [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int GetMessageTime(); - [DllImport(ExternDll.User32)] - public static extern int GetClassName(HandleRef hwnd, StringBuilder lpClassName, int nMaxCount); - - //SetClassLong won't work correctly for 64-bit: we should use SetClassLongPtr instead. On - //32-bit, SetClassLongPtr is just #defined as SetClassLong. SetClassLong really should - //take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit - //it'll be OK. - public static IntPtr SetClassLong(HandleRef hWnd, int nIndex, IntPtr dwNewLong) - { - if (IntPtr.Size == 4) - { - return SetClassLongPtr32(hWnd, nIndex, dwNewLong); - } - return SetClassLongPtr64(hWnd, nIndex, dwNewLong); - } - - [DllImport(ExternDll.User32, CharSet = System.Runtime.InteropServices.CharSet.Auto, EntryPoint = "SetClassLong")] - public static extern IntPtr SetClassLongPtr32(HandleRef hwnd, int nIndex, IntPtr dwNewLong); - - [DllImport(ExternDll.User32, CharSet = System.Runtime.InteropServices.CharSet.Auto, EntryPoint = "SetClassLongPtr")] - public static extern IntPtr SetClassLongPtr64(HandleRef hwnd, int nIndex, IntPtr dwNewLong); - [DllImport(ExternDll.Ole32, ExactSpelling = true, PreserveSig = false)] public static extern IClassFactory2 CoGetClassObject( [In] @@ -169,9 +147,6 @@ public static int DragQueryFileLongPath(HandleRef hDrop, int iFile, StringBuilde [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern bool EnumChildWindows(HandleRef hwndParent, NativeMethods.EnumChildrenCallback lpEnumFunc, HandleRef lParam); - [DllImport(ExternDll.Shell32, CharSet = CharSet.Auto, EntryPoint = "ShellExecute", BestFitMapping = false)] - public static extern IntPtr ShellExecute_NoBFM(HandleRef hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)] public static extern int SetScrollPos(HandleRef hWnd, int nBar, int nPos, bool bRedraw); @@ -223,24 +198,15 @@ public static int DragQueryFileLongPath(HandleRef hDrop, int iFile, StringBuilde [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)] public static extern IntPtr DuplicateHandle(HandleRef processSource, HandleRef handleSource, HandleRef processTarget, ref IntPtr handleTarget, int desiredAccess, bool inheritHandle, int options); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr SetWindowsHookEx(int hookid, NativeMethods.HookProc pfnhook, HandleRef hinst, int threadid); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int GetKeyboardState(byte[] keystate); [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int SetKeyboardState(byte[] keystate); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool UnhookWindowsHookEx(HandleRef hhook); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern short GetAsyncKeyState(int vkey); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr CallNextHookEx(HandleRef hhook, int code, IntPtr wparam, IntPtr lparam); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int ScreenToClient(HandleRef hWnd, ref Point pt); @@ -287,12 +253,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.Ole32, ExactSpelling = true, SetLastError = true)] public static extern int OleInitialize(int val = 0); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public extern static bool EnumThreadWindows(int dwThreadId, NativeMethods.EnumThreadWindowsCallback lpfn, HandleRef lParam); - - [DllImport(ExternDll.Kernel32, SetLastError = true)] - public extern static bool GetExitCodeThread(IntPtr hThread, out uint lpExitCode); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public extern static IntPtr SendDlgItemMessage(HandleRef hDlg, int nIDDlgItem, int Msg, IntPtr wParam, IntPtr lParam); @@ -305,9 +265,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern IntPtr ChildWindowFromPointEx(IntPtr hwndParent, Point pt, int uFlags); - [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)] - public static extern bool CloseHandle(HandleRef handle); - #region SendKeys SendInput functionality [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] @@ -338,15 +295,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public static extern short VkKeyScan(char key); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetCapture(); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetCapture(HandleRef hwnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetFocus(); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out Point pt); @@ -356,9 +304,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)] public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer); - [DllImport(ExternDll.User32, ExactSpelling = true)] - public static extern int SetWindowRgn(HandleRef hwnd, HandleRef hrgn, bool fRedraw); - [DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)] public static extern void GetTempFileName(string tempDirName, string prefixName, int unique, StringBuilder sb); @@ -407,24 +352,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) [DllImport(ExternDll.Imm32, CharSet = CharSet.Auto)] public static extern bool ImmNotifyIME(HandleRef hIMC, int dwAction, int dwIndex, int dwValue); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetFocus(HandleRef hWnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetParent(HandleRef hWnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetAncestor(HandleRef hWnd, int flags); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsChild(HandleRef hWndParent, HandleRef hwnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsZoomed(HandleRef hWnd); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr FindWindow(string className, string windowName); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int MapWindowPoints(HandleRef hWndFrom, HandleRef hWndTo, ref Interop.RECT rect, int cPoints); @@ -614,34 +541,9 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule) public const int SMTO_ABORTIFHUNG = 0x0002; - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetParent(HandleRef hWnd, HandleRef hWndParent); - - [DllImport(ExternDll.User32, ExactSpelling = true)] - public static extern bool GetWindowRect(HandleRef hWnd, ref Interop.RECT rect); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetDlgItem(HandleRef hWnd, int nIDDlgItem); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr DefMDIChildProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, - IntPtr wParam, IntPtr lParam); - - [DllImport(ExternDll.User32, CharSet = CharSet.Unicode, ExactSpelling = true)] - public static extern bool GetClassInfoW(HandleRef hInstance, string lpClassName, ref NativeMethods.WNDCLASS lpWndClass); - [DllImport(ExternDll.Gdi32, CharSet = CharSet.Auto)] public static extern bool GetTextMetrics(HandleRef hdc, NativeMethods.TEXTMETRIC tm); @@ -758,9 +660,6 @@ public unsafe static bool TrySystemParametersInfoForDpi(ref NativeMethods.NONCLI [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int ClientToScreen(HandleRef hWnd, ref Point lpPoint); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetForegroundWindow(); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int MsgWaitForMultipleObjectsEx(int nCount, IntPtr pHandles, int dwMilliseconds, int dwWakeMask, int dwFlags); @@ -831,62 +730,6 @@ public unsafe static bool TrySystemParametersInfoForDpi(ref NativeMethods.NONCLI [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int SetScrollInfo(HandleRef hWnd, int fnBar, NativeMethods.SCROLLINFO si, bool redraw); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetActiveWindow(); - - //GetWindowLong won't work correctly for 64-bit: we should use GetWindowLongPtr instead. On - //32-bit, GetWindowLongPtr is just #defined as GetWindowLong. GetWindowLong really should - //take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit - //it'll be OK. - public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex) - { - if (IntPtr.Size == 4) - { - return GetWindowLong32(hWnd, nIndex); - } - return GetWindowLongPtr64(hWnd, nIndex); - } - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "GetWindowLong")] - public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "GetWindowLongPtr")] - public static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex); - - //SetWindowLong won't work correctly for 64-bit: we should use SetWindowLongPtr instead. On - //32-bit, SetWindowLongPtr is just #defined as SetWindowLong. SetWindowLong really should - //take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit - //it'll be OK. - public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong) - { - if (IntPtr.Size == 4) - { - return SetWindowLongPtr32(hWnd, nIndex, dwNewLong); - } - return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); - } - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")] - public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")] - public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, HandleRef dwNewLong); - - public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc) - { - if (IntPtr.Size == 4) - { - return SetWindowLongPtr32(hWnd, nIndex, wndproc); - } - return SetWindowLongPtr64(hWnd, nIndex, wndproc); - } - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")] - public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")] - public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc); - [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern IntPtr CreatePopupMenu(); @@ -902,9 +745,6 @@ public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.Wnd [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetSystemMenu(HandleRef hWnd, bool bRevert); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr DefFrameProc(IntPtr hWnd, IntPtr hWndClient, int msg, IntPtr wParam, IntPtr lParam); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool TranslateMDISysAccel(IntPtr hWndClient, [In, Out] ref NativeMethods.MSG msg); @@ -914,9 +754,6 @@ public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.Wnd [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public extern static bool SetMenu(HandleRef hWnd, HandleRef hMenu); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetWindowPlacement(HandleRef hWnd, ref NativeMethods.WINDOWPLACEMENT placement); - [DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)] public static extern void GetStartupInfo([In, Out] NativeMethods.STARTUPINFO_I startupinfo_i); @@ -926,9 +763,6 @@ public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.Wnd [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern bool EnableMenuItem(HandleRef hMenu, int UIDEnabledItem, int uEnable); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetActiveWindow(HandleRef hWnd); - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] public static extern IntPtr CreateIC(string lpszDriverName, string lpszDeviceName, string lpszOutput, HandleRef /*DEVMODE*/ lpInitData); @@ -950,9 +784,6 @@ public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.Wnd [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern bool DestroyCursor(HandleRef hCurs); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool IsWindow(HandleRef hWnd); - public const int LAYOUT_RTL = 0x00000001; public const int LAYOUT_BITMAPORIENTATIONPRESERVED = 0x00000008; @@ -977,36 +808,12 @@ public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.Wnd [DllImport(ExternDll.User32, ExactSpelling = true)] public static extern IntPtr WindowFromPoint(Point pt); - [DllImport(ExternDll.User32, CharSet = CharSet.Unicode, SetLastError = true)] - public static extern IntPtr CreateWindowEx( - int dwExStyle, - string lpClassName, - string lpWindowName, - int dwStyle, - int X, - int Y, - int nWidth, - int nHeight, - HandleRef hWndParent, - HandleRef hMenu, - HandleRef hInst, - [MarshalAs(UnmanagedType.AsAny)] object lpParam); - - [DllImport(ExternDll.User32, ExactSpelling = true)] - public static extern bool DestroyWindow(HandleRef hWnd); - - [DllImport(ExternDll.User32, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)] - public static extern ushort RegisterClassW(ref NativeMethods.WNDCLASS lpWndClass); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern void PostQuitMessage(int nExitCode); [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern void WaitMessage(); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool SetWindowPlacement(HandleRef hWnd, [In] ref NativeMethods.WINDOWPLACEMENT placement); - // This method is not available until Windows 8.1 [DllImport(ExternDll.User32, ExactSpelling = true, SetLastError = true)] public static extern uint GetDpiForWindow(HandleRef hWnd); diff --git a/src/Common/tests/InternalUtilitiesForTests/InternalUtilitiesForTests.csproj b/src/Common/tests/InternalUtilitiesForTests/InternalUtilitiesForTests.csproj index b11c8d9d560..32ecadd88c0 100644 --- a/src/Common/tests/InternalUtilitiesForTests/InternalUtilitiesForTests.csproj +++ b/src/Common/tests/InternalUtilitiesForTests/InternalUtilitiesForTests.csproj @@ -13,4 +13,11 @@ + + + + + + + diff --git a/src/Common/tests/InternalUtilitiesForTests/src/PlatformDetection.Windows.cs b/src/Common/tests/InternalUtilitiesForTests/src/PlatformDetection.Windows.cs index a1efaf770fc..b07d90842c2 100644 --- a/src/Common/tests/InternalUtilitiesForTests/src/PlatformDetection.Windows.cs +++ b/src/Common/tests/InternalUtilitiesForTests/src/PlatformDetection.Windows.cs @@ -3,12 +3,11 @@ // See the LICENSE file in the project root for more information. using Microsoft.Win32; -using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Security; -using System.Text; using Xunit; +using static Interop; namespace System { @@ -180,7 +179,7 @@ public static bool IsWindowsAndElevated return false; } - Assert.True(OpenProcessToken(GetCurrentProcess(), TOKEN_READ, out IntPtr processToken)); + Assert.True(OpenProcessToken(Kernel32.GetCurrentProcess(), TOKEN_READ, out IntPtr processToken)); try { @@ -191,7 +190,7 @@ public static bool IsWindowsAndElevated } finally { - CloseHandle(processToken); + Kernel32.CloseHandle(processToken); } return s_isWindowsElevated == 1; @@ -300,14 +299,7 @@ private static unsafe int GetWindowsVersion() [DllImport("kernel32.dll", ExactSpelling = true)] private static extern int GetCurrentApplicationUserModelId(ref uint applicationUserModelIdLength, byte[] applicationUserModelId); - [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] - private static extern bool CloseHandle(IntPtr handle); - [DllImport("advapi32.dll", SetLastError = true, ExactSpelling = true)] private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle); - - // The process handle does NOT need closing - [DllImport("kernel32.dll", ExactSpelling = true)] - private static extern IntPtr GetCurrentProcess(); } } diff --git a/src/System.Windows.Forms.Design.Editors/src/Misc/NativeMethods.cs b/src/System.Windows.Forms.Design.Editors/src/Misc/NativeMethods.cs index 11cb02c1223..9c6a681c2d8 100644 --- a/src/System.Windows.Forms.Design.Editors/src/Misc/NativeMethods.cs +++ b/src/System.Windows.Forms.Design.Editors/src/Misc/NativeMethods.cs @@ -40,12 +40,6 @@ internal static partial class NativeMethods [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetDlgItem(IntPtr hWnd, int nIDDlgItem); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool EnableWindow(IntPtr hWnd, bool enable); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int GetDlgItemInt(IntPtr hWnd, int nIDDlgItem, bool[] err, bool signed); @@ -106,7 +100,6 @@ public class TRACKMOUSEEVENT public const int E_INVALIDARG = unchecked((int)0x80070057); public const int E_FAIL = unchecked((int)0x80004005); - public const int WS_EX_STATICEDGE = 0x00020000; public static readonly int TME_HOVER = 0x00000001; public const int @@ -386,17 +379,9 @@ int OnPosRectChange( [In] COMRECT lprcPosRect); } - public static readonly int WM_MOUSEENTER = Util.RegisterWindowMessage("WinFormsMouseEnter"); + public static readonly int WM_MOUSEENTER = User32.RegisterWindowMessageW("WinFormsMouseEnter"); public static readonly int HDN_ENDTRACK = HDN_ENDTRACKW; - public const int WS_DISABLED = 0x08000000; - public const int WS_CLIPSIBLINGS = 0x04000000; - public const int WS_CLIPCHILDREN = 0x02000000; - public const int WS_EX_TOOLWINDOW = 0x00000080; - public const int WS_POPUP = unchecked((int)0x80000000); - public const int WS_BORDER = 0x00800000; - public const int CS_DROPSHADOW = 0x00020000; - public const int CS_DBLCLKS = 0x0008; public const int SRCCOPY = 0x00CC0020; public const int LVM_SETCOLUMNWIDTH = 0x1000 + 30; public const int LVM_GETHEADER = 0x1000 + 31; @@ -467,10 +452,6 @@ int OnPosRectChange( public const int HHT_BELOW = 0x0200; public const int HHT_TORIGHT = 0x0400; public const int HHT_TOLEFT = 0x0800; - public const int HWND_TOP = 0; - public const int HWND_BOTTOM = 1; - public const int HWND_TOPMOST = -1; - public const int HWND_NOTOPMOST = -2; public const int CWP_SKIPINVISIBLE = 0x0001; public const int RDW_FRAME = 0x0400; public const int TVM_GETITEMRECT = 0x1100 + 4; @@ -499,14 +480,6 @@ int OnPosRectChange( public const int TVHT_BELOW = 0x0200; public const int TVHT_TORIGHT = 0x0400; public const int TVHT_TOLEFT = 0x0800; - public const int GW_HWNDFIRST = 0; - public const int GW_HWNDLAST = 1; - public const int GW_HWNDNEXT = 2; - public const int GW_HWNDPREV = 3; - public const int GW_OWNER = 4; - public const int GW_CHILD = 5; - public const int GW_MAX = 5; - public const int GWL_HWNDPARENT = -8; public const int SB_HORZ = 0; public const int SB_VERT = 1; public const int SB_CTL = 2; @@ -589,21 +562,6 @@ int OnPosRectChange( public const int PRF_CLIENT = 0x00000004; public const int PRF_ERASEBKGND = 0x00000008; public const int PRF_CHILDREN = 0x00000010; - public const int SWP_NOSIZE = 0x0001; - public const int SWP_NOMOVE = 0x0002; - public const int SWP_NOZORDER = 0x0004; - public const int SWP_NOREDRAW = 0x0008; - public const int SWP_NOACTIVATE = 0x0010; - public const int SWP_FRAMECHANGED = 0x0020; - public const int SWP_SHOWWINDOW = 0x0040; - public const int SWP_HIDEWINDOW = 0x0080; - public const int SWP_NOCOPYBITS = 0x0100; - public const int SWP_NOOWNERZORDER = 0x0200; - public const int SWP_NOSENDCHANGING = 0x0400; - public const int SWP_DRAWFRAME = 0x0020; - public const int SWP_NOREPOSITION = 0x0200; - public const int SWP_DEFERERASE = 0x2000; - public const int SWP_ASYNCWINDOWPOS = 0x4000; [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); @@ -658,8 +616,6 @@ public class TV_HITTESTINFO public delegate void TimerProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); - public delegate IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); - internal class Util { public static int MAKELONG(int low, int high) @@ -671,9 +627,6 @@ public static int LOWORD(int n) { return n & 0xffff; } - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - internal static extern int RegisterWindowMessage(string msg); } internal class ActiveX @@ -923,9 +876,6 @@ [Out] [MarshalAs(UnmanagedType.LPArray)] public const int MWMO_INPUTAVAILABLE = 0x0004; // don't use MWMO_WAITALL, see ddb#176342 - public const int GWL_EXSTYLE = -20, - GWL_STYLE = -16; - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class LOGFONT { diff --git a/src/System.Windows.Forms.Design.Editors/src/Misc/UnsafeNativeMethods.cs b/src/System.Windows.Forms.Design.Editors/src/Misc/UnsafeNativeMethods.cs index 1c00fd8e588..8c5d9b56237 100644 --- a/src/System.Windows.Forms.Design.Editors/src/Misc/UnsafeNativeMethods.cs +++ b/src/System.Windows.Forms.Design.Editors/src/Misc/UnsafeNativeMethods.cs @@ -16,23 +16,12 @@ internal class UnsafeNativeMethods [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int ScreenToClient(HandleRef hWnd, ref Point lpPoint); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetActiveWindow(); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern void NotifyWinEvent(int winEvent, HandleRef hwnd, int objType, int objID); - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr SetFocus(HandleRef hWnd); - - [DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern IntPtr GetFocus(); - [DllImport(ExternDll.Ole32)] public static extern int ReadClassStg(HandleRef pStg, ref Guid pclsid); - public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); - public const int OBJID_CLIENT = unchecked((int)0xFFFFFFFC); } } diff --git a/src/System.Windows.Forms.Design.Editors/src/System.Windows.Forms.Design.Editors.csproj b/src/System.Windows.Forms.Design.Editors/src/System.Windows.Forms.Design.Editors.csproj index d0885fe5727..83524cfd467 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System.Windows.Forms.Design.Editors.csproj +++ b/src/System.Windows.Forms.Design.Editors/src/System.Windows.Forms.Design.Editors.csproj @@ -78,12 +78,23 @@ + - + + + + + + + + + + + diff --git a/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/CollectionEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/CollectionEditor.cs index 78904e3daa8..3f51f37067a 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/CollectionEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/ComponentModel/Design/CollectionEditor.cs @@ -2135,7 +2135,7 @@ protected override void WndProc(ref Message m) if (PropertyGrid != null) { PropertyGrid.Focus(); - UnsafeNativeMethods.SetFocus(new HandleRef(PropertyGrid, PropertyGrid.Handle)); + User32.SetFocus(new HandleRef(PropertyGrid, PropertyGrid.Handle)); Application.DoEvents(); } else @@ -2146,7 +2146,7 @@ protected override void WndProc(ref Message m) if (PropertyGrid.Focused || PropertyGrid.ContainsFocus) { // recreate the keystroke to the newly activated window - NativeMethods.SendMessage(UnsafeNativeMethods.GetFocus(), WindowMessages.WM_KEYDOWN, _lastKeyDown.WParam, _lastKeyDown.LParam); + NativeMethods.SendMessage(User32.GetFocus(), WindowMessages.WM_KEYDOWN, _lastKeyDown.WParam, _lastKeyDown.LParam); } } break; @@ -2161,7 +2161,7 @@ protected override void WndProc(ref Message m) if (PropertyGrid != null) { PropertyGrid.Focus(); - UnsafeNativeMethods.SetFocus(new HandleRef(PropertyGrid, PropertyGrid.Handle)); + User32.SetFocus(new HandleRef(PropertyGrid, PropertyGrid.Handle)); Application.DoEvents(); } else @@ -2172,7 +2172,7 @@ protected override void WndProc(ref Message m) // Make sure we changed focus properly recreate the keystroke to the newly activated window if (PropertyGrid.Focused || PropertyGrid.ContainsFocus) { - IntPtr hWnd = UnsafeNativeMethods.GetFocus(); + IntPtr hWnd = User32.GetFocus(); NativeMethods.SendMessage(hWnd, WindowMessages.WM_KEYDOWN, _lastKeyDown.WParam, _lastKeyDown.LParam); NativeMethods.SendMessage(hWnd, WindowMessages.WM_CHAR, m.WParam, m.LParam); return; diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ColorEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ColorEditor.cs index 1797f7165af..40e880372b3 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ColorEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ColorEditor.cs @@ -340,7 +340,7 @@ protected virtual void LaunchDialog(int customIndex) colorUI.EditorService.CloseDropDown(); // It will be closed anyway as soon as it sees the WM_ACTIVATE CustomColorDialog dialog = new CustomColorDialog(); - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); try { DialogResult result = dialog.ShowDialog(); @@ -358,7 +358,7 @@ protected virtual void LaunchDialog(int customIndex) { if (hwndFocus != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, hwndFocus)); + User32.SetFocus(hwndFocus); } } } @@ -1121,11 +1121,11 @@ protected override IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr l NativeMethods.SendDlgItemMessage(hwnd, COLOR_GREEN, EditMessages.EM_SETMARGINS, (IntPtr)(NativeMethods.EC_LEFTMARGIN | NativeMethods.EC_RIGHTMARGIN), IntPtr.Zero); NativeMethods.SendDlgItemMessage(hwnd, COLOR_BLUE, EditMessages.EM_SETMARGINS, (IntPtr)(NativeMethods.EC_LEFTMARGIN | NativeMethods.EC_RIGHTMARGIN), IntPtr.Zero); IntPtr hwndCtl = NativeMethods.GetDlgItem(hwnd, COLOR_MIX); - NativeMethods.EnableWindow(hwndCtl, false); - NativeMethods.SetWindowPos(hwndCtl, IntPtr.Zero, 0, 0, 0, 0, NativeMethods.SWP_HIDEWINDOW); + User32.EnableWindow(hwndCtl, false); + User32.SetWindowPos(hwndCtl, IntPtr.Zero, flags: User32.WindowPosition.SWP_HIDEWINDOW); hwndCtl = NativeMethods.GetDlgItem(hwnd, NativeMethods.IDOK); - NativeMethods.EnableWindow(hwndCtl, false); - NativeMethods.SetWindowPos(hwndCtl, IntPtr.Zero, 0, 0, 0, 0, NativeMethods.SWP_HIDEWINDOW); + User32.EnableWindow(hwndCtl, false); + User32.SetWindowPos(hwndCtl, IntPtr.Zero, flags: User32.WindowPosition.SWP_HIDEWINDOW); this.Color = Color.Empty; break; case WindowMessages.WM_COMMAND: diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/FontEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/FontEditor.cs index 2be45c66507..935051c56e0 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/FontEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/FontEditor.cs @@ -3,10 +3,9 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.InteropServices; using System.Windows.Forms; using System.Windows.Forms.Design; +using static Interop; namespace System.Drawing.Design { @@ -43,7 +42,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide _fontDialog.Font = fontValue; } - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); try { if (_fontDialog.ShowDialog() == DialogResult.OK) @@ -55,7 +54,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide { if (hwndFocus != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, hwndFocus)); + User32.SetFocus(hwndFocus); } } } diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ImageEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ImageEditor.cs index 8bcab6d6158..b55f41ec2de 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ImageEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Drawing/Design/ImageEditor.cs @@ -6,10 +6,10 @@ using System.ComponentModel; using System.IO; using System.Reflection; -using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using System.Windows.Forms.Design; +using static Interop; namespace System.Drawing.Design { @@ -101,7 +101,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide _fileDialog.Filter = filter; } - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); try { if (_fileDialog.ShowDialog() == DialogResult.OK) @@ -116,7 +116,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide { if (hwndFocus != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, hwndFocus)); + User32.SetFocus(hwndFocus); } } } diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FolderNameEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FolderNameEditor.cs index 4a0075be6cb..1d760f513e7 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FolderNameEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/FolderNameEditor.cs @@ -95,7 +95,7 @@ public string Description public unsafe DialogResult ShowDialog(IWin32Window owner) { // Get/find an owner HWND for this dialog - IntPtr hWndOwner = owner == null ? owner.Handle : UnsafeNativeMethods.GetActiveWindow(); + IntPtr hWndOwner = owner == null ? owner.Handle : User32.GetActiveWindow(); // Get the IDL for the specific startLocation Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)StartLocation, out CoTaskMemSafeHandle listHandle); diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ImageListImageEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ImageListImageEditor.cs index 8601c2fbf86..053bbca10e8 100644 --- a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ImageListImageEditor.cs +++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ImageListImageEditor.cs @@ -7,7 +7,7 @@ using System.Drawing.Design; using System.IO; using System.Reflection; -using System.Runtime.InteropServices; +using static Interop; namespace System.Windows.Forms.Design { @@ -66,7 +66,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide _fileDialog.Filter = filter; } - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); try { if (_fileDialog.ShowDialog() == DialogResult.OK) @@ -87,7 +87,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide { if (hwndFocus != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, hwndFocus)); + User32.SetFocus(hwndFocus); } } diff --git a/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj b/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj index 0761ce74580..759c933ab8a 100644 --- a/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj +++ b/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj @@ -30,7 +30,6 @@ - @@ -53,6 +52,9 @@ + + + @@ -69,10 +71,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.cs b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.cs index 5819d9b6e82..6e79d6c0632 100644 --- a/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.cs +++ b/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignerActionPanel.cs @@ -2379,9 +2379,9 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_TOOLWINDOW; - cp.Style |= NativeMethods.WS_POPUP | NativeMethods.WS_BORDER; - cp.ClassStyle |= NativeMethods.CS_SAVEBITS; + cp.ExStyle |= User32.WindowStyle.WS_EX_TOOLWINDOW; + cp.Style |= User32.WindowStyle.WS_POPUP | User32.WindowStyle.WS_BORDER; + cp.ClassStyle |= (int)User32.ClassStyle.CS_SAVEBITS; if (_parentControl != null) { if (!_parentControl.IsDisposed) @@ -2417,7 +2417,7 @@ private bool OwnsWindow(IntPtr hWnd) { while (hWnd != IntPtr.Zero) { - hWnd = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWnd), NativeMethods.GWL_HWNDPARENT); + hWnd = User32.GetWindowLong(hWnd, User32.WindowLong.GWL_HWNDPARENT); if (hWnd == IntPtr.Zero) { return false; @@ -2447,14 +2447,15 @@ public void ShowDropDown(Control parent) { try { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(parent, parent.Handle)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, new HandleRef(parent, parent.Handle)); // Lifted directly from Form.ShowDialog()... - IntPtr hWndCapture = UnsafeNativeMethods.GetCapture(); + IntPtr hWndCapture = User32.GetCapture(); if (hWndCapture != IntPtr.Zero) { UnsafeNativeMethods.SendMessage(new HandleRef(null, hWndCapture), WindowMessages.WM_CANCELMODE, 0, 0); - SafeNativeMethods.ReleaseCapture(); + User32.ReleaseCapture(); } + Visible = true; // NOTE: Do this AFTER creating handle and setting parent FocusComponent(); DoModalLoop(); @@ -2462,7 +2463,7 @@ public void ShowDropDown(Control parent) finally { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(null, IntPtr.Zero)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, IntPtr.Zero); // sometimes activation goes to LALA land - if our parent control is still around, remind it to take focus. if (parent != null && parent.Visible) { @@ -2506,10 +2507,6 @@ private static class NativeMethods { public const int WA_INACTIVE = 0; public const int WA_ACTIVE = 1; - public const int WS_EX_TOOLWINDOW = 0x00000080; - public const int WS_POPUP = unchecked((int)0x80000000); - public const int WS_BORDER = 0x00800000; - public const int GWL_HWNDPARENT = (-8); public const int QS_KEY = 0x0001; public const int QS_MOUSEMOVE = 0x0002; public const int QS_MOUSEBUTTON = 0x0004; @@ -2585,9 +2582,6 @@ public struct TEXTMETRICA private static class SafeNativeMethods { - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern bool ReleaseCapture(); - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] public static extern IntPtr SelectObject(HandleRef hDC, HandleRef hObject); @@ -2636,20 +2630,11 @@ public static int GetTextMetrics(HandleRef hDC, ref NativeMethods.TEXTMETRIC lpt private static class UnsafeNativeMethods { - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr GetWindowLong(HandleRef hWnd, int nIndex); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong); - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public static extern int MsgWaitForMultipleObjectsEx(int nCount, IntPtr pHandles, int dwMilliseconds, int dwWakeMask, int dwFlags); [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam); - - [DllImport(ExternDll.User32, CharSet = CharSet.Auto)] - public static extern IntPtr GetCapture(); } #endregion diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/BehaviorService.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/BehaviorService.cs index af64eeec4d3..6efc4451ccb 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/BehaviorService.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/Behavior/BehaviorService.cs @@ -102,8 +102,8 @@ internal BehaviorService(IServiceProvider serviceProvider, Control windowFrame) _queriedSnapLines = false; //test hooks - WM_GETALLSNAPLINES = SafeNativeMethods.RegisterWindowMessage("WM_GETALLSNAPLINES"); - WM_GETRECENTSNAPLINES = SafeNativeMethods.RegisterWindowMessage("WM_GETRECENTSNAPLINES"); + WM_GETALLSNAPLINES = User32.RegisterWindowMessageW("WM_GETALLSNAPLINES"); + WM_GETRECENTSNAPLINES = User32.RegisterWindowMessageW("WM_GETRECENTSNAPLINES"); // Listen to the SystemEvents so that we can resync selection based on display settings etc. SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged); @@ -543,9 +543,9 @@ public void PushCaptureBehavior(Behavior behavior) if (uiService != null) { IWin32Window hwnd = uiService.GetDialogOwnerWindow(); - if (hwnd != null && hwnd.Handle != IntPtr.Zero && hwnd.Handle != UnsafeNativeMethods.GetActiveWindow()) + if (hwnd != null && hwnd.Handle != IntPtr.Zero && hwnd.Handle != User32.GetActiveWindow()) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(this, hwnd.Handle)); + User32.SetActiveWindow(new HandleRef(this, hwnd.Handle)); } } } @@ -608,8 +608,8 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style &= ~(NativeMethods.WS_CLIPCHILDREN | NativeMethods.WS_CLIPSIBLINGS); - cp.ExStyle |= NativeMethods.WS_EX_TRANSPARENT; + cp.Style &= ~(User32.WindowStyle.WS_CLIPCHILDREN | User32.WindowStyle.WS_CLIPSIBLINGS); + cp.ExStyle |= User32.WindowStyle.WS_EX_TRANSPARENT; return cp; } } @@ -1075,17 +1075,18 @@ private void HookMouse() if (_thisProcessID == 0) { AdornerWindow adornerWindow = AdornerWindow.s_adornerWindowList[0]; - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(adornerWindow, adornerWindow.Handle), out _thisProcessID); + User32.GetWindowThreadProcessId(new HandleRef(adornerWindow, adornerWindow.Handle), out _thisProcessID); } - NativeMethods.HookProc hook = new NativeMethods.HookProc(MouseHookProc); + var hook = new User32.HookProc(MouseHookProc); _mouseHookRoot = GCHandle.Alloc(hook); #pragma warning disable 618 - _mouseHookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE, - hook, - new HandleRef(null, IntPtr.Zero), - AppDomain.GetCurrentThreadId()); + _mouseHookHandle = User32.SetWindowsHookExW( + User32.WindowHookProcedure.WH_MOUSE, + hook, + IntPtr.Zero, + AppDomain.GetCurrentThreadId()); #pragma warning restore 618 if (_mouseHookHandle != IntPtr.Zero) { @@ -1129,7 +1130,7 @@ private unsafe IntPtr MouseHookProc(int nCode, IntPtr wparam, IntPtr lparam) } Debug.Assert(_isHooked, "How did we get here when we are diposed?"); - return UnsafeNativeMethods.CallNextHookEx(new HandleRef(this, _mouseHookHandle), nCode, wparam, lparam); + return User32.CallNextHookEx(new HandleRef(this, _mouseHookHandle), nCode, wparam, lparam); } private void UnhookMouse() @@ -1138,7 +1139,7 @@ private void UnhookMouse() { if (_mouseHookHandle != IntPtr.Zero) { - UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(this, _mouseHookHandle)); + User32.UnhookWindowsHookEx(new HandleRef(this, _mouseHookHandle)); _mouseHookRoot.Free(); _mouseHookHandle = IntPtr.Zero; _isHooked = false; @@ -1164,11 +1165,11 @@ private bool ProcessMouseMessage(IntPtr hWnd, int msg, int x, int y) IntPtr handle = adornerWindow.DesignerFrame.Handle; // if it's us or one of our children, just process as normal - if (adornerWindow.ProcessingDrag || (hWnd != handle && SafeNativeMethods.IsChild(new HandleRef(this, handle), new HandleRef(this, hWnd)))) + if (adornerWindow.ProcessingDrag || (hWnd != handle && User32.IsChild(new HandleRef(this, handle), new HandleRef(this, hWnd)))) { Debug.Assert(_thisProcessID != 0, "Didn't get our process id!"); // make sure the window is in our process - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, hWnd), out int pid); + User32.GetWindowThreadProcessId(hWnd, out int pid); // if this isn't our process, bail if (pid != _thisProcessID) { diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ControlDesigner.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ControlDesigner.cs index 3343fa42ae8..b4a6e8d6372 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ControlDesigner.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ControlDesigner.cs @@ -394,7 +394,10 @@ protected override InheritanceAttribute InheritanceAttribute /// /// Default processing for messages. This method causes the message to get processed by windows, skipping the control. This is useful if you want to block this message from getting to the control, but you do not want to block it from getting to Windows itself because it causes other messages to be generated. /// - protected void BaseWndProc(ref Message m) => m.Result = UnsafeNativeMethods.DefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam); + protected void BaseWndProc(ref Message m) + { + m.Result = User32.DefWindowProcW(m.HWnd, m.Msg, m.WParam, m.LParam); + } /// /// Determines if the this designer can be parented to the specified desinger -- generally this means if the control for this designer can be parented into the given ParentControlDesigner's designer. @@ -2353,7 +2356,7 @@ private bool IsDoubleClick(int x, int y) { bool doubleClick = false; int wait = SystemInformation.DoubleClickTime; - int elapsed = SafeNativeMethods.GetTickCount() - _lastClickMessageTime; + int elapsed = Kernel32.GetTickCount() - _lastClickMessageTime; if (elapsed <= wait) { Size dblClick = SystemInformation.DoubleClickSize; @@ -2370,7 +2373,7 @@ private bool IsDoubleClick(int x, int y) { _lastClickMessagePositionX = x; _lastClickMessagePositionY = y; - _lastClickMessageTime = SafeNativeMethods.GetTickCount(); + _lastClickMessageTime = Kernel32.GetTickCount(); } else { @@ -2656,15 +2659,15 @@ internal void HookChildHandles(IntPtr firstChild) if (child == null || Control is UserControl) { // Now do the children of this window. - HookChildHandles(UnsafeNativeMethods.GetWindow(hwndChild, NativeMethods.GW_CHILD)); + HookChildHandles(User32.GetWindow(hwndChild, User32.GetWindowOption.GW_CHILD)); } - hwndChild = UnsafeNativeMethods.GetWindow(hwndChild, NativeMethods.GW_HWNDNEXT); + hwndChild = User32.GetWindow(hwndChild, User32.GetWindowOption.GW_HWNDNEXT); } } private bool IsWindowInCurrentProcess(IntPtr hwnd) { - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, hwnd), out int pid); + User32.GetWindowThreadProcessId(hwnd, out int pid); return pid == CurrentProcessId; } @@ -2674,8 +2677,9 @@ private int CurrentProcessId { if (s_currentProcessId == 0) { - s_currentProcessId = SafeNativeMethods.GetCurrentProcessId(); + s_currentProcessId = Kernel32.GetCurrentProcessId(); } + return s_currentProcessId; } } @@ -2686,7 +2690,7 @@ private void OnHandleChange() // 1. Child handles that do not have a Control associated with them. We must subclass these and prevent them from getting design-time events. // 2. Child handles that do have a Control associated with them, but the control does not have a designer. We must hook the WindowTarget on these controls and prevent them from getting design-time events. // 3. Child handles that do have a Control associated with them, and the control has a designer. We ignore these and let the designer handle their messages. - HookChildHandles(UnsafeNativeMethods.GetWindow(Control.Handle, NativeMethods.GW_CHILD)); + HookChildHandles(User32.GetWindow(Control.Handle, User32.GetWindowOption.GW_CHILD)); HookChildControls(Control); } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionUI.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionUI.cs index e124eee5e68..42f6b90c569 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionUI.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerActionUI.cs @@ -714,7 +714,7 @@ internal void ShowDesignerActionPanel(IComponent relatedComponent, DesignerActio { Debug.WriteLineIf(s_designeActionPanelTraceSwitch.TraceVerbose, "Assigning owner to mainParentWindow"); Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "Assigning owner to mainParentWindow"); - UnsafeNativeMethods.SetWindowLong(new HandleRef(designerActionHost, designerActionHost.Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(_mainParentWindow, _mainParentWindow.Handle)); + User32.SetWindowLong(new HandleRef(designerActionHost, designerActionHost.Handle), User32.WindowLong.GWL_HWNDPARENT, new HandleRef(_mainParentWindow, _mainParentWindow.Handle)); } _cancelClose = true; @@ -737,7 +737,7 @@ private void OnShowComplete(object sender, EventArgs e) // force the panel to be the active window - for some reason someone else could have forced VS to become active for real while we were ignoring close. This might be bad cause we'd be in a bad state. if (designerActionHost != null && designerActionHost.Handle != IntPtr.Zero && designerActionHost.Visible) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(this, designerActionHost.Handle)); + User32.SetActiveWindow(new HandleRef(this, designerActionHost.Handle)); designerActionHost.CheckFocusIsRight(); } } @@ -799,19 +799,19 @@ public void UpdateContainerSize() public void CheckFocusIsRight() { // fix to get the focus to NOT stay on ContainerControl Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "Checking focus..."); - IntPtr focusedControl = UnsafeNativeMethods.GetFocus(); + IntPtr focusedControl = User32.GetFocus(); if (focusedControl == Handle) { Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, " putting focus on the panel..."); _panel.Focus(); } - focusedControl = UnsafeNativeMethods.GetFocus(); + focusedControl = User32.GetFocus(); if (CurrentPanel != null && CurrentPanel.Handle == focusedControl) { Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, " selecting next available control on the panel..."); CurrentPanel.SelectNextControl(null, true, true, true, true); } - UnsafeNativeMethods.GetFocus(); + User32.GetFocus(); } protected override void OnLayout(LayoutEventArgs levent) @@ -836,7 +836,7 @@ protected override void OnClosing(ToolStripDropDownClosingEventArgs e) // - if it's a window that's owned by the toolstrip dropdown dont exit else if (e.CloseReason == ToolStripDropDownCloseReason.AppFocusChange || e.CloseReason == ToolStripDropDownCloseReason.AppClicked) { - IntPtr hwndActivating = UnsafeNativeMethods.GetActiveWindow(); + IntPtr hwndActivating = User32.GetActiveWindow(); if (Handle == hwndActivating && e.CloseReason == ToolStripDropDownCloseReason.AppClicked) { e.Cancel = false; @@ -872,7 +872,7 @@ protected override void OnClosing(ToolStripDropDownClosingEventArgs e) } // what's the owner of the windows being activated? - IntPtr parent = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndActivating), NativeMethods.GWL_HWNDPARENT); + IntPtr parent = User32.GetWindowLong(new HandleRef(this, hwndActivating), User32.WindowLong.GWL_HWNDPARENT); // is it currently disabled (ie, the activating windows is in modal mode) if (!IsWindowEnabled(parent)) { @@ -960,7 +960,7 @@ private static bool WindowOwnsWindow(IntPtr hWndOwner, IntPtr hWndDescendant) { Debug.WriteLine("\t\tOWNER: " + GetControlInformation(hWndOwner)); Debug.WriteLine("\t\tOWNEE: " + GetControlInformation(hWndDescendant)); - IntPtr claimedOwnerHwnd = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWndDescendant), NativeMethods.GWL_HWNDPARENT); + IntPtr claimedOwnerHwnd = User32.GetWindowLong(hWndDescendant, User32.WindowLong.GWL_HWNDPARENT); Debug.WriteLine("OWNEE's CLAIMED OWNER: " + GetControlInformation(claimedOwnerHwnd)); } #endif @@ -972,7 +972,7 @@ private static bool WindowOwnsWindow(IntPtr hWndOwner, IntPtr hWndDescendant) while (hWndDescendant != IntPtr.Zero) { - hWndDescendant = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWndDescendant), NativeMethods.GWL_HWNDPARENT); + hWndDescendant = User32.GetWindowLong(hWndDescendant, User32.WindowLong.GWL_HWNDPARENT); if (hWndDescendant == IntPtr.Zero) { Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "NOPE."); @@ -1032,8 +1032,8 @@ internal static string GetControlInformation(IntPtr hwnd) } private bool IsWindowEnabled(IntPtr handle) { - int style = (int)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_STYLE); - return (style & NativeMethods.WS_DISABLED) == 0; + int style = (int)User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_STYLE); + return (style & User32.WindowStyle.WS_DISABLED) == 0; } private void WmActivate(ref Message m) @@ -1044,7 +1044,7 @@ private void WmActivate(ref Message m) if (WindowOwnsWindow(Handle, hwndActivating)) { Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "[DesignerActionUI WmActivate] setting cancel close true because WindowsOwnWindow"); - Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "[DesignerActionUI WmActivate] checking the focus... " + GetControlInformation(UnsafeNativeMethods.GetFocus())); + Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "[DesignerActionUI WmActivate] checking the focus... " + GetControlInformation(User32.GetFocus())); _cancelClose = true; } else @@ -1075,7 +1075,7 @@ protected override bool ProcessDialogKey(Keys keyData) // since we're not hosted in a form we need to do the same logic as Form.cs. If we get an enter key we need to find the current focused control. if it's a button, we click it and return that we handled the message if (keyData == Keys.Enter) { - IntPtr focusedControlPtr = UnsafeNativeMethods.GetFocus(); + IntPtr focusedControlPtr = User32.GetFocus(); Control focusedControl = Control.FromChildHandle(focusedControlPtr); if (focusedControl is IButtonControl button && button is Control) { diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerFrame.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerFrame.cs index 5567812dcee..a121cbc5cac 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerFrame.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerFrame.cs @@ -451,8 +451,11 @@ protected override void OnLayout(LayoutEventArgs e) /// private void ParentOverlay(Control control) { - NativeMethods.SetParent(control.Handle, Handle); - SafeNativeMethods.SetWindowPos(control.Handle, (IntPtr)NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); + User32.SetParent(new HandleRef(control, control.Handle), new HandleRef(this, Handle)); + User32.SetWindowPos( + new HandleRef(control, control.Handle), + User32.HWND_TOP, + flags: User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOMOVE); } /// @@ -561,7 +564,10 @@ protected override void WndProc(ref Message m) { foreach (Control c in _overlayList) { - SafeNativeMethods.SetWindowPos(c.Handle, (IntPtr)NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); + User32.SetWindowPos( + c.Handle, + User32.HWND_TOP, + flags: User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOMOVE); } } } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/FormDocumentDesigner.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/FormDocumentDesigner.cs index 1fd8d3867cc..45682612c3f 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/FormDocumentDesigner.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/FormDocumentDesigner.cs @@ -686,7 +686,7 @@ private unsafe void WmWindowPosChanging(ref Message m) // we want to update the size if we have a menu and... // 1) we're doing an autoscale // 2) we're loading a form without an inherited menu (inherited forms will already have the right size) - if (updateSize && Menu != null && (wp->flags & NativeMethods.SWP_NOSIZE) == 0 && (IsMenuInherited || _inAutoscale)) + if (updateSize && Menu != null && (wp->flags & User32.WindowPosition.SWP_NOSIZE) == 0 && (IsMenuInherited || _inAutoscale)) { _heightDelta = GetMenuHeight(); } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/SelectionUIService.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/SelectionUIService.cs index 808198697e9..ac4279df242 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/SelectionUIService.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/SelectionUIService.cs @@ -91,7 +91,7 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style &= ~(NativeMethods.WS_CLIPSIBLINGS | NativeMethods.WS_CLIPCHILDREN); + cp.Style &= ~(User32.WindowStyle.WS_CLIPSIBLINGS | User32.WindowStyle.WS_CLIPCHILDREN); return cp; } } diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ToolStripAdornerWindowService.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ToolStripAdornerWindowService.cs index e9496e065f6..766361accae 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ToolStripAdornerWindowService.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/ToolStripAdornerWindowService.cs @@ -195,8 +195,8 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style &= ~(NativeMethods.WS_CLIPCHILDREN | NativeMethods.WS_CLIPSIBLINGS); - cp.ExStyle |= NativeMethods.WS_EX_TRANSPARENT; + cp.Style &= ~(User32.WindowStyle.WS_CLIPCHILDREN | User32.WindowStyle.WS_CLIPSIBLINGS); + cp.ExStyle |= User32.WindowStyle.WS_EX_TRANSPARENT; return cp; } } diff --git a/src/System.Windows.Forms/src/System.Windows.Forms.csproj b/src/System.Windows.Forms/src/System.Windows.Forms.csproj index a5e7395c060..66d174cefb4 100644 --- a/src/System.Windows.Forms/src/System.Windows.Forms.csproj +++ b/src/System.Windows.Forms/src/System.Windows.Forms.csproj @@ -28,7 +28,6 @@ - diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs index 9c63a8dac00..3d4985f965a 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs @@ -709,10 +709,10 @@ public static VisualStyleState VisualStyleState { SafeNativeMethods.SetThemeAppProperties((int)value); - //248887 we need to send a WM_THEMECHANGED to the top level windows of this application. - //We do it this way to ensure that we get all top level windows -- whether we created them or not. - SafeNativeMethods.EnumThreadWindowsCallback callback = new SafeNativeMethods.EnumThreadWindowsCallback(Application.SendThemeChanged); - SafeNativeMethods.EnumWindows(callback, IntPtr.Zero); + // We need to send a WM_THEMECHANGED to the top level windows of this application. + // We do it this way to ensure that we get all top level windows whether we created them or not. + var callback = new User32.EnumWindowsCallback(Application.SendThemeChanged); + User32.EnumWindows(callback, IntPtr.Zero); GC.KeepAlive(callback); } @@ -724,9 +724,9 @@ public static VisualStyleState VisualStyleState /// private static bool SendThemeChanged(IntPtr handle, IntPtr extraParameter) { - int thisPID = SafeNativeMethods.GetCurrentProcessId(); - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, handle), out int processId); - if (processId == thisPID && SafeNativeMethods.IsWindowVisible(new HandleRef(null, handle))) + int thisPID = Kernel32.GetCurrentProcessId(); + User32.GetWindowThreadProcessId(handle, out int processId); + if (processId == thisPID && User32.IsWindowVisible(handle)) { SendThemeChangedRecursive(handle, IntPtr.Zero); @@ -1137,7 +1137,7 @@ private static Type GetAppMainType() /// private static ThreadContext GetContextForHandle(HandleRef handle) { - int id = SafeNativeMethods.GetWindowThreadProcessId(handle, out int pid); + int id = User32.GetWindowThreadProcessId(handle, out int pid); ThreadContext cxt = ThreadContext.FromId(id); Debug.Assert(cxt != null, "No thread context for handle. This is expected if you saw a previous assert about the handle being invalid."); return cxt; @@ -1201,10 +1201,10 @@ private static void RaiseThreadExit() /// "Parks" the given HWND to a temporary HWND. This allows WS_CHILD windows to /// be parked. /// - internal static void ParkHandle(HandleRef handle, DpiAwarenessContext dpiAwarenessContext = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) + internal static void ParkHandle(HandleRef handle, User32.DpiAwarenessContext dpiAwarenessContext = User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) { - Debug.Assert(UnsafeNativeMethods.IsWindow(handle), "Handle being parked is not a valid window handle"); - Debug.Assert(((int)UnsafeNativeMethods.GetWindowLong(handle, NativeMethods.GWL_STYLE) & NativeMethods.WS_CHILD) != 0, "Only WS_CHILD windows should be parked."); + Debug.Assert(User32.IsWindow(handle), "Handle being parked is not a valid window handle"); + Debug.Assert(((int)User32.GetWindowLong(handle, User32.WindowLong.GWL_STYLE) & User32.WindowStyle.WS_CHILD) != 0, "Only WS_CHILD windows should be parked."); ThreadContext cxt = GetContextForHandle(handle); if (cxt != null) @@ -1218,7 +1218,7 @@ internal static void ParkHandle(HandleRef handle, DpiAwarenessContext dpiAwarene /// /// create params for control handle /// dpi awareness - internal static void ParkHandle(CreateParams cp, DpiAwarenessContext dpiAwarenessContext = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) + internal static void ParkHandle(CreateParams cp, User32.DpiAwarenessContext dpiAwarenessContext = User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) { ThreadContext cxt = ThreadContext.FromCurrent(); if (cxt != null) @@ -1247,7 +1247,7 @@ public static void OnThreadException(Exception t) /// "Unparks" the given HWND to a temporary HWND. This allows WS_CHILD windows to /// be parked. /// - internal static void UnparkHandle(HandleRef handle, DpiAwarenessContext context) + internal static void UnparkHandle(HandleRef handle, User32.DpiAwarenessContext context) { ThreadContext cxt = GetContextForHandle(handle); if (cxt != null) @@ -1899,13 +1899,12 @@ int pvLoopData // PVOID rgmsg[0] = msg; continueLoop = component.FContinueMessageLoop(reason, pvLoopData, rgmsg); - // If the component wants us to process the message, do it. - // The component manager hosts windows from many places. We must be sensitive - // to ansi / Unicode windows here. + // If the component wants us to process the message, do it. The component manager + // hosts windows from many places. We must be sensitive to ansi/Unicode windows here. // if (continueLoop) { - if (msg.hwnd != IntPtr.Zero && SafeNativeMethods.IsWindowUnicode(new HandleRef(null, msg.hwnd))) + if (msg.hwnd != IntPtr.Zero && User32.IsWindowUnicode(msg.hwnd)) { unicodeWindow = true; UnsafeNativeMethods.GetMessageW(ref msg, NativeMethods.NullHandleRef, 0, 0); @@ -2210,13 +2209,18 @@ public ThreadContext() { IntPtr address = IntPtr.Zero; - UnsafeNativeMethods.DuplicateHandle(new HandleRef(null, SafeNativeMethods.GetCurrentProcess()), new HandleRef(null, SafeNativeMethods.GetCurrentThread()), - new HandleRef(null, SafeNativeMethods.GetCurrentProcess()), ref address, 0, false, - NativeMethods.DUPLICATE_SAME_ACCESS); + UnsafeNativeMethods.DuplicateHandle( + new HandleRef(null, Kernel32.GetCurrentProcess()), + new HandleRef(null, Kernel32.GetCurrentThread()), + new HandleRef(null, Kernel32.GetCurrentProcess()), + ref address, + 0, + false, + NativeMethods.DUPLICATE_SAME_ACCESS); handle = address; - id = SafeNativeMethods.GetCurrentThreadId(); + id = Kernel32.GetCurrentThreadId(); messageLoopCount = 0; currentThreadContext = this; contextHash[id] = this; @@ -2421,7 +2425,7 @@ internal bool CustomThreadExceptionHandlerAttached /// Retrieves the actual parking form. This will demand create the parking window /// if it needs to. /// - internal ParkingWindow GetParkingWindow(DpiAwarenessContext context) + internal ParkingWindow GetParkingWindow(User32.DpiAwarenessContext context) { // Locking 'this' here is ok since this is an internal class. @@ -2453,7 +2457,7 @@ internal ParkingWindow GetParkingWindow(DpiAwarenessContext context) /// Returns parking window that matches dpi awareness context. return null if not found. /// /// return matching parking window from list. returns null if not found - internal ParkingWindow GetParkingWindowForContext(DpiAwarenessContext context) + internal ParkingWindow GetParkingWindowForContext(User32.DpiAwarenessContext context) { if (parkingWindows.Count == 0) @@ -2464,7 +2468,7 @@ internal ParkingWindow GetParkingWindowForContext(DpiAwarenessContext context) // Legacy OS/target framework scenario where ControlDpiContext is set to DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_UNSPECIFIED // because of 'ThreadContextDpiAwareness' API unavailability or this feature is not enabled. - if (!DpiHelper.IsScalingRequirementMet || CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(context, DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED)) + if (!DpiHelper.IsScalingRequirementMet || User32.DpiAwarenessContextsEquals(context, User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED)) { Debug.Assert(parkingWindows.Count == 1, "parkingWindows count can not be > 1 for legacy OS/target framework versions"); @@ -2474,7 +2478,7 @@ internal ParkingWindow GetParkingWindowForContext(DpiAwarenessContext context) // Supported OS scenario. foreach (ParkingWindow p in parkingWindows) { - if (CommonUnsafeNativeMethods.TryFindDpiAwarenessContextsEqual(p.DpiAwarenessContext, context)) + if (User32.DpiAwarenessContextsEquals(p.DpiAwarenessContext, context)) { return p; } @@ -2636,24 +2640,19 @@ internal void Dispose(bool postQuit) } else { - bool ourThread = SafeNativeMethods.GetCurrentThreadId() == id; + bool ourThread = Kernel32.GetCurrentThreadId() == id; try { - // We can only clean up if we're being called on our - // own thread. - // + // We can only clean up if we're being called on our own thread. if (ourThread) { - // If we had a component manager, detach from it. - // if (componentManager != null) { RevokeComponent(); } - // DisposeAssociatedComponents(); DisposeThreadWindows(); try @@ -2673,10 +2672,9 @@ internal void Dispose(bool postQuit) finally { // We can always clean up this handle, though - // if (handle != IntPtr.Zero) { - UnsafeNativeMethods.CloseHandle(new HandleRef(this, handle)); + Kernel32.CloseHandle(new HandleRef(this, handle)); handle = IntPtr.Zero; } @@ -2728,9 +2726,8 @@ private void DisposeParkingWindow() // and do not call Dispose. Otherwise we would destroy // controls that are living on the parking window. // - int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(parkingWindows[0], parkingWindows[0].Handle), out int pid); - int currentThread = SafeNativeMethods.GetCurrentThreadId(); - + int hwndThread = User32.GetWindowThreadProcessId(new HandleRef(parkingWindows[0], parkingWindows[0].Handle), out int pid); + int currentThread = Kernel32.GetCurrentThreadId(); for (int i = 0; i < parkingWindows.Count; i++) { if (hwndThread == currentThread) @@ -2742,6 +2739,7 @@ private void DisposeParkingWindow() parkingWindows[i] = null; } } + parkingWindows.Clear(); } } @@ -2873,21 +2871,17 @@ internal static void ExitDomain() } /// - /// Our finalization. Minimal stuff... this shouldn't be called... We should always be disposed. + /// Our finalization. This shouldn't be called as we should always be disposed. /// ~ThreadContext() { - - // We used to call OleUninitialize() here if we were - // still STATE_OLEINITIALIZED, but that's never the correct thing to do. - // At this point we're on the wrong thread and we should never have been - // called here in the first place. - + // We used to call OleUninitialize() here if we were still STATE_OLEINITIALIZED, but that's + // never the correct thing to do. At this point we're on the wrong thread and we should never + // have been called here in the first place. // We can always clean up this handle, though - // if (handle != IntPtr.Zero) { - UnsafeNativeMethods.CloseHandle(new HandleRef(this, handle)); + Kernel32.CloseHandle(new HandleRef(this, handle)); handle = IntPtr.Zero; } } @@ -2943,7 +2937,7 @@ internal static ThreadContext FromCurrent() internal static ThreadContext FromId(int id) { ThreadContext context = (ThreadContext)contextHash[(object)id]; - if (context == null && id == SafeNativeMethods.GetCurrentThreadId()) + if (context == null && id == Kernel32.GetCurrentThreadId()) { context = new ThreadContext(); } @@ -2977,13 +2971,13 @@ internal int GetId() } /// - /// Retrieves the culture for this thread. + /// Retrieves the culture for this thread. /// internal CultureInfo GetCulture() { - if (culture == null || culture.LCID != SafeNativeMethods.GetThreadLocale()) + if (culture == null || culture.LCID != Kernel32.GetThreadLocale()) { - culture = new CultureInfo(SafeNativeMethods.GetThreadLocale()); + culture = new CultureInfo(Kernel32.GetThreadLocale()); } return culture; @@ -3288,7 +3282,7 @@ private void RunMessageLoopInner(int reason, ApplicationContext context) bool fullModal = false; bool localModal = false; - HandleRef hwndOwner = new HandleRef(null, IntPtr.Zero); + IntPtr hwndOwner = IntPtr.Zero; if (reason == NativeMethods.MSOCM.msoloopDoEventsModal) { @@ -3313,18 +3307,17 @@ private void RunMessageLoopInner(int reason, ApplicationContext context) // If the owner window of the dialog is still enabled, disable it now. // This can happen if the owner window is from a different thread or // process. - hwndOwner = new HandleRef(null, UnsafeNativeMethods.GetWindowLong(new HandleRef(currentForm, currentForm.Handle), NativeMethods.GWL_HWNDPARENT)); - if (hwndOwner.Handle != IntPtr.Zero) + hwndOwner = User32.GetWindowLong(new HandleRef(currentForm, currentForm.Handle), User32.WindowLong.GWL_HWNDPARENT); + if (hwndOwner != IntPtr.Zero) { - if (SafeNativeMethods.IsWindowEnabled(hwndOwner)) + if (User32.IsWindowEnabled(hwndOwner)) { - SafeNativeMethods.EnableWindow(hwndOwner, false); + User32.EnableWindow(hwndOwner, false); } else { - // reset hwndOwner so we are not tempted to - // fiddle with it - hwndOwner = new HandleRef(null, IntPtr.Zero); + // Reset hwndOwner so we are not tempted to fiddle with it + hwndOwner = IntPtr.Zero; } } @@ -3333,9 +3326,9 @@ private void RunMessageLoopInner(int reason, ApplicationContext context) // if (currentForm != null && currentForm.IsHandleCreated && - SafeNativeMethods.IsWindowEnabled(new HandleRef(currentForm, currentForm.Handle)) != modalEnabled) + User32.IsWindowEnabled(new HandleRef(currentForm, currentForm.Handle)) != modalEnabled) { - SafeNativeMethods.EnableWindow(new HandleRef(currentForm, currentForm.Handle), modalEnabled); + User32.EnableWindow(new HandleRef(currentForm, currentForm.Handle), modalEnabled); } } @@ -3384,9 +3377,9 @@ private void RunMessageLoopInner(int reason, ApplicationContext context) EndModalMessageLoop(context); // Again, if the hwndOwner was valid and disabled above, re-enable it. - if (hwndOwner.Handle != IntPtr.Zero) + if (hwndOwner != IntPtr.Zero) { - SafeNativeMethods.EnableWindow(hwndOwner, true); + User32.EnableWindow(hwndOwner, true); } } @@ -3436,11 +3429,10 @@ private bool LocalModalMessageLoop(Form form) if (peeked) { - // If the component wants us to process the message, do it. - // The component manager hosts windows from many places. We must be sensitive - // to ansi / Unicode windows here. + // If the component wants us to process the message, do it. The component manager + // hosts windows from many places. We must be sensitive to ansi/Unicode windows here. // - if (msg.hwnd != IntPtr.Zero && SafeNativeMethods.IsWindowUnicode(new HandleRef(null, msg.hwnd))) + if (msg.hwnd != IntPtr.Zero && User32.IsWindowUnicode(msg.hwnd)) { unicodeWindow = true; if (!UnsafeNativeMethods.GetMessageW(ref msg, NativeMethods.NullHandleRef, 0, 0)) @@ -3622,11 +3614,9 @@ internal bool PreTranslateMessage(ref NativeMethods.MSG msg) { // See if this is a dialog message -- this is for handling any native dialogs that are launched from // winforms code. This can happen with ActiveX controls that launch dialogs specificially - // - // first, get the first top-level window in the hierarchy. - // - IntPtr hwndRoot = UnsafeNativeMethods.GetAncestor(new HandleRef(null, msg.hwnd), NativeMethods.GA_ROOT); + // First, get the first top-level window in the hierarchy. + IntPtr hwndRoot = User32.GetAncestor(msg.hwnd, User32.GetAncestorFlag.GA_ROOT); // if we got a valid HWND, then call IsDialogMessage on it. If that returns true, it's been processed // so we should return true to prevent Translate/Dispatch from being called. @@ -3678,13 +3668,13 @@ private void RevokeComponent() } /// - /// Sets the culture for this thread. + /// Sets the culture for this thread. /// internal void SetCulture(CultureInfo culture) { - if (culture != null && culture.LCID != SafeNativeMethods.GetThreadLocale()) + if (culture != null && culture.LCID != Kernel32.GetThreadLocale()) { - SafeNativeMethods.SetThreadLocale(culture.LCID); + Kernel32.SetThreadLocale(culture.LCID); } } @@ -3902,8 +3892,8 @@ bool UnsafeNativeMethods.IMsoComponent.FContinueMessageLoop(int reason, int pvLo // For focus wait, check to see if we are now the active application. // int pid; - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()), out pid); - if (pid == SafeNativeMethods.GetCurrentProcessId()) + User32.GetWindowThreadProcessId(User32.GetActiveWindow(), out pid); + if (pid == Kernel32.GetCurrentProcessId()) { continueLoop = false; } @@ -4005,7 +3995,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; // Message only windows are cheaper and have fewer issues than // full blown invisible windows. - cp.Parent = (IntPtr)NativeMethods.HWND_MESSAGE; + cp.Parent = User32.HWND_MESSAGE; return cp; } } @@ -4052,7 +4042,7 @@ protected override CreateParams CreateParams // Message only windows are cheaper and have fewer issues than // full blown invisible windows. - cp.Parent = (IntPtr)NativeMethods.HWND_MESSAGE; + cp.Parent = User32.HWND_MESSAGE; return cp; } } @@ -4085,7 +4075,7 @@ internal override void RemoveReflectChild() //messagepump is gone and then decide to clean them up. We should clean //up the parkingwindow in this case and a postmessage won't do it. //unused - int id = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, HandleInternal), out int lpdwProcessId); + int id = User32.GetWindowThreadProcessId(new HandleRef(this, HandleInternal), out int lpdwProcessId); ThreadContext ctx = Application.ThreadContext.FromId(id); //We only do this if the ThreadContext tells us that we are currently @@ -4107,7 +4097,7 @@ private void CheckDestroy() { if (childCount == 0) { - IntPtr hwndChild = UnsafeNativeMethods.GetWindow(new HandleRef(this, Handle), NativeMethods.GW_CHILD); + IntPtr hwndChild = User32.GetWindow(new HandleRef(this, Handle), User32.GetWindowOption.GW_CHILD); if (hwndChild == IntPtr.Zero) { DestroyHandle(); @@ -4131,7 +4121,7 @@ internal void ParkHandle(HandleRef handle) CreateHandle(); } - UnsafeNativeMethods.SetParent(handle, new HandleRef(this, Handle)); + User32.SetParent(handle, new HandleRef(this, Handle)); } /// @@ -4142,7 +4132,7 @@ internal void UnparkHandle(HandleRef handle) { if (IsHandleCreated) { - Debug.Assert(UnsafeNativeMethods.GetParent(handle) != Handle, "Always set the handle's parent to someone else before calling UnparkHandle"); + Debug.Assert(User32.GetParent(handle) != Handle, "Always set the handle's parent to someone else before calling UnparkHandle"); // If there are no child windows in this handle any longer, destroy the parking window. CheckDestroy(); } @@ -4191,19 +4181,20 @@ internal ThreadWindows(bool onlyWinForms) { windows = new IntPtr[16]; this.onlyWinForms = onlyWinForms; - UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(), - new NativeMethods.EnumThreadWindowsCallback(Callback), - NativeMethods.NullHandleRef); + var callback = new User32.EnumThreadWindowsCallback(Callback); + User32.EnumThreadWindows( + Kernel32.GetCurrentThreadId(), + callback, + IntPtr.Zero); + GC.KeepAlive(callback); } private bool Callback(IntPtr hWnd, IntPtr lparam) { - - // We only do visible and enabled windows. Also, we only do top level windows. + // We only do visible and enabled windows. Also, we only do top level windows. // Finally, we only include windows that are DNA windows, since other MSO components // will be responsible for disabling their own windows. - // - if (SafeNativeMethods.IsWindowVisible(new HandleRef(null, hWnd)) && SafeNativeMethods.IsWindowEnabled(new HandleRef(null, hWnd))) + if (User32.IsWindowVisible(hWnd) && User32.IsWindowEnabled(hWnd)) { bool add = true; @@ -4236,7 +4227,7 @@ internal void Dispose() for (int i = 0; i < windowCount; i++) { IntPtr hWnd = windows[i]; - if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWnd))) + if (User32.IsWindow(hWnd)) { Control c = Control.FromHandle(hWnd); if (c != null) @@ -4253,7 +4244,7 @@ internal void Enable(bool state) if (!onlyWinForms && !state) { - activeHwnd = UnsafeNativeMethods.GetActiveWindow(); + activeHwnd = User32.GetActiveWindow(); Control activatingControl = Application.ThreadContext.FromCurrent().ActivatingControl; if (activatingControl != null) { @@ -4261,7 +4252,7 @@ internal void Enable(bool state) } else { - focusedHwnd = UnsafeNativeMethods.GetFocus(); + focusedHwnd = User32.GetFocus(); } } @@ -4269,9 +4260,9 @@ internal void Enable(bool state) { IntPtr hWnd = windows[i]; Debug.WriteLineIf(CompModSwitches.MSOComponentManager.TraceInfo, "ComponentManager : Changing enabled on window: " + hWnd.ToString() + " : " + state.ToString()); - if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWnd))) + if (User32.IsWindow(hWnd)) { - SafeNativeMethods.EnableWindow(new HandleRef(null, hWnd), state); + User32.EnableWindow(hWnd, state); } } @@ -4285,14 +4276,14 @@ internal void Enable(bool state) // we are created with a TRUE for onlyWinForms. if (!onlyWinForms && state) { - if (activeHwnd != IntPtr.Zero && UnsafeNativeMethods.IsWindow(new HandleRef(null, activeHwnd))) + if (activeHwnd != IntPtr.Zero && User32.IsWindow(activeHwnd)) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(null, activeHwnd)); + User32.SetActiveWindow(activeHwnd); } - if (focusedHwnd != IntPtr.Zero && UnsafeNativeMethods.IsWindow(new HandleRef(null, focusedHwnd))) + if (focusedHwnd != IntPtr.Zero && User32.IsWindow(focusedHwnd)) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, focusedHwnd)); + User32.SetFocus(focusedHwnd); } } } @@ -4322,7 +4313,7 @@ public void DisableThreadWindows(bool disable, bool onlyWinForms) // get ahold of the parenting control // - IntPtr parentHandle = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, MainForm.Handle), NativeMethods.GWL_HWNDPARENT); + IntPtr parentHandle = User32.GetWindowLong(new HandleRef(this, MainForm.Handle), User32.WindowLong.GWL_HWNDPARENT); parentControl = Control.FromHandle(parentHandle); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs b/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs index 2e93bc35da5..fa4773cc72b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs @@ -101,7 +101,7 @@ internal class AxFlags private const int OLEIVERB_PROPERTIES = -7; private const int OLEIVERB_PRIMARY = 0; - private readonly int REGMSG_MSG = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_subclassCheck"); + private readonly int REGMSG_MSG = User32.RegisterWindowMessageW(Application.WindowMessagesVersion + "_subclassCheck"); private const int REGMSG_RETVAL = 123; private static int logPixelsX = -1; @@ -298,7 +298,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; if (axState[fOwnWindow] && IsUserMode()) { - cp.Style &= (~NativeMethods.WS_VISIBLE); + cp.Style &= (~User32.WindowStyle.WS_VISIBLE); } return cp; } @@ -1153,7 +1153,7 @@ protected override void OnLostFocus(EventArgs e) if (uiDeactivate && IsHandleCreated) { - uiDeactivate = !UnsafeNativeMethods.IsChild(new HandleRef(this, GetHandleNoCreate()), new HandleRef(null, hwndFocus)); + uiDeactivate = !User32.IsChild(new HandleRef(this, GetHandleNoCreate()), hwndFocus); } base.OnLostFocus(e); @@ -1462,7 +1462,7 @@ private bool CheckSubclassing() } IntPtr handle = Handle; - IntPtr currentWndproc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC); + IntPtr currentWndproc = User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC); if (currentWndproc == wndprocAddr) { return true; @@ -1478,7 +1478,7 @@ private bool CheckSubclassing() // we need to resubclass outselves now... Debug.Assert(!OwnWindow(), "why are we here if we own our window?"); WindowReleaseHandle(); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC, new HandleRef(this, currentWndproc)); + User32.SetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC, new HandleRef(this, currentWndproc)); WindowAssignHandle(handle, axState[assignUniqueID]); InformOfNewHandle(); axState[manualUpdate] = true; @@ -2023,7 +2023,7 @@ protected internal override bool ProcessMnemonic(char charCode) message = WindowMessages.WM_SYSKEYDOWN, wParam = (IntPtr)char.ToUpper(charCode, CultureInfo.CurrentCulture), lParam = (IntPtr)0x20180001, - time = SafeNativeMethods.GetTickCount() + time = Kernel32.GetTickCount() }; UnsafeNativeMethods.GetCursorPos(out Point p); msg.pt = p; @@ -2575,9 +2575,6 @@ private void CreateWithLicense(string license, Guid clsid) private void CreateInstance() { Debug.Assert(instance == null, "instance must be null"); - //Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "before created "+Windows.GetCurrentThreadId()); - //Debug.WriteStackTraceIf("AxHTrace"); - //checkThreadingModel(); try { instance = CreateInstanceCore(clsid); @@ -2586,7 +2583,8 @@ private void CreateInstance() catch (ExternalException e) { if (e.ErrorCode == unchecked((int)0x80040112)) - { // CLASS_E_NOTLICENSED + { + // CLASS_E_NOTLICENSED throw new LicenseException(GetType(), this, SR.AXNoLicenseToUse); } throw; @@ -2699,7 +2697,7 @@ public void InvokeEditMode() AddSelectionHandler(); editMode = EDITM_HOST; SetSelectionStyle(2); - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); try { UiActivate(); @@ -3641,8 +3639,8 @@ private void DetachAndForward(ref Message m) DetachWindow(); if (handle != IntPtr.Zero) { - IntPtr wndProc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC); - m.Result = UnsafeNativeMethods.CallWindowProc(wndProc, handle, m.Msg, m.WParam, m.LParam); + IntPtr wndProc = User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC); + m.Result = User32.CallWindowProcW(wndProc, handle, m.Msg, m.WParam, m.LParam); } } @@ -3651,15 +3649,6 @@ private void DetachWindow() if (IsHandleCreated) { OnHandleDestroyed(EventArgs.Empty); - for (Control c = this; c != null; c = c.ParentInternal) - { - /* NOT NEEDED - if (c.GetAxState(STATE_HANDLEHOOK)) { - ((IHandleHook)c.Site.GetService(IHandleHook.class)).OnDestroyHandle(GetHandle()); - break; - } - */ - } WindowReleaseHandle(); } } @@ -3667,19 +3656,7 @@ private void DetachWindow() private void InformOfNewHandle() { Debug.Assert(IsHandleCreated, "we got to have a handle to be here..."); - for (Control c = this; c != null; c = c.ParentInternal) - { - /* NOT NEEDED - if (c.GetAxState(STATE_HANDLEHOOK)) { - ((IHandleHook)c.Site.GetService(IHandleHook.class)).OnCreateHandle(GetHandle()); - break; - } - */ - } - wndprocAddr = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_WNDPROC); - /* NOT NEEDED - SetAxState(STATE_CREATENOTIFIED, true); - */ + wndprocAddr = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_WNDPROC); } private void AttachWindow(IntPtr hwnd) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs index d176cd03d7a..5e605291984 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ButtonBase.cs @@ -195,7 +195,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; if (!OwnerDraw) { - cp.ExStyle &= ~NativeMethods.WS_EX_RIGHT; // WS_EX_RIGHT overrides the BS_XXXX alignment styles + cp.ExStyle &= ~User32.WindowStyle.WS_EX_RIGHT; // WS_EX_RIGHT overrides the BS_XXXX alignment styles cp.Style |= NativeMethods.BS_MULTILINE; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/CheckedListBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/CheckedListBox.cs index ef614f3707c..2e92745f81a 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/CheckedListBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/CheckedListBox.cs @@ -75,8 +75,8 @@ public class CheckedListBox : ListBox static CheckedListBox() { - LBC_GETCHECKSTATE = SafeNativeMethods.RegisterWindowMessage("LBC_GETCHECKSTATE"); - LBC_SETCHECKSTATE = SafeNativeMethods.RegisterWindowMessage("LBC_SETCHECKSTATE"); + LBC_GETCHECKSTATE = User32.RegisterWindowMessageW("LBC_GETCHECKSTATE"); + LBC_SETCHECKSTATE = User32.RegisterWindowMessageW("LBC_SETCHECKSTATE"); } /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ColorDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ColorDialog.cs index ac08d65cda8..cf4c3656c24 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ColorDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ColorDialog.cs @@ -240,7 +240,7 @@ private void ResetColor() protected override bool RunDialog(IntPtr hwndOwner) { - NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(HookProc); + User32.WndProc hookProcPtr = new User32.WndProc(HookProc); NativeMethods.CHOOSECOLOR cc = new NativeMethods.CHOOSECOLOR(); IntPtr custColorPtr = Marshal.AllocCoTaskMem(64); try diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs index fbb10c47ce2..606999a7660 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ComboBox.cs @@ -401,8 +401,8 @@ protected override CreateParams CreateParams { CreateParams cp = base.CreateParams; cp.ClassName = "COMBOBOX"; - cp.Style |= NativeMethods.WS_VSCROLL | NativeMethods.CBS_HASSTRINGS | NativeMethods.CBS_AUTOHSCROLL; - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.Style |= User32.WindowStyle.WS_VSCROLL | NativeMethods.CBS_HASSTRINGS | NativeMethods.CBS_AUTOHSCROLL; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; if (!integralHeight) { cp.Style |= NativeMethods.CBS_NOINTEGRALHEIGHT; @@ -668,7 +668,7 @@ public override bool Focused return true; } - IntPtr focus = UnsafeNativeMethods.GetFocus(); + IntPtr focus = User32.GetFocus(); return focus != IntPtr.Zero && ((childEdit != null && focus == childEdit.Handle) || (childListBox != null && focus == childListBox.Handle)); } } @@ -1653,12 +1653,12 @@ internal Point EditToComboboxMapping(Message m) // Get the Combox Rect ... // RECT comboRectMid = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref comboRectMid); + User32.GetWindowRect(new HandleRef(this, Handle), ref comboRectMid); // //Get the Edit Rectangle... // RECT editRectMid = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, childEdit.Handle), ref editRectMid); + User32.GetWindowRect(new HandleRef(this, childEdit.Handle), ref editRectMid); //get the delta int comboXMid = NativeMethods.Util.SignedLOWORD(m.LParam) + (editRectMid.left - comboRectMid.left); @@ -1909,8 +1909,8 @@ private void ChildWndProc(ref Message m) case WindowMessages.WM_LBUTTONUP: // Get the mouse location // - RECT r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + var r = new RECT(); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); Rectangle ClientRect = new Rectangle(r.left, r.top, r.right - r.left, r.bottom - r.top); // Get the mouse location // @@ -2070,7 +2070,7 @@ private void OnMouseEnterInternal(EventArgs args) private void OnMouseLeaveInternal(EventArgs args) { RECT rect = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref rect); + User32.GetWindowRect(new HandleRef(this, Handle), ref rect); Rectangle Rect = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); Point p = MousePosition; if (!Rect.Contains(p)) @@ -2536,7 +2536,7 @@ protected override void OnHandleCreated(EventArgs e) if (ok && DropDownStyle != ComboBoxStyle.DropDownList) { - IntPtr hwnd = UnsafeNativeMethods.GetWindow(new HandleRef(this, Handle), NativeMethods.GW_CHILD); + IntPtr hwnd = User32.GetWindow(new HandleRef(this, Handle), User32.GetWindowOption.GW_CHILD); if (hwnd != IntPtr.Zero) { @@ -2549,7 +2549,7 @@ protected override void OnHandleCreated(EventArgs e) // get the edits hwnd... // - hwnd = UnsafeNativeMethods.GetWindow(new HandleRef(this, hwnd), NativeMethods.GW_HWNDNEXT); + hwnd = User32.GetWindow(new HandleRef(this, hwnd), User32.GetWindowOption.GW_HWNDNEXT); } childEdit = new ComboBoxChildNativeWindow(this, ChildWindowType.Edit); @@ -3530,7 +3530,7 @@ private void UpdateDropDownHeight() { if (dropDownHandle != IntPtr.Zero) { - //Now use the DropDownHeight property instead of calculating the Height... + // Now use the DropDownHeight property instead of calculating the Height... int height = DropDownHeight; if (height == DefaultDropDownHeight) { @@ -3538,8 +3538,15 @@ private void UpdateDropDownHeight() int count = Math.Min(Math.Max(itemCount, 1), maxDropDownItems); height = (ItemHeight * count + 2); } - SafeNativeMethods.SetWindowPos(new HandleRef(this, dropDownHandle), NativeMethods.NullHandleRef, 0, 0, DropDownWidth, height, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOZORDER); + + User32.SetWindowPos( + new HandleRef(this, dropDownHandle), + IntPtr.Zero, + 0, + 0, + DropDownWidth, + height, + User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOZORDER); } } @@ -3897,8 +3904,8 @@ protected override void WndProc(ref Message m) case WindowMessages.WM_LBUTTONUP: // Get the mouse location // - RECT r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + var r = new RECT(); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); Rectangle ClientRect = new Rectangle(r.left, r.top, r.right - r.left, r.bottom - r.top); int x = NativeMethods.Util.SignedLOWORD(m.LParam); @@ -6143,13 +6150,7 @@ private static bool RegisterACWindowRecursive(IntPtr handle, IntPtr lparam) return true; } - internal bool Visible - { - get - { - return SafeNativeMethods.IsWindowVisible(new HandleRef(this, Handle)); - } - } + internal bool Visible => User32.IsWindowVisible(new HandleRef(this, Handle)); static internal bool AutoCompleteActive { @@ -6238,7 +6239,6 @@ internal static void ClearNullACWindows() /// private class AutoCompleteDropDownFinder { - private const int MaxClassName = 256; private const string AutoCompleteClassName = "Auto-Suggest Dropdown"; bool shouldSubClass = false; //nonstatic @@ -6254,30 +6254,28 @@ internal void FindDropDowns(bool subclass) //generating a before snapshot -- lets lose the null handles ACNativeWindow.ClearNullACWindows(); } + // Look for a popped up dropdown shouldSubClass = subclass; - UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(), new NativeMethods.EnumThreadWindowsCallback(Callback), new HandleRef(null, IntPtr.Zero)); + var callback = new User32.EnumThreadWindowsCallback(Callback); + User32.EnumThreadWindows( + Kernel32.GetCurrentThreadId(), + callback, + IntPtr.Zero); + + GC.KeepAlive(callback); } private bool Callback(IntPtr hWnd, IntPtr lParam) { - HandleRef hRef = new HandleRef(null, hWnd); - // Check class name and see if it's visible - if (GetClassName(hRef) == AutoCompleteClassName) + if (User32.GetClassName(hWnd) == AutoCompleteClassName) { - ACNativeWindow.RegisterACWindow(hRef.Handle, shouldSubClass); + ACNativeWindow.RegisterACWindow(hWnd, shouldSubClass); } return true; } - - static string GetClassName(HandleRef hRef) - { - StringBuilder sb = new StringBuilder(MaxClassName); - UnsafeNativeMethods.GetClassName(hRef, sb, MaxClassName); - return sb.ToString(); - } } private FlatComboAdapter FlatComboBoxAdapter diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/CommonDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/CommonDialog.cs index e52c23a0171..1b1a4a25772 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/CommonDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/CommonDialog.cs @@ -65,7 +65,7 @@ protected virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lp // Under some circumstances, the dialog does not initially focus on any // control. We fix that by explicitly setting focus ourselves. _defaultControlHwnd = wparam; - UnsafeNativeMethods.SetFocus(new HandleRef(null, wparam)); + User32.SetFocus(wparam); } else if (msg == WindowMessages.WM_SETFOCUS) { @@ -76,7 +76,7 @@ protected virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lp // If the dialog box gets focus, bounce it to the default control. // So we post a message back to ourselves to wait for the focus change // then push it to the default control. - UnsafeNativeMethods.SetFocus(new HandleRef(this, _defaultControlHwnd)); + User32.SetFocus(new HandleRef(this, _defaultControlHwnd)); } return IntPtr.Zero; @@ -89,13 +89,19 @@ protected virtual IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lp /// private protected static void MoveToScreenCenter(IntPtr hWnd) { - RECT r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(null, hWnd), ref r); + var r = new RECT(); + User32.GetWindowRect(hWnd, ref r); Rectangle screen = Screen.GetWorkingArea(Control.MousePosition); int x = screen.X + (screen.Width - r.right + r.left) / 2; int y = screen.Y + (screen.Height - r.bottom + r.top) / 3; - SafeNativeMethods.SetWindowPos(new HandleRef(null, hWnd), NativeMethods.NullHandleRef, x, y, 0, 0, NativeMethods.SWP_NOSIZE | - NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + hWnd, + IntPtr.Zero, + x, + y, + 0, + 0, + User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOZORDER | User32.WindowPosition.SWP_NOACTIVATE); } /// @@ -134,7 +140,7 @@ protected virtual IntPtr OwnerWndProc(IntPtr hWnd, int msg, IntPtr wparam, IntPt return IntPtr.Zero; } - return UnsafeNativeMethods.CallWindowProc(_defOwnerWndProc, hWnd, msg, wparam, lparam); + return User32.CallWindowProcW(_defOwnerWndProc, hWnd, msg, wparam, lparam); } @@ -179,7 +185,7 @@ public DialogResult ShowDialog(IWin32Window owner) if (hwndOwner == IntPtr.Zero) { - hwndOwner = UnsafeNativeMethods.GetActiveWindow(); + hwndOwner = User32.GetActiveWindow(); } if (hwndOwner == IntPtr.Zero) @@ -192,18 +198,17 @@ public DialogResult ShowDialog(IWin32Window owner) if (s_helpMsg == 0) { - s_helpMsg = SafeNativeMethods.RegisterWindowMessage("commdlg_help"); + s_helpMsg = User32.RegisterWindowMessageW("commdlg_help"); } - NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(OwnerWndProc); + User32.WndProc ownerProc = new User32.WndProc(OwnerWndProc); _hookedWndProc = Marshal.GetFunctionPointerForDelegate(ownerProc); Debug.Assert(IntPtr.Zero == _defOwnerWndProc, "The previous subclass wasn't properly cleaned up"); IntPtr userCookie = IntPtr.Zero; try { - // UnsafeNativeMethods.[Get|Set]WindowLong is smart enough to call SetWindowLongPtr on 64-bit OS - _defOwnerWndProc = UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc); + _defOwnerWndProc = User32.SetWindowLong(new HandleRef(this, hwndOwner), User32.WindowLong.GWL_WNDPROC, ownerProc); if (Application.UseVisualStyles) { @@ -222,10 +227,10 @@ public DialogResult ShowDialog(IWin32Window owner) } finally { - IntPtr currentSubClass = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC); + IntPtr currentSubClass = User32.GetWindowLong(new HandleRef(this, hwndOwner), User32.WindowLong.GWL_WNDPROC); if (_defOwnerWndProc != IntPtr.Zero || currentSubClass != _hookedWndProc) { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(this, _defOwnerWndProc)); + User32.SetWindowLong(new HandleRef(this, hwndOwner), User32.WindowLong.GWL_WNDPROC, new HandleRef(this, _defOwnerWndProc)); } UnsafeNativeMethods.ThemingScope.Deactivate(userCookie); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentManagerBroker.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentManagerBroker.cs index e729e95c903..7c1bc628c0b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentManagerBroker.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentManagerBroker.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Globalization; +using static Interop; namespace System.Windows.Forms { @@ -94,7 +95,7 @@ internal sealed class ComponentManagerBroker : MarshalByRefObject /// static ComponentManagerBroker() { - int pid = SafeNativeMethods.GetCurrentProcessId(); + int pid = Kernel32.GetCurrentProcessId(); _syncObject = new object(); _remoteObjectName = string.Format(CultureInfo.CurrentCulture, "ComponentManagerBroker.{0}.{1:X}", Application.WindowsFormsVersion, pid); } @@ -233,7 +234,7 @@ internal ComponentManagerProxy(ComponentManagerBroker broker, UnsafeNativeMethod { _broker = broker; _original = original; - _creationThread = SafeNativeMethods.GetCurrentThreadId(); + _creationThread = Kernel32.GetCurrentThreadId(); _refCount = 0; } @@ -493,7 +494,7 @@ bool UnsafeNativeMethods.IMsoComponentManager.FRevokeComponent(IntPtr dwComponen return false; } - if (_refCount == 1 && SafeNativeMethods.GetCurrentThreadId() == _creationThread) + if (_refCount == 1 && Kernel32.GetCurrentThreadId() == _creationThread) { if (!RevokeComponent()) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyBuilderUITypeEditor.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyBuilderUITypeEditor.cs index 8feaaa0682c..e580fc4c2f9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyBuilderUITypeEditor.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyBuilderUITypeEditor.cs @@ -8,6 +8,7 @@ using System.Globalization; using System.Runtime.InteropServices; using System.Windows.Forms.Design; +using static Interop; namespace System.Windows.Forms.ComponentModel.Com2Interop { @@ -33,7 +34,7 @@ public Com2PropertyBuilderUITypeEditor(Com2PropertyDescriptor pd, string guidStr /// public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { - IntPtr parentHandle = (IntPtr)UnsafeNativeMethods.GetFocus(); + IntPtr parentHandle = (IntPtr)User32.GetFocus(); IUIService uiSvc = (IUIService)provider.GetService(typeof(IUIService)); if (uiSvc != null) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyPageUITypeConverter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyPageUITypeConverter.cs index fe059d135e8..9e0641c9eaf 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyPageUITypeConverter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ComponentModel/COM2Interop/COM2PropertyPageUITypeConverter.cs @@ -6,6 +6,7 @@ using System.Drawing.Design; using System.Runtime.InteropServices; using System.Windows.Forms.Design; +using static Interop; namespace System.Windows.Forms.ComponentModel.Com2Interop { @@ -29,7 +30,7 @@ public Com2PropertyPageUITypeEditor(Com2PropertyDescriptor pd, Guid guid, UIType /// public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { - IntPtr hWndParent = UnsafeNativeMethods.GetFocus(); // Windows.GetForegroundWindow + IntPtr hWndParent = User32.GetFocus(); try { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ContainerControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ContainerControl.cs index c28ca717986..586c5ee4c47 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ContainerControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ContainerControl.cs @@ -292,7 +292,7 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTROLPARENT; return cp; } } @@ -644,10 +644,10 @@ internal void FocusActiveControlInternal() if (_activeControl != null && _activeControl.Visible) { // Avoid focus loops, especially with ComboBoxes. - IntPtr focusHandle = UnsafeNativeMethods.GetFocus(); + IntPtr focusHandle = User32.GetFocus(); if (focusHandle == IntPtr.Zero || Control.FromChildHandle(focusHandle) != _activeControl) { - UnsafeNativeMethods.SetFocus(new HandleRef(_activeControl, _activeControl.Handle)); + User32.SetFocus(new HandleRef(_activeControl, _activeControl.Handle)); } } else @@ -669,7 +669,7 @@ internal void FocusActiveControlInternal() if (cc != null && cc.Visible) { - UnsafeNativeMethods.SetFocus(new HandleRef(cc, cc.Handle)); + User32.SetFocus(new HandleRef(cc, cc.Handle)); } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.ActiveXImpl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.ActiveXImpl.cs index 7b7e78027cc..18aa81665ab 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.ActiveXImpl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.ActiveXImpl.cs @@ -908,7 +908,7 @@ internal void InPlaceActivate(int verb) // If it doesn't, that means that the host // won't reflect messages back to us. HWNDParent = hwndParent; - UnsafeNativeMethods.SetParent(new HandleRef(_control, _control.Handle), new HandleRef(null, hwndParent)); + User32.SetParent(new HandleRef(_control, _control.Handle), hwndParent); // Now create our handle if it hasn't already been done. _control.CreateControl(); @@ -2193,7 +2193,7 @@ internal void SetObjectRects(NativeMethods.COMRECT lprcPosRect, NativeMethods.CO { // Offset the rectangle back to client coordinates RECT rcIntersect = intersect; - IntPtr hWndParent = UnsafeNativeMethods.GetParent(new HandleRef(_control, _control.Handle)); + IntPtr hWndParent = User32.GetParent(new HandleRef(_control, _control.Handle)); Debug.WriteLineIf(CompModSwitches.ActiveX.TraceInfo, "Old Intersect: " + new Rectangle(rcIntersect.left, rcIntersect.top, rcIntersect.right - rcIntersect.left, rcIntersect.bottom - rcIntersect.top)); Debug.WriteLineIf(CompModSwitches.ActiveX.TraceInfo, "New Control Bounds: " + posRect); @@ -2224,11 +2224,10 @@ internal void SetObjectRects(NativeMethods.COMRECT lprcPosRect, NativeMethods.CO finalClipRegion = MergeRegion(rgn); } - UnsafeNativeMethods.SetWindowRgn(new HandleRef(_control, _control.Handle), new HandleRef(this, finalClipRegion), SafeNativeMethods.IsWindowVisible(new HandleRef(_control, _control.Handle))); + User32.SetWindowRgn(new HandleRef(_control, _control.Handle), new HandleRef(this, finalClipRegion), User32.IsWindowVisible(new HandleRef(_control, _control.Handle))); } - // Yuck. Forms^3 uses transparent overlay windows that appear to cause - // painting artifacts. Flicker like a banshee. + // Form uses transparent overlay windows that appear to cause painting artifacts and flicker. _control.Invalidate(); } @@ -2296,7 +2295,7 @@ internal int TranslateAccelerator(ref NativeMethods.MSG lpmsg) // Someone returned true from IsInputKey or IsInputChar UnsafeNativeMethods.TranslateMessage(ref lpmsg); - if (SafeNativeMethods.IsWindowUnicode(new HandleRef(null, lpmsg.hwnd))) + if (User32.IsWindowUnicode(lpmsg.hwnd)) { UnsafeNativeMethods.DispatchMessageW(ref lpmsg); } @@ -2395,15 +2394,14 @@ internal void Unadvise(int dwConnection) /// /// Notifies our site that we have changed our size and location. /// - internal void UpdateBounds(ref int x, ref int y, ref int width, ref int height, int flags) + internal void UpdateBounds(ref int x, ref int y, ref int width, ref int height, User32.WindowPosition flags) { if (!_activeXState[s_adjustingRect] && _activeXState[s_inPlaceVisible]) { if (_clientSite is UnsafeNativeMethods.IOleInPlaceSite ioleClientSite) { - NativeMethods.COMRECT rc = new NativeMethods.COMRECT(); - - if ((flags & NativeMethods.SWP_NOMOVE) != 0) + var rc = new NativeMethods.COMRECT(); + if ((flags & User32.WindowPosition.SWP_NOMOVE) != 0) { rc.left = _control.Left; rc.top = _control.Top; @@ -2414,7 +2412,7 @@ internal void UpdateBounds(ref int x, ref int y, ref int width, ref int height, rc.top = y; } - if ((flags & NativeMethods.SWP_NOSIZE) != 0) + if ((flags & User32.WindowPosition.SWP_NOSIZE) != 0) { rc.right = rc.left + _control.Width; rc.bottom = rc.top + _control.Height; @@ -2440,12 +2438,12 @@ internal void UpdateBounds(ref int x, ref int y, ref int width, ref int height, } // On output, the new bounds will be reflected in rc - if ((flags & NativeMethods.SWP_NOMOVE) == 0) + if ((flags & User32.WindowPosition.SWP_NOMOVE) == 0) { x = rc.left; y = rc.top; } - if ((flags & NativeMethods.SWP_NOSIZE) == 0) + if ((flags & User32.WindowPosition.SWP_NOSIZE) == 0) { width = rc.right - rc.left; height = rc.bottom - rc.top; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs index 13492d9709d..1057ba27675 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.Ime.cs @@ -289,7 +289,7 @@ protected virtual ImeMode ImeModeBase } else if (ContainsFocus) { - ctl = FromChildHandle(UnsafeNativeMethods.GetFocus()); + ctl = FromChildHandle(User32.GetFocus()); } if (ctl != null && ctl.CanEnableIme) @@ -418,7 +418,7 @@ protected static ImeMode PropagatingImeMode Debug.WriteLineIf(CompModSwitches.ImeMode.Level >= TraceLevel.Verbose, "Initializing PropagatingImeMode"); ImeMode imeMode = ImeMode.Inherit; - IntPtr focusHandle = UnsafeNativeMethods.GetFocus(); + IntPtr focusHandle = User32.GetFocus(); if (focusHandle != IntPtr.Zero) { @@ -428,8 +428,7 @@ protected static ImeMode PropagatingImeMode // this is the case of a disabled winforms control hosted in a non-Form shell. if (imeMode == ImeMode.Disable) { - focusHandle = UnsafeNativeMethods.GetAncestor(new HandleRef(null, focusHandle), NativeMethods.GA_ROOT); - + focusHandle = User32.GetAncestor(focusHandle, User32.GetAncestorFlag.GA_ROOT); if (focusHandle != IntPtr.Zero) { imeMode = ImeContext.GetImeMode(focusHandle); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs index 336efab217e..a8c58a99132 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs @@ -134,8 +134,8 @@ private protected void TraceCanProcessMnemonic() private static readonly BooleanSwitch s_bufferDisabled = new BooleanSwitch("BufferDisabled", "Makes double buffered controls non-double buffered"); #endif - private static readonly int WM_GETCONTROLNAME = SafeNativeMethods.RegisterWindowMessage("WM_GETCONTROLNAME"); - private static readonly int WM_GETCONTROLTYPE = SafeNativeMethods.RegisterWindowMessage("WM_GETCONTROLTYPE"); + private static readonly int WM_GETCONTROLNAME = User32.RegisterWindowMessageW("WM_GETCONTROLNAME"); + private static readonly int WM_GETCONTROLTYPE = User32.RegisterWindowMessageW("WM_GETCONTROLTYPE"); internal const int STATE_CREATED = 0x00000001; internal const int STATE_VISIBLE = 0x00000002; @@ -536,16 +536,15 @@ public Control(Control parent, string text, int left, int top, int width, int he /// /// Gets control Dpi awareness context value. /// - internal DpiAwarenessContext DpiAwarenessContext => _window.DpiAwarenessContext; + internal User32.DpiAwarenessContext DpiAwarenessContext => _window.DpiAwarenessContext; /// /// The Accessibility Object for this Control /// - [ - Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced), - DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), - SRDescription(nameof(SR.ControlAccessibilityObjectDescr)) - ] + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Advanced)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + [SRDescription(nameof(SR.ControlAccessibilityObjectDescr))] public AccessibleObject AccessibilityObject { get @@ -1267,8 +1266,9 @@ public bool CanFocus { return false; } - bool visible = SafeNativeMethods.IsWindowVisible(new HandleRef(_window, Handle)); - bool enabled = SafeNativeMethods.IsWindowEnabled(new HandleRef(_window, Handle)); + + bool visible = User32.IsWindowVisible(new HandleRef(_window, Handle)); + bool enabled = User32.IsWindowEnabled(new HandleRef(_window, Handle)); return (visible && enabled); } } @@ -1309,18 +1309,18 @@ public bool Capture internal bool CaptureInternal { - get => IsHandleCreated && UnsafeNativeMethods.GetCapture() == Handle; + get => IsHandleCreated && User32.GetCapture() == Handle; set { if (CaptureInternal != value) { if (value) { - UnsafeNativeMethods.SetCapture(new HandleRef(this, Handle)); + User32.SetCapture(new HandleRef(this, Handle)); } else { - SafeNativeMethods.ReleaseCapture(); + User32.ReleaseCapture(); } } } @@ -1481,7 +1481,7 @@ public bool ContainsFocus return false; } - IntPtr focusHwnd = UnsafeNativeMethods.GetFocus(); + IntPtr focusHwnd = User32.GetFocus(); if (focusHwnd == IntPtr.Zero) { @@ -1493,7 +1493,7 @@ public bool ContainsFocus return true; } - if (UnsafeNativeMethods.IsChild(new HandleRef(this, Handle), new HandleRef(this, focusHwnd))) + if (User32.IsChild(new HandleRef(this, Handle), new HandleRef(this, focusHwnd))) { return true; } @@ -1675,12 +1675,12 @@ protected virtual CreateParams CreateParams cp.Width = _width; cp.Height = _height; - cp.Style = NativeMethods.WS_CLIPCHILDREN; + cp.Style = User32.WindowStyle.WS_CLIPCHILDREN; if (GetStyle(ControlStyles.ContainerControl)) { - cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTROLPARENT; } - cp.ClassStyle = (int)NativeMethods.ClassStyle.CS_DBLCLKS; + cp.ClassStyle = (int)User32.ClassStyle.CS_DBLCLKS; if ((_state & STATE_TOPLEVEL) == 0) { @@ -1688,7 +1688,7 @@ protected virtual CreateParams CreateParams // parking form if cp.parent == 0. // cp.Parent = _parent == null ? IntPtr.Zero : _parent.InternalHandle; - cp.Style |= NativeMethods.WS_CHILD | NativeMethods.WS_CLIPSIBLINGS; + cp.Style |= User32.WindowStyle.WS_CHILD | User32.WindowStyle.WS_CLIPSIBLINGS; } else { @@ -1697,19 +1697,19 @@ protected virtual CreateParams CreateParams if ((_state & STATE_TABSTOP) != 0) { - cp.Style |= NativeMethods.WS_TABSTOP; + cp.Style |= User32.WindowStyle.WS_TABSTOP; } if ((_state & STATE_VISIBLE) != 0) { - cp.Style |= NativeMethods.WS_VISIBLE; + cp.Style |= User32.WindowStyle.WS_VISIBLE; } // Unlike Visible, Windows doesn't correctly inherit disabledness from its parent -- an enabled child // of a disabled parent will look enabled but not get mouse events if (!Enabled) { - cp.Style |= NativeMethods.WS_DISABLED; + cp.Style |= User32.WindowStyle.WS_DISABLED; } // If we are being hosted as an Ax control, try to prevent the parking window @@ -1722,9 +1722,9 @@ protected virtual CreateParams CreateParams // Set Rtl bits if (RightToLeft == RightToLeft.Yes) { - cp.ExStyle |= NativeMethods.WS_EX_RTLREADING; - cp.ExStyle |= NativeMethods.WS_EX_RIGHT; - cp.ExStyle |= NativeMethods.WS_EX_LEFTSCROLLBAR; + cp.ExStyle |= User32.WindowStyle.WS_EX_RTLREADING; + cp.ExStyle |= User32.WindowStyle.WS_EX_RIGHT; + cp.ExStyle |= User32.WindowStyle.WS_EX_LEFTSCROLLBAR; } return cp; @@ -1834,8 +1834,8 @@ internal bool IsCurrentlyBeingScaled /// created yet, this method will return the current thread's ID. /// internal int CreateThreadId => IsHandleCreated - ? SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, Handle), out int _) - : SafeNativeMethods.GetCurrentThreadId(); + ? User32.GetWindowThreadProcessId(new HandleRef(this, Handle), out int _) + : Kernel32.GetCurrentThreadId(); /// /// Retrieves the cursor that will be displayed when the mouse is over this @@ -1900,9 +1900,9 @@ public virtual Cursor Cursor // This includes the case where the mouse is over one of our children var r = new RECT(); UnsafeNativeMethods.GetCursorPos(out Point p); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); - if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || UnsafeNativeMethods.GetCapture() == Handle) + if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || User32.GetCapture() == Handle) { SendMessage(WindowMessages.WM_SETCURSOR, Handle, (IntPtr)NativeMethods.HTCLIENT); } @@ -2231,7 +2231,7 @@ public event EventHandler EnabledChanged SRDescription(nameof(SR.ControlFocusedDescr)) ] public virtual bool Focused - => IsHandleCreated && UnsafeNativeMethods.GetFocus() == Handle; + => IsHandleCreated && User32.GetFocus() == Handle; /// /// Retrieves the current font for this control. This will be the font used @@ -2695,30 +2695,21 @@ internal bool HostedInWin32DialogManager } else { - IntPtr parentHandle = UnsafeNativeMethods.GetParent(new HandleRef(this, Handle)); + IntPtr parentHandle = User32.GetParent(new HandleRef(this, Handle)); IntPtr lastParentHandle = parentHandle; - StringBuilder sb = new StringBuilder(32); - SetState(STATE_HOSTEDINDIALOG, false); while (parentHandle != IntPtr.Zero) { - int len = UnsafeNativeMethods.GetClassName(new HandleRef(null, lastParentHandle), null, 0); - if (len > sb.Capacity) - { - sb.Capacity = len + 5; - } - UnsafeNativeMethods.GetClassName(new HandleRef(null, lastParentHandle), sb, sb.Capacity); - - if (sb.ToString() == "#32770") + if (User32.GetClassName(lastParentHandle) == "#32770") { SetState(STATE_HOSTEDINDIALOG, true); break; } lastParentHandle = parentHandle; - parentHandle = UnsafeNativeMethods.GetParent(new HandleRef(null, parentHandle)); + parentHandle = User32.GetParent(parentHandle); } } @@ -2766,7 +2757,7 @@ internal bool IsWindowObscured } } - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref temp); + User32.GetWindowRect(new HandleRef(this, Handle), ref temp); working = new Region(Rectangle.FromLTRB(temp.left, temp.top, temp.right, temp.bottom)); try @@ -2784,14 +2775,14 @@ internal bool IsWindowObscured } for (prev = start; - (next = UnsafeNativeMethods.GetWindow(new HandleRef(null, prev), NativeMethods.GW_HWNDPREV)) != IntPtr.Zero; + (next = User32.GetWindow(new HandleRef(null, prev), User32.GetWindowOption.GW_HWNDPREV)) != IntPtr.Zero; prev = next) { - UnsafeNativeMethods.GetWindowRect(new HandleRef(null, next), ref temp); + User32.GetWindowRect(next, ref temp); Rectangle current = Rectangle.FromLTRB(temp.left, temp.top, temp.right, temp.bottom); - if (SafeNativeMethods.IsWindowVisible(new HandleRef(null, next))) + if (User32.IsWindowVisible(next)) { working.Exclude(current); } @@ -2864,9 +2855,8 @@ public bool InvokeRequired hwnd = new HandleRef(marshalingControl, marshalingControl.Handle); } - int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(hwnd, out int pid); - int currentThread = SafeNativeMethods.GetCurrentThreadId(); - return (hwndThread != currentThread); + int hwndThread = User32.GetWindowThreadProcessId(hwnd, out int pid); + return hwndThread != Kernel32.GetCurrentThreadId(); } } } @@ -2923,7 +2913,7 @@ public bool IsMirrored if (!IsHandleCreated) { CreateParams cp = CreateParams; - SetState(STATE_MIRRORED, (cp.ExStyle & NativeMethods.WS_EX_LAYOUTRTL) != 0); + SetState(STATE_MIRRORED, (cp.ExStyle & User32.WindowStyle.WS_EX_LAYOUTRTL) != 0); } return GetState(STATE_MIRRORED); } @@ -3318,7 +3308,7 @@ public Region Region regionHandle = ActiveXMergeRegion(regionHandle); } - if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(this, regionHandle), SafeNativeMethods.IsWindowVisible(new HandleRef(this, Handle))) != 0) + if (User32.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(this, regionHandle), User32.IsWindowVisible(new HandleRef(this, Handle))) != 0) { //The Hwnd owns the region. regionHandle = IntPtr.Zero; @@ -3656,7 +3646,7 @@ public bool TabStop TabStopInternal = value; if (IsHandleCreated) { - SetWindowStyle(NativeMethods.WS_TABSTOP, value); + SetWindowStyle(User32.WindowStyle.WS_TABSTOP, value); } OnTabStopChanged(EventArgs.Empty); @@ -3923,8 +3913,10 @@ protected internal virtual bool ShowFocusCues } } - // The parameter used in the call to ShowWindow for this control - internal virtual int ShowParams => NativeMethods.SW_SHOW; + /// + /// The parameter used in the call to ShowWindow for this control + /// + internal virtual User32.ShowWindowCommand ShowParams => User32.ShowWindowCommand.SW_SHOW; /// /// When this property in true the Cursor Property is set to WaitCursor as well as the Cursor Property @@ -4059,13 +4051,14 @@ private void WaitForWaitHandle(WaitHandle waitHandle) bool returnValue = false; while (!processed) { - //Get the thread's exit code, if we found the thread as expected + // Get the thread's exit code, if we found the thread as expected if (threadHandle != null) { - returnValue = UnsafeNativeMethods.GetExitCodeThread(threadHandle, out exitCode); + returnValue = Kernel32.GetExitCodeThread(threadHandle, out exitCode); } - //If we didn't find the thread, or if GetExitCodeThread failed, we don't know the thread's state: - //if we don't know, we shouldn't throw. + + // If we didn't find the thread, or if GetExitCodeThread failed, we don't know the thread's state: + // if we don't know, we shouldn't throw. if ((returnValue && exitCode != NativeMethods.STILL_ACTIVE) || (!returnValue && Marshal.GetLastWin32Error() == NativeMethods.ERROR_INVALID_HANDLE) || AppDomain.CurrentDomain.IsFinalizingForUnload()) @@ -4115,14 +4108,8 @@ public int Width /// private int WindowExStyle { - get - { - return unchecked((int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE)); - } - set - { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE, new HandleRef(null, (IntPtr)value)); - } + get => unchecked((int)(long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_EXSTYLE)); + set => User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_EXSTYLE, (IntPtr)value); } /// @@ -4130,14 +4117,8 @@ private int WindowExStyle /// internal int WindowStyle { - get - { - return unchecked((int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE)); - } - set - { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)value)); - } + get => unchecked((int)(long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE)); + set => User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)value); } /// @@ -4716,7 +4697,7 @@ private void ActiveXViewChanged() /// Helper method for retrieving an ActiveX property. We abstract these /// to another method so we do not force JIT the ActiveX codebase. /// - private void ActiveXUpdateBounds(ref int x, ref int y, ref int width, ref int height, int flags) + private void ActiveXUpdateBounds(ref int x, ref int y, ref int width, ref int height, User32.WindowPosition flags) { ActiveXInstance.UpdateBounds(ref x, ref y, ref width, ref height, flags); } @@ -4884,12 +4865,12 @@ public void BringToFront() { _parent.Controls.SetChildIndex(this, 0); } - else if (IsHandleCreated && GetTopLevel() && SafeNativeMethods.IsWindowEnabled(new HandleRef(_window, Handle))) + else if (IsHandleCreated && GetTopLevel() && User32.IsWindowEnabled(new HandleRef(_window, Handle))) { - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(_window, Handle), - NativeMethods.HWND_TOP, - flags: NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE); + User32.HWND_TOP, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE); } } @@ -5087,7 +5068,7 @@ protected virtual void CreateHandle() } CreateParams cp = CreateParams; - SetState(STATE_MIRRORED, (cp.ExStyle & NativeMethods.WS_EX_LAYOUTRTL) != 0); + SetState(STATE_MIRRORED, (cp.ExStyle & User32.WindowStyle.WS_EX_LAYOUTRTL) != 0); // Adjust for scrolling of parent... if (_parent != null) @@ -5108,9 +5089,9 @@ protected virtual void CreateHandle() } // And if we are WS_CHILD, ensure we have a parent handle. - if (cp.Parent == IntPtr.Zero && (cp.Style & NativeMethods.WS_CHILD) != 0) + if (cp.Parent == IntPtr.Zero && (cp.Style & User32.WindowStyle.WS_CHILD) != 0) { - Debug.Assert((cp.ExStyle & NativeMethods.WS_EX_MDICHILD) == 0, "Can't put MDI child forms on the parking form"); + Debug.Assert((cp.ExStyle & User32.WindowStyle.WS_EX_MDICHILD) == 0, "Can't put MDI child forms on the parking form"); Application.ParkHandle(cp); } @@ -5277,9 +5258,9 @@ protected virtual void DestroyHandle() } } - if (0 != (NativeMethods.WS_EX_MDICHILD & (int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(_window, InternalHandle), NativeMethods.GWL_EXSTYLE))) + if ((User32.WindowStyle.WS_EX_MDICHILD & (int)(long)User32.GetWindowLong(new HandleRef(_window, InternalHandle), User32.WindowLong.GWL_EXSTYLE)) != 0) { - UnsafeNativeMethods.DefMDIChildProc(InternalHandle, WindowMessages.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); + User32.DefMDIChildProcW(InternalHandle, WindowMessages.WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } else { @@ -5537,7 +5518,7 @@ public object EndInvoke(IAsyncResult asyncResult) { // ignored Control marshaler = FindMarshalingControl(); - if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(marshaler, marshaler.Handle), out int pid) == SafeNativeMethods.GetCurrentThreadId()) + if (User32.GetWindowThreadProcessId(new HandleRef(marshaler, marshaler.Handle), out int pid) == Kernel32.GetCurrentThreadId()) { marshaler.InvokeMarshaledCallbacks(); } @@ -5690,7 +5671,7 @@ private protected virtual bool FocusInternal() Debug.WriteLineIf(s_focusTracing.TraceVerbose, "Control::FocusInternal - " + Name); if (CanFocus) { - UnsafeNativeMethods.SetFocus(new HandleRef(this, Handle)); + User32.SetFocus(new HandleRef(this, Handle)); } if (Focused && ParentInternal != null) { @@ -5730,7 +5711,7 @@ public static Control FromChildHandle(IntPtr handle) return ctl; } - handle = UnsafeNativeMethods.GetAncestor(new HandleRef(null, handle), NativeMethods.GA_PARENT); + handle = User32.GetAncestor(handle, User32.GetAncestorFlag.GA_PARENT); } return null; } @@ -6103,9 +6084,9 @@ private static ArrayList GetChildWindows(IntPtr hWndParent) { ArrayList windows = new ArrayList(); - for (IntPtr hWndChild = UnsafeNativeMethods.GetWindow(new HandleRef(null, hWndParent), NativeMethods.GW_CHILD); + for (IntPtr hWndChild = User32.GetWindow(new HandleRef(null, hWndParent), User32.GetWindowOption.GW_CHILD); hWndChild != IntPtr.Zero; - hWndChild = UnsafeNativeMethods.GetWindow(new HandleRef(null, hWndChild), NativeMethods.GW_HWNDNEXT)) + hWndChild = User32.GetWindow(new HandleRef(null, hWndChild), User32.GetWindowOption.GW_HWNDNEXT)) { windows.Add(hWndChild); } @@ -6364,7 +6345,7 @@ internal static IntPtr GetSafeHandle(IWin32Window window) else { IntPtr hWnd = window.Handle; - if (hWnd == IntPtr.Zero || UnsafeNativeMethods.IsWindow(new HandleRef(null, hWnd))) + if (hWnd == IntPtr.Zero || User32.IsWindow(hWnd)) { return hWnd; } @@ -7131,7 +7112,7 @@ private object MarshaledInvoke(Control caller, Delegate method, object[] args, b bool syncSameThread = false; // ignored - if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, Handle), out int pid) == SafeNativeMethods.GetCurrentThreadId()) + if (User32.GetWindowThreadProcessId(new HandleRef(this, Handle), out int pid) == Kernel32.GetCurrentThreadId()) { if (synchronous) { @@ -7161,7 +7142,7 @@ private object MarshaledInvoke(Control caller, Delegate method, object[] args, b { if (s_threadCallbackMessage == 0) { - s_threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage"); + s_threadCallbackMessage = User32.RegisterWindowMessageW(Application.WindowMessagesVersion + "_ThreadCallbackMessage"); } _threadCallbackList.Enqueue(tme); } @@ -7484,7 +7465,7 @@ protected virtual void OnEnabledChanged(EventArgs e) if (IsHandleCreated) { - SafeNativeMethods.EnableWindow(new HandleRef(this, Handle), Enabled); + User32.EnableWindow(new HandleRef(this, Handle), Enabled); // User-paint controls should repaint when their enabled state changes if (GetStyle(ControlStyles.UserPaint)) @@ -7700,7 +7681,7 @@ internal virtual void OnParentHandleRecreated() { if (IsHandleCreated) { - UnsafeNativeMethods.SetParent(new HandleRef(this, Handle), new HandleRef(parent, parent.Handle)); + User32.SetParent(new HandleRef(this, Handle), new HandleRef(parent, parent.Handle)); UpdateZOrder(); } } @@ -8035,7 +8016,7 @@ protected virtual void OnHandleCreated(EventArgs e) regionHandle = ActiveXMergeRegion(regionHandle); } - if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(this, regionHandle), SafeNativeMethods.IsWindowVisible(new HandleRef(this, Handle))) != 0) + if (User32.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(this, regionHandle), User32.IsWindowVisible(new HandleRef(this, Handle))) != 0) { //The HWnd owns the region. regionHandle = IntPtr.Zero; @@ -9498,8 +9479,8 @@ internal virtual void PrintToMetaFileRecursive(HandleRef hDC, IntPtr lParam, Rec PrintToMetaFile_SendPrintMessage(hDC, (IntPtr)((long)lParam & ~NativeMethods.PRF_CLIENT)); // figure out mapping for the client area - RECT windowRect = new RECT(); - bool success = UnsafeNativeMethods.GetWindowRect(new HandleRef(null, Handle), ref windowRect); + var windowRect = new RECT(); + bool success = User32.GetWindowRect(Handle, ref windowRect); Debug.Assert(success, "GetWindowRect() failed."); Point clientOffset = PointToScreen(Point.Empty); clientOffset = new Point(clientOffset.X - windowRect.left, clientOffset.Y - windowRect.top); @@ -9831,7 +9812,7 @@ internal void ProcessUICues(ref Message msg) // (we're in charge here, we've got to change the state of the root window) UnsafeNativeMethods.SendMessage( new HandleRef(topMostParent, topMostParent.Handle), - UnsafeNativeMethods.GetParent(new HandleRef(null, topMostParent.Handle)) == IntPtr.Zero ? WindowMessages.WM_CHANGEUISTATE : WindowMessages.WM_UPDATEUISTATE, + User32.GetParent(topMostParent.Handle) == IntPtr.Zero ? WindowMessages.WM_CHANGEUISTATE : WindowMessages.WM_UPDATEUISTATE, (IntPtr)(NativeMethods.UIS_CLEAR | (toClear << 16)), IntPtr.Zero); } @@ -9982,7 +9963,7 @@ internal virtual void RecreateHandleCore() UnhookMouseEvent(); } - HandleRef parentHandle = new HandleRef(this, UnsafeNativeMethods.GetParent(new HandleRef(this, Handle))); + HandleRef parentHandle = new HandleRef(this, User32.GetParent(new HandleRef(this, Handle))); try { @@ -10053,12 +10034,12 @@ internal virtual void RecreateHandleCore() { if (parentHandle.Handle != IntPtr.Zero // the parent was not null && (FromHandle(parentHandle.Handle) == null || _parent == null) // but wasnt a windows forms window - && UnsafeNativeMethods.IsWindow(parentHandle)) + && User32.IsWindow(parentHandle)) { // and still is a window // correctly parent back up to where we were before. // if we were parented to a proper windows forms control, CreateControl would have properly parented // us back. - UnsafeNativeMethods.SetParent(new HandleRef(this, Handle), parentHandle); + User32.SetParent(new HandleRef(this, Handle), parentHandle); } } @@ -10860,10 +10841,10 @@ public void SendToBack() } else if (IsHandleCreated && GetTopLevel()) { - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(_window, Handle), - NativeMethods.HWND_BOTTOM, - flags: NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE); + User32.HWND_BOTTOM, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE); } } @@ -10981,24 +10962,22 @@ protected virtual void SetBoundsCore(int x, int y, int width, int height, Bounds { if (!GetState(STATE_SIZELOCKEDBYOS)) { - int flags = NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE; - + User32.WindowPosition flags = User32.WindowPosition.SWP_NOZORDER | User32.WindowPosition.SWP_NOACTIVATE; if (_x == x && _y == y) { - flags |= NativeMethods.SWP_NOMOVE; + flags |= User32.WindowPosition.SWP_NOMOVE; } if (_width == width && _height == height) { - flags |= NativeMethods.SWP_NOSIZE; + flags |= User32.WindowPosition.SWP_NOSIZE; } - // // Give a chance for derived controls to do what they want, just before we resize. OnBoundsUpdate(x, y, width, height); - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(_window, Handle), - NativeMethods.NullHandleRef, + IntPtr.Zero, x, y, width, @@ -11008,8 +10987,6 @@ protected virtual void SetBoundsCore(int x, int y, int width, int height, Bounds // NOTE: SetWindowPos causes a WM_WINDOWPOSCHANGED which is processed // synchonously so we effectively end up in UpdateBounds immediately following // SetWindowPos. - // - //UpdateBounds(x, y, width, height); } } } @@ -11082,7 +11059,7 @@ private void SetParentHandle(IntPtr value) if (IsHandleCreated) { - IntPtr parentHandle = UnsafeNativeMethods.GetParent(new HandleRef(_window, Handle)); + IntPtr parentHandle = User32.GetParent(new HandleRef(_window, Handle)); bool topLevel = GetTopLevel(); if (parentHandle != value || (parentHandle == IntPtr.Zero && !topLevel)) { @@ -11120,7 +11097,7 @@ private void SetParentHandle(IntPtr value) } else { - UnsafeNativeMethods.SetParent(new HandleRef(_window, Handle), new HandleRef(null, value)); + User32.SetParent(new HandleRef(_window, Handle), value); if (_parent != null) { _parent.UpdateChildZOrder(this); @@ -11134,7 +11111,7 @@ private void SetParentHandle(IntPtr value) // The handle was previously parented to the parking window. Its TopLevel property was // then changed to true so the above call to GetParent returns null even though the parent of the control is // not null. We need to explicitly set the parent to null. - UnsafeNativeMethods.SetParent(new HandleRef(_window, Handle), new HandleRef(null, IntPtr.Zero)); + User32.SetParent(new HandleRef(_window, Handle), IntPtr.Zero); Application.UnparkHandle(new HandleRef(_window, Handle), _window.DpiAwarenessContext); } } @@ -11231,12 +11208,10 @@ protected virtual void SetVisibleCore(bool value) if (GetTopLevel()) { - // The processing of WmShowWindow will set the visibility - // bit and call CreateControl() - + // The processing of WmShowWindow will set the visibility bit and call CreateControl() if (IsHandleCreated || value) { - SafeNativeMethods.ShowWindow(new HandleRef(this, Handle), value ? ShowParams : NativeMethods.SW_HIDE); + User32.ShowWindow(new HandleRef(this, Handle), value ? ShowParams : User32.ShowWindowCommand.SW_HIDE); } } else if (IsHandleCreated || value && _parent != null && _parent.Created) @@ -11254,14 +11229,14 @@ protected virtual void SetVisibleCore(bool value) CreateControl(); } - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(_window, Handle), - NativeMethods.NullHandleRef, - flags: NativeMethods.SWP_NOSIZE - | NativeMethods.SWP_NOMOVE - | NativeMethods.SWP_NOZORDER - | NativeMethods.SWP_NOACTIVATE - | (value ? NativeMethods.SWP_SHOWWINDOW : NativeMethods.SWP_HIDEWINDOW)); + IntPtr.Zero, + flags: User32.WindowPosition.SWP_NOSIZE + | User32.WindowPosition.SWP_NOMOVE + | User32.WindowPosition.SWP_NOZORDER + | User32.WindowPosition.SWP_NOACTIVATE + | (value ? User32.WindowPosition.SWP_SHOWWINDOW : User32.WindowPosition.SWP_HIDEWINDOW)); } catch { @@ -11299,7 +11274,7 @@ protected virtual void SetVisibleCore(bool value) // PERF - setting Visible=false twice can get us into this else block // which makes us process WM_WINDOWPOS* messages - make sure we've already // visible=false - if not, make it so. - if (!SafeNativeMethods.IsWindowVisible(new HandleRef(this, Handle))) + if (!User32.IsWindowVisible(new HandleRef(this, Handle))) { // we're already invisible - bail. return; @@ -11313,14 +11288,14 @@ protected virtual void SetVisibleCore(bool value) // but the child control has already been created. if (IsHandleCreated) { - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(_window, Handle), - NativeMethods.NullHandleRef, - flags: NativeMethods.SWP_NOSIZE - | NativeMethods.SWP_NOMOVE - | NativeMethods.SWP_NOZORDER - | NativeMethods.SWP_NOACTIVATE - | (value ? NativeMethods.SWP_SHOWWINDOW : NativeMethods.SWP_HIDEWINDOW)); + IntPtr.Zero, + flags: User32.WindowPosition.SWP_NOSIZE + | User32.WindowPosition.SWP_NOMOVE + | User32.WindowPosition.SWP_NOZORDER + | User32.WindowPosition.SWP_NOACTIVATE + | (value ? User32.WindowPosition.SWP_SHOWWINDOW : User32.WindowPosition.SWP_HIDEWINDOW)); } } } @@ -11528,8 +11503,8 @@ private void SetWindowFont() private void SetWindowStyle(int flag, bool value) { - int styleFlags = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)(value ? styleFlags | flag : styleFlags & ~flag))); + int styleFlags = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)(value ? styleFlags | flag : styleFlags & ~flag)); } /// @@ -11633,10 +11608,10 @@ protected internal void UpdateBounds() UnsafeNativeMethods.GetClientRect(new HandleRef(_window, InternalHandle), ref rect); int clientWidth = rect.right; int clientHeight = rect.bottom; - UnsafeNativeMethods.GetWindowRect(new HandleRef(_window, InternalHandle), ref rect); + User32.GetWindowRect(new HandleRef(_window, InternalHandle), ref rect); if (!GetTopLevel()) { - UnsafeNativeMethods.MapWindowPoints(NativeMethods.NullHandleRef, new HandleRef(null, UnsafeNativeMethods.GetParent(new HandleRef(_window, InternalHandle))), ref rect, 2); + UnsafeNativeMethods.MapWindowPoints(NativeMethods.NullHandleRef, new HandleRef(null, User32.GetParent(new HandleRef(_window, InternalHandle))), ref rect, 2); } UpdateBounds(rect.left, rect.top, rect.right - rect.left, @@ -11763,7 +11738,7 @@ private void UpdateChildControlIndex(Control ctl) int newIndex = 0; int curIndex = Controls.GetChildIndex(ctl); IntPtr hWnd = ctl.InternalHandle; - while ((hWnd = UnsafeNativeMethods.GetWindow(new HandleRef(null, hWnd), NativeMethods.GW_HWNDPREV)) != IntPtr.Zero) + while ((hWnd = User32.GetWindow(new HandleRef(null, hWnd), User32.GetWindowOption.GW_HWNDPREV)) != IntPtr.Zero) { Control c = FromHandle(hWnd); if (c != null) @@ -11793,7 +11768,7 @@ private void UpdateReflectParent(bool findNewParent) { if (!Disposing && findNewParent && IsHandleCreated) { - IntPtr parentHandle = UnsafeNativeMethods.GetParent(new HandleRef(this, Handle)); + IntPtr parentHandle = User32.GetParent(new HandleRef(this, Handle)); if (parentHandle != IntPtr.Zero) { ReflectParent = FromHandle(parentHandle); @@ -11825,7 +11800,7 @@ private void UpdateChildZOrder(Control ctl) return; } - IntPtr prevHandle = (IntPtr)NativeMethods.HWND_TOP; + IntPtr prevHandle = (IntPtr)User32.HWND_TOP; for (int i = Controls.GetChildIndex(ctl); --i >= 0;) { Control c = Controls[i]; @@ -11835,15 +11810,15 @@ private void UpdateChildZOrder(Control ctl) break; } } - if (UnsafeNativeMethods.GetWindow(new HandleRef(ctl._window, ctl.Handle), NativeMethods.GW_HWNDPREV) != prevHandle) + if (User32.GetWindow(new HandleRef(ctl._window, ctl.Handle), User32.GetWindowOption.GW_HWNDPREV) != prevHandle) { _state |= STATE_NOZORDER; try { - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(ctl._window, ctl.Handle), - new HandleRef(null, prevHandle), - flags: NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE); + prevHandle, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE); } finally { @@ -11884,7 +11859,7 @@ internal virtual void UpdateStylesCore() // resolve the Form's lazy visibility. if ((_state & STATE_VISIBLE) != 0) { - cp.Style |= NativeMethods.WS_VISIBLE; + cp.Style |= User32.WindowStyle.WS_VISIBLE; } if (winStyle != cp.Style) { @@ -11893,17 +11868,17 @@ internal virtual void UpdateStylesCore() if (exStyle != cp.ExStyle) { WindowExStyle = cp.ExStyle; - SetState(STATE_MIRRORED, (cp.ExStyle & NativeMethods.WS_EX_LAYOUTRTL) != 0); + SetState(STATE_MIRRORED, (cp.ExStyle & User32.WindowStyle.WS_EX_LAYOUTRTL) != 0); } - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(this, Handle), - NativeMethods.NullHandleRef, - flags: NativeMethods.SWP_DRAWFRAME - | NativeMethods.SWP_NOACTIVATE - | NativeMethods.SWP_NOMOVE - | NativeMethods.SWP_NOSIZE - | NativeMethods.SWP_NOZORDER); + IntPtr.Zero, + flags: User32.WindowPosition.SWP_DRAWFRAME + | User32.WindowPosition.SWP_NOACTIVATE + | User32.WindowPosition.SWP_NOMOVE + | User32.WindowPosition.SWP_NOSIZE + | User32.WindowPosition.SWP_NOZORDER); Invalidate(true); } @@ -11946,11 +11921,11 @@ private void WmClose(ref Message m) while (parentHandle != IntPtr.Zero) { lastParentHandle = parentHandle; - parentHandle = UnsafeNativeMethods.GetParent(new HandleRef(null, parentHandle)); + parentHandle = User32.GetParent(parentHandle); - int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(null, lastParentHandle), NativeMethods.GWL_STYLE))); + int style = unchecked((int)((long)User32.GetWindowLong(lastParentHandle, User32.WindowLong.GWL_STYLE))); - if ((style & NativeMethods.WS_CHILD) == 0) + if ((style & User32.WindowStyle.WS_CHILD) == 0) { break; } @@ -13079,11 +13054,11 @@ private unsafe void WmWindowPosChanging(ref Message m) // bool different = false; - if ((wp->flags & NativeMethods.SWP_NOMOVE) == 0 && (wp->x != Left || wp->y != Top)) + if ((wp->flags & User32.WindowPosition.SWP_NOMOVE) == 0 && (wp->x != Left || wp->y != Top)) { different = true; } - if ((wp->flags & NativeMethods.SWP_NOSIZE) == 0 && (wp->cx != Width || wp->cy != Height)) + if ((wp->flags & User32.WindowPosition.SWP_NOSIZE) == 0 && (wp->cx != Width || wp->cy != Height)) { different = true; } @@ -13328,12 +13303,12 @@ private unsafe void WmWindowPosChanged(ref Message m) DefWndProc(ref m); // Update new size / position UpdateBounds(); - if (_parent != null && UnsafeNativeMethods.GetParent(new HandleRef(_window, InternalHandle)) == _parent.InternalHandle && + if (_parent != null && User32.GetParent(new HandleRef(_window, InternalHandle)) == _parent.InternalHandle && (_state & STATE_NOZORDER) == 0) { NativeMethods.WINDOWPOS* wp = (NativeMethods.WINDOWPOS*)m.LParam; - if ((wp->flags & NativeMethods.SWP_NOZORDER) == 0) + if ((wp->flags & User32.WindowPosition.SWP_NOZORDER) == 0) { _parent.UpdateChildControlIndex(this); } @@ -14630,8 +14605,8 @@ internal virtual void OnKeyboardToolTipUnhook(ToolTip toolTip) internal virtual Rectangle GetToolNativeScreenRectangle() { - RECT rectangle = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref rectangle); + var rectangle = new RECT(); + User32.GetWindowRect(new HandleRef(this, Handle), ref rectangle); return Rectangle.FromLTRB(rectangle.left, rectangle.top, rectangle.right, rectangle.bottom); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridToolTip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridToolTip.cs index f3dd136d667..b68d1e3f1fe 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridToolTip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridToolTip.cs @@ -47,7 +47,10 @@ public void CreateToolTipHandle() tipWindow.CreateHandle(cparams); User32.SendMessageW(tipWindow, WindowMessages.TTM_SETMAXTIPWIDTH, IntPtr.Zero, (IntPtr)SystemInformation.MaxWindowTrackSize.Width); - SafeNativeMethods.SetWindowPos(new HandleRef(tipWindow, tipWindow.Handle), NativeMethods.HWND_NOTOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(tipWindow, tipWindow.Handle), + User32.HWND_NOTOPMOST, + flags: User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOACTIVATE); User32.SendMessageW(tipWindow, WindowMessages.TTM_SETDELAYTIME, (IntPtr)ComCtl32.TTDT.INITIAL, (IntPtr)0); } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs index 592db75ee9d..958c5ccfd8c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewCell.cs @@ -9,6 +9,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; +using static Interop; namespace System.Windows.Forms { @@ -1185,7 +1186,7 @@ public virtual void DetachEditingControl() // We don't want the grid to get the keyboard focus // when the editing control gets parented to the parking window, // because some other window is in the middle of receiving the focus. - UnsafeNativeMethods.SetFocus(new HandleRef(null, IntPtr.Zero)); + User32.SetFocus(IntPtr.Zero); } } Debug.Assert(dgv.EditingControl.ParentInternal == dgv.EditingPanel); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/DateTimePicker.cs b/src/System.Windows.Forms/src/System/Windows/Forms/DateTimePicker.cs index 71aaffa9867..4297b3ea095 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/DateTimePicker.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/DateTimePicker.cs @@ -481,14 +481,14 @@ protected override CreateParams CreateParams break; } - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for DateTimePicker explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; @@ -1673,10 +1673,10 @@ private void WmDropDown(ref Message m) IntPtr handle = SendMessage(NativeMethods.DTM_GETMONTHCAL, 0, 0); if (handle != IntPtr.Zero) { - int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_EXSTYLE))); - style |= NativeMethods.WS_EX_LAYOUTRTL | NativeMethods.WS_EX_NOINHERITLAYOUT; - style &= ~(NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_RTLREADING); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_EXSTYLE, new HandleRef(this, (IntPtr)style)); + int style = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_EXSTYLE))); + style |= User32.WindowStyle.WS_EX_LAYOUTRTL | User32.WindowStyle.WS_EX_NOINHERITLAYOUT; + style &= ~(User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_RTLREADING); + User32.SetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_EXSTYLE, new HandleRef(this, (IntPtr)style)); } } OnDropDown(EventArgs.Empty); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs index 91460123134..1f50a68a46b 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorForm.cs @@ -673,7 +673,7 @@ protected override CreateParams CreateParams { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_STATICEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_STATICEDGE; return cp; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs index 3658b9f4928..b4a88f85557 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Design/ComponentEditorPage.cs @@ -3,9 +3,9 @@ // See the LICENSE file in the project root for more information. using System.ComponentModel; -using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; +using static Interop; namespace System.Windows.Forms.Design { @@ -84,7 +84,7 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style &= ~(NativeMethods.WS_BORDER | NativeMethods.WS_OVERLAPPED | NativeMethods.WS_DLGFRAME); + cp.Style &= ~(User32.WindowStyle.WS_BORDER | User32.WindowStyle.WS_OVERLAPPED | User32.WindowStyle.WS_DLGFRAME); return cp; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider.cs index 877a35fb9b8..f9a8e8bf3b8 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider.cs @@ -873,8 +873,8 @@ bool EnsureCreated() CreateParams cparams = new CreateParams { Caption = string.Empty, - Style = NativeMethods.WS_VISIBLE | NativeMethods.WS_CHILD, - ClassStyle = (int)NativeMethods.ClassStyle.CS_DBLCLKS, + Style = User32.WindowStyle.WS_VISIBLE | User32.WindowStyle.WS_CHILD, + ClassStyle = (int)User32.ClassStyle.CS_DBLCLKS, X = 0, Y = 0, Width = 0, @@ -900,7 +900,10 @@ bool EnsureCreated() _tipWindow.CreateHandle(cparams); User32.SendMessageW(_tipWindow, WindowMessages.TTM_SETMAXTIPWIDTH, IntPtr.Zero, (IntPtr)SystemInformation.MaxWindowTrackSize.Width); - SafeNativeMethods.SetWindowPos(new HandleRef(_tipWindow, _tipWindow.Handle), NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(_tipWindow, _tipWindow.Handle), + User32.HWND_TOP, + flags: User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOACTIVATE); User32.SendMessageW(_tipWindow, WindowMessages.TTM_SETDELAYTIME, (IntPtr)ComCtl32.TTDT.INITIAL, (IntPtr)0); } @@ -923,19 +926,16 @@ private void EnsureDestroyed() _tipWindow = null; } - // Hide the window and invalidate the parent to ensure - // that we leave no visual artifacts. given that we - // have a bizare region window, this is needed. - // - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), - NativeMethods.HWND_TOP, - _windowBounds.X, - _windowBounds.Y, - _windowBounds.Width, - _windowBounds.Height, - NativeMethods.SWP_HIDEWINDOW - | NativeMethods.SWP_NOSIZE - | NativeMethods.SWP_NOMOVE); + // Hide the window and invalidate the parent to ensure that we leave no visual artifacts. + // Given that we have a bizare region window, this is needed. + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOP, + _windowBounds.X, + _windowBounds.Y, + _windowBounds.Width, + _windowBounds.Height, + User32.WindowPosition.SWP_HIDEWINDOW | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOMOVE); _parent?.Invalidate(true); DestroyHandle(); @@ -1208,7 +1208,7 @@ public void Update(bool timerCaused) RestoreMirrorDC(); } - if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(windowRegion, windowRegionHandle), true) != 0) + if (User32.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(windowRegion, windowRegionHandle), true) != 0) { // The HWnd owns the region. windowRegionHandle = IntPtr.Zero; @@ -1233,8 +1233,14 @@ public void Update(bool timerCaused) } } - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.HWND_TOP, _windowBounds.X, _windowBounds.Y, - _windowBounds.Width, _windowBounds.Height, NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOP, + _windowBounds.X, + _windowBounds.Y, + _windowBounds.Width, + _windowBounds.Height, + User32.WindowPosition.SWP_NOACTIVATE); SafeNativeMethods.InvalidateRect(new HandleRef(this, Handle), null, false); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/FileDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/FileDialog.cs index 7769049d7f0..8edae0dc87d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/FileDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/FileDialog.cs @@ -477,7 +477,7 @@ protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr l { if (msg == WindowMessages.WM_NOTIFY) { - _dialogHWnd = UnsafeNativeMethods.GetParent(new HandleRef(null, hWnd)); + _dialogHWnd = User32.GetParent(hWnd); try { UnsafeNativeMethods.OFNOTIFY notify = Marshal.PtrToStructure(lparam); @@ -535,13 +535,13 @@ protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr l { // This is the second CDN_FILEOK, so we want to ignore it. _ignoreSecondFileOkNotification = false; - UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hWnd), 0, new HandleRef(null, NativeMethods.InvalidIntPtr)); + User32.SetWindowLong(hWnd, 0, NativeMethods.InvalidIntPtr); return NativeMethods.InvalidIntPtr; } } if (!DoFileOk(notify.lpOFN)) { - UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hWnd), 0, new HandleRef(null, NativeMethods.InvalidIntPtr)); + User32.SetWindowLong(hWnd, 0, NativeMethods.InvalidIntPtr); return NativeMethods.InvalidIntPtr; } break; @@ -664,7 +664,7 @@ private bool ProcessFileNames() private protected bool MessageBoxWithFocusRestore(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) { - IntPtr focusHandle = UnsafeNativeMethods.GetFocus(); + IntPtr focusHandle = User32.GetFocus(); try { return RTLAwareMessageBox.Show(null, message, caption, buttons, icon, @@ -672,7 +672,7 @@ private protected bool MessageBoxWithFocusRestore(string message, string caption } finally { - UnsafeNativeMethods.SetFocus(new HandleRef(null, focusHandle)); + User32.SetFocus(focusHandle); } } @@ -744,7 +744,7 @@ protected override bool RunDialog(IntPtr hWndOwner) private bool RunDialogOld(IntPtr hWndOwner) { - var hookProcPtr = new NativeMethods.WndProc(HookProc); + var hookProcPtr = new User32.WndProc(HookProc); var ofn = new NativeMethods.OPENFILENAME_I(); try { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/FontDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/FontDialog.cs index 488107f1ae0..b43c3f5bc0d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/FontDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/FontDialog.cs @@ -483,9 +483,9 @@ protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr l if (!showColor) { IntPtr hWndCtl = UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), NativeMethods.cmb4); - SafeNativeMethods.ShowWindow(new HandleRef(null, hWndCtl), NativeMethods.SW_HIDE); + User32.ShowWindow(hWndCtl, User32.ShowWindowCommand.SW_HIDE); hWndCtl = UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), NativeMethods.stc4); - SafeNativeMethods.ShowWindow(new HandleRef(null, hWndCtl), NativeMethods.SW_HIDE); + User32.ShowWindow(hWndCtl, User32.ShowWindowCommand.SW_HIDE); } break; } @@ -528,7 +528,7 @@ private void ResetFont() /// protected unsafe override bool RunDialog(IntPtr hWndOwner) { - NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(HookProc); + User32.WndProc hookProcPtr = new User32.WndProc(HookProc); NativeMethods.LOGFONTW logFont; using ScreenDC dc = ScreenDC.Create(); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs index 388d28ecd36..5b2c9dfd9ec 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Form.cs @@ -343,19 +343,7 @@ internal bool Active /// /// Gets the currently active form for this application. /// - public static Form ActiveForm - { - get - { - IntPtr hwnd = UnsafeNativeMethods.GetForegroundWindow(); - Control c = Control.FromHandle(hwnd); - if (c != null && c is Form) - { - return (Form)c; - } - return null; - } - } + public static Form ActiveForm => Control.FromHandle(User32.GetForegroundWindow()) as Form; /// /// @@ -889,21 +877,21 @@ protected override CreateParams CreateParams { CreateParams cp = base.CreateParams; - if (IsHandleCreated && (WindowStyle & NativeMethods.WS_DISABLED) != 0) + if (IsHandleCreated && (WindowStyle & User32.WindowStyle.WS_DISABLED) != 0) { // Forms that are parent of a modal dialog must keep their WS_DISABLED style - cp.Style |= NativeMethods.WS_DISABLED; + cp.Style |= User32.WindowStyle.WS_DISABLED; } else if (TopLevel) { // It doesn't seem to make sense to allow a top-level form to be disabled // - cp.Style &= (~NativeMethods.WS_DISABLED); + cp.Style &= (~User32.WindowStyle.WS_DISABLED); } if (TopLevel && (formState[FormStateLayered] != 0)) { - cp.ExStyle |= NativeMethods.WS_EX_LAYERED; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYERED; } IWin32Window dialogOwner = (IWin32Window)Properties.GetObject(PropDialogOwner); @@ -918,7 +906,7 @@ protected override CreateParams CreateParams if (formState[FormStateTaskBar] != 0) { - cp.ExStyle |= NativeMethods.WS_EX_APPWINDOW; + cp.ExStyle |= User32.WindowStyle.WS_EX_APPWINDOW; } FormBorderStyle borderStyle = FormBorderStyle; @@ -927,7 +915,7 @@ protected override CreateParams CreateParams borderStyle == FormBorderStyle.Fixed3D || borderStyle == FormBorderStyle.FixedSingle)) { - cp.ExStyle |= NativeMethods.WS_EX_DLGMODALFRAME; + cp.ExStyle |= User32.WindowStyle.WS_EX_DLGMODALFRAME; } if (IsMdiChild) @@ -942,7 +930,7 @@ protected override CreateParams CreateParams if (form != null && form.WindowState == FormWindowState.Maximized) { - cp.Style |= NativeMethods.WS_MAXIMIZE; + cp.Style |= User32.WindowStyle.WS_MAXIMIZE; formState[FormStateWindowState] = (int)FormWindowState.Maximized; SetState(STATE_SIZELOCKEDBYOS, true); } @@ -950,9 +938,9 @@ protected override CreateParams CreateParams if (formState[FormStateMdiChildMax] != 0) { - cp.Style |= NativeMethods.WS_MAXIMIZE; + cp.Style |= User32.WindowStyle.WS_MAXIMIZE; } - cp.ExStyle |= NativeMethods.WS_EX_MDICHILD; + cp.ExStyle |= User32.WindowStyle.WS_EX_MDICHILD; } if (TopLevel || IsMdiChild) @@ -962,10 +950,10 @@ protected override CreateParams CreateParams // to allow applyClientSize to adjust the size before displaying // the form. // - if ((cp.Style & NativeMethods.WS_VISIBLE) != 0) + if ((cp.Style & User32.WindowStyle.WS_VISIBLE) != 0) { formState[FormStateShowWindowOnCreate] = 1; - cp.Style &= (~NativeMethods.WS_VISIBLE); + cp.Style &= (~User32.WindowStyle.WS_VISIBLE); } else { @@ -976,9 +964,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Form explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL | NativeMethods.WS_EX_NOINHERITLAYOUT; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL | User32.WindowStyle.WS_EX_NOINHERITLAYOUT; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; } @@ -1630,12 +1618,14 @@ public override Size MinimumSize { // "Move" the form to the same size and position to prevent windows from moving it // when the user tries to grab a resizing border. - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.NullHandleRef, - Location.X, - Location.Y, - Size.Width, - Size.Height, - NativeMethods.SWP_NOZORDER); + User32.SetWindowPos( + new HandleRef(this, Handle), + IntPtr.Zero, + Location.X, + Location.Y, + Size.Width, + Size.Height, + User32.WindowPosition.SWP_NOZORDER); } OnMinimumSizeChanged(EventArgs.Empty); @@ -1981,11 +1971,11 @@ public double Opacity formState[FormStateLayered] = (TransparencyKey != Color.Empty) ? 1 : 0; if (oldLayered != (formState[FormStateLayered] != 0)) { - int exStyle = unchecked((int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE)); + int exStyle = unchecked((int)(long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_EXSTYLE)); CreateParams cp = CreateParams; if (exStyle != cp.ExStyle) { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE, new HandleRef(null, (IntPtr)cp.ExStyle)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_EXSTYLE, (IntPtr)cp.ExStyle); } } } @@ -2205,7 +2195,7 @@ public bool ShowIcon } } - internal override int ShowParams + internal override User32.ShowWindowCommand ShowParams { get { @@ -2221,20 +2211,20 @@ internal override int ShowParams switch (WindowState) { case FormWindowState.Maximized: - return NativeMethods.SW_SHOWMAXIMIZED; + return User32.ShowWindowCommand.SW_SHOWMAXIMIZED; case FormWindowState.Minimized: - return NativeMethods.SW_SHOWMINIMIZED; + return User32.ShowWindowCommand.SW_SHOWMINIMIZED; } if (ShowWithoutActivation) { - return NativeMethods.SW_SHOWNOACTIVATE; + return User32.ShowWindowCommand.SW_SHOWNOACTIVATE; } - return NativeMethods.SW_SHOW; + return User32.ShowWindowCommand.SW_SHOW; } } /// - /// When this property returns true, the internal ShowParams property will return NativeMethods.SW_SHOWNOACTIVATE. + /// When this property returns true, the internal ShowParams property will return User32.ShowWindowCommand.SW_SHOWNOACTIVATE. /// [Browsable(false)] protected virtual bool ShowWithoutActivation @@ -2394,7 +2384,7 @@ private HandleRef TaskbarOwner { CreateParams cp = new CreateParams { - ExStyle = NativeMethods.WS_EX_TOOLWINDOW + ExStyle = User32.WindowStyle.WS_EX_TOOLWINDOW }; ownerWindow.CreateHandle(cp); } @@ -2451,9 +2441,14 @@ public bool TopMost { if (IsHandleCreated && TopLevel) { - HandleRef key = value ? NativeMethods.HWND_TOPMOST : NativeMethods.HWND_NOTOPMOST; - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), key, 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE); + User32.SetWindowPos( + new HandleRef(this, Handle), + value ? User32.HWND_TOPMOST : User32.HWND_NOTOPMOST, + 0, + 0, + 0, + 0, + User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE); } formState[FormStateTopMost] = value ? 1 : 0; @@ -2598,7 +2593,7 @@ protected override void SetVisibleCore(bool value) SuspendLayout(); try { - SafeNativeMethods.ShowWindow(new HandleRef(this, Handle), NativeMethods.SW_SHOW); + User32.ShowWindow(new HandleRef(this, Handle), User32.ShowWindowCommand.SW_SHOW); CreateControl(); // If this form is mdichild and maximized, we need to redraw the MdiParent non-client area to @@ -2663,13 +2658,13 @@ public FormWindowState WindowState switch (value) { case FormWindowState.Normal: - SafeNativeMethods.ShowWindow(new HandleRef(this, hWnd), NativeMethods.SW_NORMAL); + User32.ShowWindow(new HandleRef(this, hWnd), User32.ShowWindowCommand.SW_NORMAL); break; case FormWindowState.Maximized: - SafeNativeMethods.ShowWindow(new HandleRef(this, hWnd), NativeMethods.SW_MAXIMIZE); + User32.ShowWindow(new HandleRef(this, hWnd), User32.ShowWindowCommand.SW_MAXIMIZE); break; case FormWindowState.Minimized: - SafeNativeMethods.ShowWindow(new HandleRef(this, hWnd), NativeMethods.SW_MINIMIZE); + User32.ShowWindow(new HandleRef(this, hWnd), User32.ShowWindowCommand.SW_MINIMIZE); break; } } @@ -3469,7 +3464,7 @@ protected override void CreateHandle() // if (!ShowInTaskbar && OwnerInternal == null && TopLevel) { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, TaskbarOwner); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, TaskbarOwner); // Make sure the large icon is set so the ALT+TAB icon // reflects the real icon of the application @@ -3548,23 +3543,19 @@ private void DeactivateMdiChild() } /// - /// Calls the default window proc for the form. If - /// a - /// subclass overrides this function, - /// it must call the base implementation. - /// - [ - EditorBrowsable(EditorBrowsableState.Advanced) - ] + /// Calls the default window proc for the form. If a subclass overrides this function, it must call the + /// base implementation. + /// + [EditorBrowsable(EditorBrowsableState.Advanced)] protected override void DefWndProc(ref Message m) { if (ctlClient != null && ctlClient.IsHandleCreated && ctlClient.ParentInternal == this) { - m.Result = UnsafeNativeMethods.DefFrameProc(m.HWnd, ctlClient.Handle, m.Msg, m.WParam, m.LParam); + m.Result = User32.DefFrameProcW(m.HWnd, ctlClient.Handle, m.Msg, m.WParam, m.LParam); } - else if (0 != formStateEx[FormStateExUseMdiChildProc]) + else if (formStateEx[FormStateExUseMdiChildProc] != 0) { - m.Result = UnsafeNativeMethods.DefMDIChildProc(m.HWnd, m.Msg, m.WParam, m.LParam); + m.Result = User32.DefMDIChildProcW(m.HWnd, m.Msg, m.WParam, m.LParam); } else { @@ -3700,34 +3691,34 @@ private void FillInCreateParamsBorderIcons(CreateParams cp) { if (!string.IsNullOrEmpty(Text)) { - cp.Style |= NativeMethods.WS_CAPTION; + cp.Style |= User32.WindowStyle.WS_CAPTION; } if (ControlBox) { - cp.Style |= NativeMethods.WS_SYSMENU | NativeMethods.WS_CAPTION; + cp.Style |= User32.WindowStyle.WS_SYSMENU | User32.WindowStyle.WS_CAPTION; } else { - cp.Style &= (~NativeMethods.WS_SYSMENU); + cp.Style &= (~User32.WindowStyle.WS_SYSMENU); } if (MaximizeBox) { - cp.Style |= NativeMethods.WS_MAXIMIZEBOX; + cp.Style |= User32.WindowStyle.WS_MAXIMIZEBOX; } else { - cp.Style &= ~NativeMethods.WS_MAXIMIZEBOX; + cp.Style &= ~User32.WindowStyle.WS_MAXIMIZEBOX; } if (MinimizeBox) { - cp.Style |= NativeMethods.WS_MINIMIZEBOX; + cp.Style |= User32.WindowStyle.WS_MINIMIZEBOX; } else { - cp.Style &= ~NativeMethods.WS_MINIMIZEBOX; + cp.Style &= ~User32.WindowStyle.WS_MINIMIZEBOX; } if (HelpButton && !MaximizeBox && !MinimizeBox && ControlBox) @@ -3736,11 +3727,11 @@ private void FillInCreateParamsBorderIcons(CreateParams cp) // But someone must have failed the check, because Windows 2000 // will show a help button if either the maximize or // minimize button is disabled. - cp.ExStyle |= NativeMethods.WS_EX_CONTEXTHELP; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTEXTHELP; } else { - cp.ExStyle &= ~NativeMethods.WS_EX_CONTEXTHELP; + cp.ExStyle &= ~User32.WindowStyle.WS_EX_CONTEXTHELP; } } } @@ -3755,26 +3746,26 @@ private void FillInCreateParamsBorderStyles(CreateParams cp) case FormBorderStyle.None: break; case FormBorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; case FormBorderStyle.Sizable: - cp.Style |= NativeMethods.WS_BORDER | NativeMethods.WS_THICKFRAME; + cp.Style |= User32.WindowStyle.WS_BORDER | User32.WindowStyle.WS_THICKFRAME; break; case FormBorderStyle.Fixed3D: - cp.Style |= NativeMethods.WS_BORDER; - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.Style |= User32.WindowStyle.WS_BORDER; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case FormBorderStyle.FixedDialog: - cp.Style |= NativeMethods.WS_BORDER; - cp.ExStyle |= NativeMethods.WS_EX_DLGMODALFRAME; + cp.Style |= User32.WindowStyle.WS_BORDER; + cp.ExStyle |= User32.WindowStyle.WS_EX_DLGMODALFRAME; break; case FormBorderStyle.FixedToolWindow: - cp.Style |= NativeMethods.WS_BORDER; - cp.ExStyle |= NativeMethods.WS_EX_TOOLWINDOW; + cp.Style |= User32.WindowStyle.WS_BORDER; + cp.ExStyle |= User32.WindowStyle.WS_EX_TOOLWINDOW; break; case FormBorderStyle.SizableToolWindow: - cp.Style |= NativeMethods.WS_BORDER | NativeMethods.WS_THICKFRAME; - cp.ExStyle |= NativeMethods.WS_EX_TOOLWINDOW; + cp.Style |= User32.WindowStyle.WS_BORDER | User32.WindowStyle.WS_THICKFRAME; + cp.ExStyle |= User32.WindowStyle.WS_EX_TOOLWINDOW; break; } } @@ -3788,7 +3779,7 @@ private void FillInCreateParamsStartPosition(CreateParams cp) { // When computing the client window size, don't tell them that // we are going to be maximized! - int maskedStyle = cp.Style & ~(NativeMethods.WS_MAXIMIZE | NativeMethods.WS_MINIMIZE); + int maskedStyle = cp.Style & ~(User32.WindowStyle.WS_MAXIMIZE | User32.WindowStyle.WS_MINIMIZE); Size correct = ComputeWindowSize(ClientSize, maskedStyle, cp.ExStyle); cp.Width = correct.Width; cp.Height = correct.Height; @@ -3859,10 +3850,10 @@ private void FillInCreateParamsWindowState(CreateParams cp) switch ((FormWindowState)formState[FormStateWindowState]) { case FormWindowState.Maximized: - cp.Style |= NativeMethods.WS_MAXIMIZE; + cp.Style |= User32.WindowStyle.WS_MAXIMIZE; break; case FormWindowState.Minimized: - cp.Style |= NativeMethods.WS_MINIMIZE; + cp.Style |= User32.WindowStyle.WS_MINIMIZE; break; } } @@ -4066,14 +4057,13 @@ protected void CenterToParent() Size s = Size; IntPtr ownerHandle = IntPtr.Zero; - ownerHandle = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT); + ownerHandle = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT); if (ownerHandle != IntPtr.Zero) { Screen desktop = Screen.FromHandle(ownerHandle); Rectangle screenRect = desktop.WorkingArea; - RECT ownerRect = new RECT(); - - UnsafeNativeMethods.GetWindowRect(new HandleRef(null, ownerHandle), ref ownerRect); + var ownerRect = new RECT(); + User32.GetWindowRect(ownerHandle, ref ownerRect); p.X = (ownerRect.left + ownerRect.right - s.Width) / 2; if (p.X < screenRect.X) @@ -4123,7 +4113,7 @@ protected void CenterToScreen() IntPtr hWndOwner = IntPtr.Zero; if (TopLevel) { - hWndOwner = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT); + hWndOwner = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT); } if (hWndOwner != IntPtr.Zero) { @@ -4704,7 +4694,14 @@ protected virtual void OnDpiChanged(DpiChangedEventArgs e) SuspendAllLayout(this); try { - SafeNativeMethods.SetWindowPos(new HandleRef(this, HandleInternal), NativeMethods.NullHandleRef, e.SuggestedRectangle.X, e.SuggestedRectangle.Y, e.SuggestedRectangle.Width, e.SuggestedRectangle.Height, NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, HandleInternal), + IntPtr.Zero, + e.SuggestedRectangle.X, + e.SuggestedRectangle.Y, + e.SuggestedRectangle.Width, + e.SuggestedRectangle.Height, + User32.WindowPosition.SWP_NOZORDER | User32.WindowPosition.SWP_NOACTIVATE); if (AutoScaleMode != AutoScaleMode.Font) { Font = new Font(Font.FontFamily, Font.Size * factor, Font.Style); @@ -5105,14 +5102,12 @@ internal bool RaiseFormClosingOnAppExit() internal override void RecreateHandleCore() { - //Debug.Assert( CanRecreateHandle(), "Recreating handle when form is not ready yet." ); - NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT(); + var wp = new User32.WINDOWPLACEMENT(); FormStartPosition oldStartPosition = FormStartPosition.Manual; if (!IsMdiChild && (WindowState == FormWindowState.Minimized || WindowState == FormWindowState.Maximized)) { - wp.length = Marshal.SizeOf(); - UnsafeNativeMethods.GetWindowPlacement(new HandleRef(this, Handle), ref wp); + User32.GetWindowPlacement(new HandleRef(this, Handle), out wp); } if (StartPosition != FormStartPosition.Manual) @@ -5124,20 +5119,18 @@ internal override void RecreateHandleCore() } EnumThreadWindowsCallback etwcb = null; - SafeNativeMethods.EnumThreadWindowsCallback callback = null; + User32.EnumThreadWindowsCallback callback = null; if (IsHandleCreated) { // First put all the owned windows into a list etwcb = new EnumThreadWindowsCallback(); - if (etwcb != null) - { - callback = new SafeNativeMethods.EnumThreadWindowsCallback(etwcb.Callback); - UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(), - new NativeMethods.EnumThreadWindowsCallback(callback), - new HandleRef(this, Handle)); - // Reset the owner of the windows in the list - etwcb.ResetOwners(); - } + callback = new User32.EnumThreadWindowsCallback(etwcb.Callback); + User32.EnumThreadWindows( + Kernel32.GetCurrentThreadId(), + callback, + new HandleRef(this, Handle)); + // Reset the owner of the windows in the list + etwcb.ResetOwners(); } base.RecreateHandleCore(); @@ -5155,7 +5148,7 @@ internal override void RecreateHandleCore() if (wp.length > 0) { - UnsafeNativeMethods.SetWindowPlacement(new HandleRef(this, Handle), ref wp); + User32.SetWindowPlacement(new HandleRef(this, Handle), ref wp); } if (callback != null) @@ -5325,11 +5318,11 @@ protected override void Select(bool directed, bool forward) if (TopLevel) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(this, Handle)); + User32.SetActiveWindow(new HandleRef(this, Handle)); } else if (IsMdiChild) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(MdiParentInternal, MdiParentInternal.Handle)); + User32.SetActiveWindow(new HandleRef(MdiParentInternal, MdiParentInternal.Handle)); MdiParentInternal.MdiClient.SendMessage(WindowMessages.WM_MDIACTIVATE, Handle, 0); } else @@ -5621,15 +5614,15 @@ public void Show(IWin32Window owner) { throw new InvalidOperationException(SR.CantShowModalOnNonInteractive); } - else if ((owner != null) && ((int)UnsafeNativeMethods.GetWindowLong(new HandleRef(owner, Control.GetSafeHandle(owner)), NativeMethods.GWL_EXSTYLE) - & NativeMethods.WS_EX_TOPMOST) == 0) + else if ((owner != null) && ((int)User32.GetWindowLong(new HandleRef(owner, Control.GetSafeHandle(owner)), User32.WindowLong.GWL_EXSTYLE) + & User32.WindowStyle.WS_EX_TOPMOST) == 0) { // It's not the top-most window if (owner is Control) { owner = ((Control)owner).TopLevelControlInternal; } } - IntPtr hWndActive = UnsafeNativeMethods.GetActiveWindow(); + IntPtr hWndActive = User32.GetActiveWindow(); IntPtr hWndOwner = owner == null ? hWndActive : Control.GetSafeHandle(owner); IntPtr hWndOldOwner = IntPtr.Zero; Properties.SetObject(PropDialogOwner, owner); @@ -5641,15 +5634,15 @@ public void Show(IWin32Window owner) if (hWndOwner != IntPtr.Zero && hWndOwner != Handle) { // Catch the case of a window trying to own its owner - if (UnsafeNativeMethods.GetWindowLong(new HandleRef(owner, hWndOwner), NativeMethods.GWL_HWNDPARENT) == Handle) + if (User32.GetWindowLong(new HandleRef(owner, hWndOwner), User32.WindowLong.GWL_HWNDPARENT) == Handle) { throw new ArgumentException(string.Format(SR.OwnsSelfOrOwner, "show"), "owner"); } // Set the new owner. - hWndOldOwner = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(owner, hWndOwner)); + hWndOldOwner = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, new HandleRef(owner, hWndOwner)); } Visible = true; @@ -5697,8 +5690,8 @@ public DialogResult ShowDialog(IWin32Window owner) { throw new InvalidOperationException(SR.CantShowModalOnNonInteractive); } - else if ((owner != null) && ((int)UnsafeNativeMethods.GetWindowLong(new HandleRef(owner, Control.GetSafeHandle(owner)), NativeMethods.GWL_EXSTYLE) - & NativeMethods.WS_EX_TOPMOST) == 0) + else if ((owner != null) && ((int)User32.GetWindowLong(new HandleRef(owner, Control.GetSafeHandle(owner)), User32.WindowLong.GWL_EXSTYLE) + & User32.WindowStyle.WS_EX_TOPMOST) == 0) { // It's not the top-most window if (owner is Control) { @@ -5712,13 +5705,14 @@ public DialogResult ShowDialog(IWin32Window owner) // for modal dialogs make sure we reset close reason. CloseReason = CloseReason.None; - IntPtr hWndCapture = UnsafeNativeMethods.GetCapture(); + IntPtr hWndCapture = User32.GetCapture(); if (hWndCapture != IntPtr.Zero) { UnsafeNativeMethods.SendMessage(new HandleRef(null, hWndCapture), WindowMessages.WM_CANCELMODE, IntPtr.Zero, IntPtr.Zero); - SafeNativeMethods.ReleaseCapture(); + User32.ReleaseCapture(); } - IntPtr hWndActive = UnsafeNativeMethods.GetActiveWindow(); + + IntPtr hWndActive = User32.GetActiveWindow(); IntPtr hWndOwner = owner == null ? hWndActive : Control.GetSafeHandle(owner); IntPtr hWndOldOwner = IntPtr.Zero; Properties.SetObject(PropDialogOwner, owner); @@ -5754,15 +5748,15 @@ public DialogResult ShowDialog(IWin32Window owner) if (hWndOwner != IntPtr.Zero && hWndOwner != Handle) { // Catch the case of a window trying to own its owner - if (UnsafeNativeMethods.GetWindowLong(new HandleRef(owner, hWndOwner), NativeMethods.GWL_HWNDPARENT) == Handle) + if (User32.GetWindowLong(new HandleRef(owner, hWndOwner), User32.WindowLong.GWL_HWNDPARENT) == Handle) { throw new ArgumentException(string.Format(SR.OwnsSelfOrOwner, "showDialog"), "owner"); } // Set the new owner. - hWndOldOwner = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(owner, hWndOwner)); + hWndOldOwner = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, new HandleRef(owner, hWndOwner)); } try @@ -5781,18 +5775,18 @@ public DialogResult ShowDialog(IWin32Window owner) // Call SetActiveWindow before setting Visible = false. // - if (!UnsafeNativeMethods.IsWindow(new HandleRef(null, hWndActive))) + if (!User32.IsWindow(hWndActive)) { hWndActive = hWndOwner; } - if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWndActive)) && SafeNativeMethods.IsWindowVisible(new HandleRef(null, hWndActive))) + if (User32.IsWindow(hWndActive) && User32.IsWindowVisible(hWndActive)) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(null, hWndActive)); + User32.SetActiveWindow(hWndActive); } - else if (UnsafeNativeMethods.IsWindow(new HandleRef(null, hWndOwner)) && SafeNativeMethods.IsWindowVisible(new HandleRef(null, hWndOwner))) + else if (User32.IsWindow(hWndOwner) && User32.IsWindowVisible(hWndOwner)) { - UnsafeNativeMethods.SetActiveWindow(new HandleRef(null, hWndOwner)); + User32.SetActiveWindow(hWndOwner); } SetVisibleCore(false); @@ -6012,7 +6006,7 @@ private void UpdateHandleWithOwner() } } - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, ownerHwnd); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, ownerHwnd); } } @@ -6473,39 +6467,34 @@ private void UpdateWindowState() if (IsHandleCreated) { FormWindowState oldState = WindowState; - NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT - { - length = Marshal.SizeOf() - }; - UnsafeNativeMethods.GetWindowPlacement(new HandleRef(this, Handle), ref wp); - + User32.GetWindowPlacement(new HandleRef(this, Handle), out User32.WINDOWPLACEMENT wp); switch (wp.showCmd) { - case NativeMethods.SW_NORMAL: - case NativeMethods.SW_RESTORE: - case NativeMethods.SW_SHOW: - case NativeMethods.SW_SHOWNA: - case NativeMethods.SW_SHOWNOACTIVATE: + case User32.ShowWindowCommand.SW_NORMAL: + case User32.ShowWindowCommand.SW_RESTORE: + case User32.ShowWindowCommand.SW_SHOW: + case User32.ShowWindowCommand.SW_SHOWNA: + case User32.ShowWindowCommand.SW_SHOWNOACTIVATE: if (formState[FormStateWindowState] != (int)FormWindowState.Normal) { formState[FormStateWindowState] = (int)FormWindowState.Normal; } break; - case NativeMethods.SW_SHOWMAXIMIZED: + case User32.ShowWindowCommand.SW_SHOWMAXIMIZED: if (formState[FormStateMdiChildMax] == 0) { formState[FormStateWindowState] = (int)FormWindowState.Maximized; } break; - case NativeMethods.SW_SHOWMINIMIZED: - case NativeMethods.SW_MINIMIZE: - case NativeMethods.SW_SHOWMINNOACTIVE: + case User32.ShowWindowCommand.SW_SHOWMINIMIZED: + case User32.ShowWindowCommand.SW_MINIMIZE: + case User32.ShowWindowCommand.SW_SHOWMINNOACTIVE: if (formState[FormStateMdiChildMax] == 0) { formState[FormStateWindowState] = (int)FormWindowState.Minimized; } break; - case NativeMethods.SW_HIDE: + case User32.ShowWindowCommand.SW_HIDE: default: break; } @@ -6622,10 +6611,10 @@ private void WmCreate(ref Message m) { switch (si.wShowWindow) { - case NativeMethods.SW_MAXIMIZE: + case (short)User32.ShowWindowCommand.SW_MAXIMIZE: WindowState = FormWindowState.Maximized; break; - case NativeMethods.SW_MINIMIZE: + case (short)User32.ShowWindowCommand.SW_MINIMIZE: WindowState = FormWindowState.Minimized; break; } @@ -7360,7 +7349,7 @@ public override void Remove(Control value) // after handle recreation private class EnumThreadWindowsCallback { - private List ownedWindows; + private List ownedWindows; internal EnumThreadWindowsCallback() { @@ -7369,45 +7358,49 @@ internal EnumThreadWindowsCallback() internal bool Callback(IntPtr hWnd, IntPtr lParam) { Debug.Assert(lParam != IntPtr.Zero); - HandleRef hRef = new HandleRef(null, hWnd); - IntPtr parent = UnsafeNativeMethods.GetWindowLong(hRef, NativeMethods.GWL_HWNDPARENT); + IntPtr parent = User32.GetWindowLong(hWnd, User32.WindowLong.GWL_HWNDPARENT); if (parent == lParam) { // Enumerated window is owned by this Form. // Store it in a list for further treatment. if (ownedWindows == null) { - ownedWindows = new List(); + ownedWindows = new List(); } - ownedWindows.Add(hRef); + + ownedWindows.Add(hWnd); } + return true; } - // Resets the owner of all the windows owned by this Form before handle recreation. + /// + /// Resets the owner of all the windows owned by this Form before handle recreation. + /// internal void ResetOwners() { if (ownedWindows != null) { - foreach (HandleRef hRef in ownedWindows) + foreach (IntPtr hRef in ownedWindows) { - UnsafeNativeMethods.SetWindowLong(hRef, NativeMethods.GWL_HWNDPARENT, NativeMethods.NullHandleRef); + User32.SetWindowLong(hRef, User32.WindowLong.GWL_HWNDPARENT, IntPtr.Zero); } } } - // Sets the owner of the windows back to this Form after its handle recreation. + /// + /// Sets the owner of the windows back to this Form after its handle recreation. + /// internal void SetOwners(HandleRef hRefOwner) { if (ownedWindows != null) { - foreach (HandleRef hRef in ownedWindows) + foreach (IntPtr hRef in ownedWindows) { - UnsafeNativeMethods.SetWindowLong(hRef, NativeMethods.GWL_HWNDPARENT, hRefOwner); + User32.SetWindowLong(hRef, User32.WindowLong.GWL_HWNDPARENT, hRefOwner); } } } } } } - diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/GroupBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/GroupBox.cs index 2298f84a9a3..3618063e924 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/GroupBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/GroupBox.cs @@ -148,7 +148,7 @@ protected override CreateParams CreateParams cp.ClassName = null; cp.Style &= ~NativeMethods.BS_GROUPBOX; } - cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTROLPARENT; return cp; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Help.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Help.cs index 6fd3511004e..6d1f8cb3d48 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Help.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Help.cs @@ -8,6 +8,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; +using static Interop; namespace System.Windows.Forms { @@ -201,7 +202,7 @@ private static void ShowHTML10Help(Control parent, string url, HelpNavigator com } else { - handle = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); + handle = new HandleRef(null, User32.GetActiveWindow()); } object htmlParam; @@ -286,11 +287,11 @@ private static void ShowHTMLFile(Control parent, string url, HelpNavigator comma } else { - handle = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); + handle = new HandleRef(null, User32.GetActiveWindow()); } Debug.WriteLineIf(Help.WindowsFormsHelpTrace.TraceVerbose, "\tExecuting '" + file.ToString() + "'"); - UnsafeNativeMethods.ShellExecute_NoBFM(handle, null, file.ToString(), null, null, NativeMethods.SW_NORMAL); + Interop.Shell32.ShellExecuteA(handle, null, file.ToString(), null, null, User32.ShowWindowCommand.SW_NORMAL); } private static Uri Resolve(string partialUri) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs b/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs index 23d6f768f36..668fcc43cd9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/KeyboardToolTipStateMachine.cs @@ -153,7 +153,7 @@ internal IKeyboardToolTip LastFocusedTool return tool; } - return Control.FromHandle(UnsafeNativeMethods.GetFocus()); + return Control.FromHandle(User32.GetFocus()); } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs index c0743bdb1ee..16761aeb3a2 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Label.cs @@ -307,7 +307,7 @@ protected override CreateParams CreateParams // Since we're owner draw, I don't see any point in setting the // SS_CENTER/SS_RIGHT styles. // - cp.ExStyle &= ~NativeMethods.WS_EX_RIGHT; // WS_EX_RIGHT overrides the SS_XXXX alignment styles + cp.ExStyle &= ~User32.WindowStyle.WS_EX_RIGHT; // WS_EX_RIGHT overrides the SS_XXXX alignment styles } if (!OwnerDraw) @@ -341,7 +341,7 @@ protected override CreateParams CreateParams switch (BorderStyle) { case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; case BorderStyle.Fixed3D: cp.Style |= NativeMethods.SS_SUNKEN; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/LinkLabel.cs b/src/System.Windows.Forms/src/System/Windows/Forms/LinkLabel.cs index e6d9a87e56c..223a06fc384 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/LinkLabel.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/LinkLabel.cs @@ -422,8 +422,8 @@ protected Cursor OverrideCursor // This includes the case where the mouse is over one of our children var r = new RECT(); UnsafeNativeMethods.GetCursorPos(out Point p); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); - if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || UnsafeNativeMethods.GetCapture() == Handle) + User32.GetWindowRect(new HandleRef(this, Handle), ref r); + if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || User32.GetCapture() == Handle) { SendMessage(WindowMessages.WM_SETCURSOR, Handle, NativeMethods.HTCLIENT); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs index 2a623c4e607..399947813e9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ListBox.cs @@ -325,7 +325,7 @@ protected override CreateParams CreateParams CreateParams cp = base.CreateParams; cp.ClassName = "LISTBOX"; - cp.Style |= NativeMethods.WS_VSCROLL | NativeMethods.LBS_NOTIFY | NativeMethods.LBS_HASSTRINGS; + cp.Style |= User32.WindowStyle.WS_VSCROLL | NativeMethods.LBS_NOTIFY | NativeMethods.LBS_HASSTRINGS; if (scrollAlwaysVisible) { cp.Style |= NativeMethods.LBS_DISABLENOSCROLL; @@ -344,20 +344,20 @@ protected override CreateParams CreateParams switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } if (multiColumn) { - cp.Style |= NativeMethods.LBS_MULTICOLUMN | NativeMethods.WS_HSCROLL; + cp.Style |= NativeMethods.LBS_MULTICOLUMN | User32.WindowStyle.WS_HSCROLL; } else if (horizontalScrollbar) { - cp.Style |= NativeMethods.WS_HSCROLL; + cp.Style |= User32.WindowStyle.WS_HSCROLL; } switch (selectionMode) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs index a186145a915..8de6fc39ec3 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs @@ -716,8 +716,8 @@ protected override CreateParams CreateParams // if (IsHandleCreated) { - int currentStyle = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); - cp.Style |= (currentStyle & (NativeMethods.WS_HSCROLL | NativeMethods.WS_VSCROLL)); + int currentStyle = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); + cp.Style |= (currentStyle & (User32.WindowStyle.WS_HSCROLL | User32.WindowStyle.WS_VSCROLL)); } cp.Style |= NativeMethods.LVS_SHAREIMAGELISTS; @@ -740,10 +740,10 @@ protected override CreateParams CreateParams switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } @@ -806,9 +806,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Form explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; } @@ -4540,9 +4540,9 @@ protected override void OnHandleCreated(EventArgs e) // if (!Scrollable) { - int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); + int style = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); style |= NativeMethods.LVS_NOSCROLL; - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)style)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)style); } // in VirtualMode we have to tell the list view to ask for the list view item's state image index @@ -4827,7 +4827,7 @@ protected virtual void OnVirtualItemsSelectionRangeChanged(ListViewVirtualItemsS private unsafe void PositionHeader() { - IntPtr hdrHWND = UnsafeNativeMethods.GetWindow(new HandleRef(this, Handle), NativeMethods.GW_CHILD); + IntPtr hdrHWND = User32.GetWindow(new HandleRef(this, Handle), User32.GetWindowOption.GW_CHILD); if (hdrHWND != IntPtr.Zero) { @@ -4866,13 +4866,14 @@ private unsafe void PositionHeader() NativeMethods.WINDOWPOS wpos = Marshal.PtrToStructure(pwpos); // position the header control - SafeNativeMethods.SetWindowPos(new HandleRef(this, hdrHWND), - new HandleRef(this, wpos.hwndInsertAfter), - wpos.x, - wpos.y, - wpos.cx, - wpos.cy, - wpos.flags | NativeMethods.SWP_SHOWWINDOW); + User32.SetWindowPos( + new HandleRef(this, hdrHWND), + new HandleRef(this, wpos.hwndInsertAfter), + wpos.x, + wpos.y, + wpos.cx, + wpos.cy, + wpos.flags | User32.WindowPosition.SWP_SHOWWINDOW); } finally { @@ -5337,7 +5338,7 @@ internal void SetToolTip(ToolTip toolTip, string toolTipCaption) this.toolTipCaption = toolTipCaption; //native ListView expects tooltip HWND as a wParam and ignores lParam IntPtr oldHandle = UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.LVM_SETTOOLTIPS, new HandleRef(toolTip, toolTip.Handle), 0); - UnsafeNativeMethods.DestroyWindow(new HandleRef(null, oldHandle)); + User32.DestroyWindow(oldHandle); } internal void SetItemImage(int index, int image) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MDIClient.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MDIClient.cs index 60f6d0d3248..ab9a0859f8a 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MDIClient.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MDIClient.cs @@ -96,22 +96,22 @@ protected override CreateParams CreateParams // Add the style MDIS_ALLCHILDSTYLES // so that MDI Client windows can have the WS_VISIBLE style removed from the window style // to make them not visible but still present. - cp.Style |= NativeMethods.WS_VSCROLL | NativeMethods.WS_HSCROLL; - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.Style |= User32.WindowStyle.WS_VSCROLL | User32.WindowStyle.WS_HSCROLL; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; cp.Param = new NativeMethods.CLIENTCREATESTRUCT(IntPtr.Zero, 1); ISite site = ParentInternal?.Site; if (site != null && site.DesignMode) { - cp.Style |= NativeMethods.WS_DISABLED; + cp.Style |= User32.WindowStyle.WS_DISABLED; SetState(STATE_ENABLED, false); } if (RightToLeft == RightToLeft.Yes && ParentInternal != null && ParentInternal.IsMirrored) { //We want to turn on mirroring for MdiClient explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL | NativeMethods.WS_EX_NOINHERITLAYOUT; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL | User32.WindowStyle.WS_EX_NOINHERITLAYOUT; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; @@ -226,12 +226,6 @@ protected override void SetBoundsCore(int x, int y, int width, int height, Bound // NOTE: This logic is to keep minimized MDI children anchored to // the bottom left of the client area, normally they are anchored // to the top right which just looks wierd! - // - NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT - { - length = Marshal.SizeOf() - }; - for (int i = 0; i < Controls.Count; i++) { Control ctl = Controls[i]; @@ -242,7 +236,7 @@ protected override void SetBoundsCore(int x, int y, int width, int height, Bound // them from being re-displayed. if (child.CanRecreateHandle() && child.WindowState == FormWindowState.Minimized) { - UnsafeNativeMethods.GetWindowPlacement(new HandleRef(child, child.Handle), ref wp); + User32.GetWindowPlacement(new HandleRef(child, child.Handle), out User32.WINDOWPLACEMENT wp); wp.ptMinPosition.Y -= yDelta; if (wp.ptMinPosition.Y == -1) { @@ -256,7 +250,7 @@ protected override void SetBoundsCore(int x, int y, int width, int height, Bound } } wp.flags = NativeMethods.WPF_SETMINPOSITION; - UnsafeNativeMethods.SetWindowPlacement(new HandleRef(child, child.Handle), ref wp); + User32.SetWindowPlacement(new HandleRef(child, child.Handle), ref wp); wp.flags = 0; } } @@ -305,7 +299,7 @@ private void SetWindowRgn() throw new InvalidOperationException(SR.ErrorSettingWindowRegion); } - if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, Handle), new HandleRef(null, rgn1), true) == 0) + if (User32.SetWindowRgn(new HandleRef(this, Handle), rgn1, true) == 0) { throw new InvalidOperationException(SR.ErrorSettingWindowRegion); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MaskedTextBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MaskedTextBox.cs index 02f07edd22b..5c950c1db09 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MaskedTextBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MaskedTextBox.cs @@ -338,7 +338,7 @@ protected override CreateParams CreateParams // Translate for Rtl if necessary // HorizontalAlignment align = RtlTranslateHorizontal(textAlign); - cp.ExStyle &= ~NativeMethods.WS_EX_RIGHT; // WS_EX_RIGHT overrides the ES_XXXX alignment styles + cp.ExStyle &= ~User32.WindowStyle.WS_EX_RIGHT; // WS_EX_RIGHT overrides the ES_XXXX alignment styles switch (align) { case HorizontalAlignment.Left: diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs index 3b062c3d6ea..a4334298d07 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MessageBox.cs @@ -391,7 +391,7 @@ private static DialogResult ShowCore(IWin32Window owner, string text, string cap { if (owner == null) { - handle = UnsafeNativeMethods.GetActiveWindow(); + handle = User32.GetActiveWindow(); } else { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/MonthCalendar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/MonthCalendar.cs index 21e332d9b0c..ca40ebc87ef 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/MonthCalendar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/MonthCalendar.cs @@ -384,9 +384,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Form explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs b/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs index d459741520a..12153074288 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/NativeWindow.cs @@ -63,7 +63,7 @@ public class NativeWindow : MarshalByRefObject, IWin32Window, IHandle private static readonly object internalSyncObject = new object(); private static readonly object createWindowSyncObject = new object(); - private NativeMethods.WndProc windowProc; + private User32.WndProc windowProc; private static IntPtr _defaultWindowProc; private IntPtr windowProcPtr; private IntPtr defWindowProc; @@ -102,9 +102,9 @@ public NativeWindow() /// /// Cache window DpiContext awareness information that helps to create handle with right context at the later time. /// - internal DpiAwarenessContext DpiAwarenessContext { get; } = DpiHelper.IsScalingRequirementMet - ? CommonUnsafeNativeMethods.TryGetThreadDpiAwarenessContext() - : DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; + internal User32.DpiAwarenessContext DpiAwarenessContext { get; } = DpiHelper.IsScalingRequirementMet + ? User32.GetCurrentThreadDpiAwarenessContext() + : User32.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; /// /// Override's the base object's finalize method. @@ -136,15 +136,15 @@ internal void ForceExitMessageLoop() // a WM_NULL to the window. Why? Because if the main ui thread is INSIDE // the wndproc for this control during our unsubclass, then we could AV // when control finally reaches us. - if (UnsafeNativeMethods.IsWindow(new HandleRef(null, Handle))) + if (User32.IsWindow(Handle)) { - int id = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, Handle), out int lpdwProcessId); + int id = User32.GetWindowThreadProcessId(Handle, out int lpdwProcessId); Application.ThreadContext ctx = Application.ThreadContext.FromId(id); IntPtr threadHandle = (ctx == null ? IntPtr.Zero : ctx.GetHandle()); if (threadHandle != IntPtr.Zero) { - SafeNativeMethods.GetExitCodeThread(new HandleRef(null, threadHandle), out int exitCode); + Kernel32.GetExitCodeThread(threadHandle, out uint exitCode); if (!AppDomain.CurrentDomain.IsFinalizingForUnload() && exitCode == NativeMethods.STATUS_PENDING) { if (UnsafeNativeMethods.SendMessageTimeout(new HandleRef(null, Handle), @@ -417,7 +417,7 @@ internal static void AddWindowToIDTable(object wrapper, IntPtr handle) { NativeWindow.hashForIdHandle[NativeWindow.globalID] = handle; NativeWindow.hashForHandleId[handle] = NativeWindow.globalID; - UnsafeNativeMethods.SetWindowLong(new HandleRef(wrapper, handle), NativeMethods.GWL_ID, new HandleRef(wrapper, (IntPtr)globalID)); + User32.SetWindowLong(new HandleRef(wrapper, handle), User32.WindowLong.GWL_ID, new HandleRef(wrapper, (IntPtr)globalID)); globalID++; } @@ -439,30 +439,30 @@ internal void AssignHandle(IntPtr handle, bool assignUniqueID) this.Handle = handle; - defWindowProc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC); + defWindowProc = User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC); Debug.Assert(defWindowProc != IntPtr.Zero, "defWindowProc is 0"); if (WndProcShouldBeDebuggable) { Debug.WriteLineIf(WndProcChoice.TraceVerbose, "Using debuggable wndproc"); - windowProc = new NativeMethods.WndProc(DebuggableCallback); + windowProc = new User32.WndProc(DebuggableCallback); } else { Debug.WriteLineIf(WndProcChoice.TraceVerbose, "Using normal wndproc"); - windowProc = new NativeMethods.WndProc(Callback); + windowProc = new User32.WndProc(Callback); } AddWindowToTable(handle, this); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC, windowProc); - windowProcPtr = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC); + User32.SetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC, windowProc); + windowProcPtr = User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_WNDPROC); Debug.Assert(defWindowProc != windowProcPtr, "Uh oh! Subclassed ourselves!!!"); if (assignUniqueID && - (unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_STYLE))) & NativeMethods.WS_CHILD) != 0 && - unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_ID))) == 0) + (unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_STYLE))) & User32.WindowStyle.WS_CHILD) != 0 && + unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_ID))) == 0) { - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_ID, new HandleRef(this, handle)); + User32.SetWindowLong(new HandleRef(this, handle), User32.WindowLong.GWL_ID, new HandleRef(this, handle)); } if (suppressedGC) @@ -539,7 +539,7 @@ public virtual void CreateHandle(CreateParams cp) lock (this) { CheckReleased(); - WindowClass windowClass = WindowClass.Create(cp.ClassName, (NativeMethods.ClassStyle)cp.ClassStyle); + WindowClass windowClass = WindowClass.Create(cp.ClassName, (User32.ClassStyle)cp.ClassStyle); lock (createWindowSyncObject) { // The CLR will sometimes pump messages while we're waiting on the lock. @@ -572,7 +572,7 @@ public virtual void CreateHandle(CreateParams cp) cp.Caption = cp.Caption.Substring(0, short.MaxValue); } - createResult = UnsafeNativeMethods.CreateWindowEx( + createResult = User32.CreateWindowExW( cp.ExStyle, windowClass._windowClassName, cp.Caption, @@ -582,8 +582,8 @@ public virtual void CreateHandle(CreateParams cp) cp.Width, cp.Height, new HandleRef(cp, cp.Parent), - NativeMethods.NullHandleRef, - new HandleRef(null, modHandle), + IntPtr.Zero, + modHandle, cp.Param); lastWin32Error = Marshal.GetLastWin32Error(); @@ -658,13 +658,13 @@ public void DefWndProc(ref Message m) { Debug.Fail($"Can't find a default window procedure for message {m} on class {GetType().Name}"); - // At this point, there isn't much we can do. There's a - // small chance the following line will allow the rest of - // the program to run, but don't get your hopes up. - m.Result = UnsafeNativeMethods.DefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam); + // At this point, there isn't much we can do. There's a small chance the following line will + // allow the rest of the program to run, but don't get your hopes up. + m.Result = User32.DefWindowProcW(m.HWnd, m.Msg, m.WParam, m.LParam); return; } - m.Result = UnsafeNativeMethods.CallWindowProc(defWindowProc, m.HWnd, m.Msg, m.WParam, m.LParam); + + m.Result = User32.CallWindowProcW(defWindowProc, m.HWnd, m.Msg, m.WParam, m.LParam); } else { @@ -684,7 +684,7 @@ public virtual void DestroyHandle() { if (Handle != IntPtr.Zero) { - if (!UnsafeNativeMethods.DestroyWindow(new HandleRef(this, Handle))) + if (!User32.DestroyWindow(new HandleRef(this, Handle))) { UnSubclass(); //then post a close and let it do whatever it needs to do on its own. @@ -933,8 +933,8 @@ private static void OnShutdown(object sender, EventArgs e) if (b.handle != IntPtr.Zero && b.handle != new IntPtr(-1)) { HandleRef href = new HandleRef(b, b.handle); - UnsafeNativeMethods.SetWindowLong(href, NativeMethods.GWL_WNDPROC, new HandleRef(null, DefaultWindowProc)); - UnsafeNativeMethods.SetClassLong(href, NativeMethods.GCL_WNDPROC, DefaultWindowProc); + User32.SetWindowLong(href, User32.WindowLong.GWL_WNDPROC, DefaultWindowProc); + User32.SetClassLong(href, User32.ClassLong.GCL_WNDPROC, DefaultWindowProc); UnsafeNativeMethods.PostMessage(href, WindowMessages.WM_CLOSE, 0, 0); // Fish out the Window object, if it is valid, and NULL the handle pointer. This @@ -1214,7 +1214,7 @@ private void UnSubclass() // Don't touch if the current window proc is not ours. - IntPtr currentWinPrc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_WNDPROC); + IntPtr currentWinPrc = User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_WNDPROC); if (windowProcPtr == currentWinPrc) { if (PreviousWindow == null) @@ -1222,7 +1222,7 @@ private void UnSubclass() // If the defWindowProc points to a native window proc, previousWindow will // be null. In this case, it is completely safe to assign defWindowProc // to the current wndproc. - UnsafeNativeMethods.SetWindowLong(href, NativeMethods.GWL_WNDPROC, new HandleRef(this, defWindowProc)); + User32.SetWindowLong(href, User32.WindowLong.GWL_WNDPROC, new HandleRef(this, defWindowProc)); } else { @@ -1233,14 +1233,14 @@ private void UnSubclass() // holding a ref to it, and it is holding a ref to us. The only way this cycle will // finalize is if no one else is hanging onto it. So, we re-assign the window proc to // userDefWindowProc. - UnsafeNativeMethods.SetWindowLong(href, NativeMethods.GWL_WNDPROC, new HandleRef(this, DefaultWindowProc)); + User32.SetWindowLong(href, User32.WindowLong.GWL_WNDPROC, new HandleRef(this, DefaultWindowProc)); } else { // Here we are not finalizing so we use the windowProc for our previous window. This may // DIFFER from the value we are currently storing in defWindowProc because someone may // have re-subclassed. - UnsafeNativeMethods.SetWindowLong(href, NativeMethods.GWL_WNDPROC, PreviousWindow.windowProc); + User32.SetWindowLong(href, User32.WindowLong.GWL_WNDPROC, PreviousWindow.windowProc); } } } @@ -1257,7 +1257,7 @@ private void UnSubclass() if (nextWindow == null || nextWindow.defWindowProc != windowProcPtr) { // we didn't find it... let's unhook anyway and cut the chain... this prevents crashes - UnsafeNativeMethods.SetWindowLong(href, NativeMethods.GWL_WNDPROC, new HandleRef(this, DefaultWindowProc)); + User32.SetWindowLong(href, User32.WindowLong.GWL_WNDPROC, new HandleRef(this, DefaultWindowProc)); } } } @@ -1304,11 +1304,11 @@ private class WindowClass internal WindowClass _next; internal string _className; - internal NativeMethods.ClassStyle _classStyle; + internal User32.ClassStyle _classStyle; internal string _windowClassName; internal int _hashCode; internal IntPtr _defaultWindowProc; - internal NativeMethods.WndProc _windowProc; + internal User32.WndProc _windowProc; internal NativeWindow _targetWindow; // There is only ever one AppDomain @@ -1316,7 +1316,7 @@ private class WindowClass private static readonly object s_wcInternalSyncObject = new object(); - internal WindowClass(string className, NativeMethods.ClassStyle classStyle) + internal WindowClass(string className, User32.ClassStyle classStyle) { _className = className; _classStyle = classStyle; @@ -1328,7 +1328,7 @@ public IntPtr Callback(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) Debug.Assert(hWnd != IntPtr.Zero, "Windows called us with an HWND of 0"); // Set the window procedure to the default window procedure - UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hWnd), NativeMethods.GWL_WNDPROC, new HandleRef(this, _defaultWindowProc)); + User32.SetWindowLong(hWnd, User32.WindowLong.GWL_WNDPROC, new HandleRef(this, _defaultWindowProc)); _targetWindow.AssignHandle(hWnd); return _targetWindow.Callback(hWnd, msg, wparam, lparam); } @@ -1338,7 +1338,7 @@ public IntPtr Callback(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) /// object if there is no such class/style available, or retrun a /// cached object if one exists. /// - internal static WindowClass Create(string className, NativeMethods.ClassStyle classStyle) + internal static WindowClass Create(string className, User32.ClassStyle classStyle) { lock (s_wcInternalSyncObject) { @@ -1410,7 +1410,7 @@ private string GetFullClassName(string className) /// private unsafe void RegisterClass() { - NativeMethods.WNDCLASS windowClass = new NativeMethods.WNDCLASS(); + var windowClass = new User32.WNDCLASS(); string localClassName = _className; @@ -1431,7 +1431,7 @@ private unsafe void RegisterClass() { // A system defined Window class was specified, get its info - if (!UnsafeNativeMethods.GetClassInfoW(NativeMethods.NullHandleRef, _className, ref windowClass)) + if (!User32.GetClassInfoW(IntPtr.Zero, _className, ref windowClass)) { throw new Win32Exception(Marshal.GetLastWin32Error(), SR.InvalidWndClsName); } @@ -1442,7 +1442,7 @@ private unsafe void RegisterClass() } _windowClassName = GetFullClassName(localClassName); - _windowProc = new NativeMethods.WndProc(Callback); + _windowProc = Callback; windowClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_windowProc); windowClass.hInstance = Kernel32.GetModuleHandleW(null); @@ -1450,7 +1450,7 @@ private unsafe void RegisterClass() { windowClass.lpszClassName = c; - if (UnsafeNativeMethods.RegisterClassW(ref windowClass) == 0) + if (User32.RegisterClassW(ref windowClass) == 0) { _windowProc = null; throw new Win32Exception(); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/NotifyIcon.cs b/src/System.Windows.Forms/src/System/Windows/Forms/NotifyIcon.cs index f2591e6b4d8..455ee3f8fd7 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/NotifyIcon.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/NotifyIcon.cs @@ -35,7 +35,7 @@ public sealed class NotifyIcon : Component private static readonly object EVENT_BALLOONTIPCLOSED = new object(); private const int WM_TRAYMOUSEMESSAGE = WindowMessages.WM_USER + 1024; - private static readonly int WM_TASKBARCREATED = SafeNativeMethods.RegisterWindowMessage("TaskbarCreated"); + private static readonly int WM_TASKBARCREATED = User32.RegisterWindowMessageW("TaskbarCreated"); private readonly object syncObj = new object(); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Panel.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Panel.cs index 44f0fd4712a..1f6cd671102 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Panel.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Panel.cs @@ -6,6 +6,7 @@ using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms.Layout; +using static Interop; namespace System.Windows.Forms { @@ -128,18 +129,18 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTROLPARENT; - cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); - cp.Style &= (~NativeMethods.WS_BORDER); + cp.ExStyle &= (~User32.WindowStyle.WS_EX_CLIENTEDGE); + cp.Style &= (~User32.WindowStyle.WS_BORDER); switch (_borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } return cp; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs index ac0bb86fe29..727002f56d2 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PictureBox.cs @@ -11,6 +11,7 @@ using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms.Layout; +using static Interop; namespace System.Windows.Forms { @@ -200,10 +201,10 @@ protected override CreateParams CreateParams switch (_borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs index 4a0cbfc8bc0..760e40afc96 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PageSetupDialog.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Runtime.InteropServices; using System.Text; +using static Interop; namespace System.Windows.Forms { @@ -349,12 +350,12 @@ private static void UpdateSettings(NativeMethods.PAGESETUPDLG data, PageSettings protected override bool RunDialog(IntPtr hwndOwner) { - NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(HookProc); if (pageSettings == null) { throw new ArgumentException(SR.PSDcantShowWithoutPage); } + var hookProcPtr = new User32.WndProc(HookProc); NativeMethods.PAGESETUPDLG data = new NativeMethods.PAGESETUPDLG(); data.lStructSize = Marshal.SizeOf(data); data.Flags = GetFlags(); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs index 084d9a442c9..4394a0c2b66 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintDialog.cs @@ -5,6 +5,7 @@ using System.ComponentModel; using System.Drawing.Printing; using System.Runtime.InteropServices; +using static Interop; namespace System.Windows.Forms { @@ -352,7 +353,7 @@ internal static NativeMethods.PRINTDLGEX CreatePRINTDLGEX() protected override bool RunDialog(IntPtr hwndOwner) { - var hookProcPtr = new NativeMethods.WndProc(HookProc); + var hookProcPtr = new User32.WndProc(HookProc); if (!UseEXDialog) { @@ -366,7 +367,7 @@ protected override bool RunDialog(IntPtr hwndOwner) } } - private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.WndProc hookProcPtr, NativeMethods.PRINTDLG data) + private bool ShowPrintDialog(IntPtr hwndOwner, User32.WndProc hookProcPtr, NativeMethods.PRINTDLG data) { data.Flags = GetFlags(); data.nCopies = (short)PrinterSettings.Copies; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs index 84120e2e2b9..19a79350ce9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -151,8 +151,8 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style |= NativeMethods.WS_HSCROLL; - cp.Style |= NativeMethods.WS_VSCROLL; + cp.Style |= User32.WindowStyle.WS_HSCROLL; + cp.Style |= User32.WindowStyle.WS_VSCROLL; return cp; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ProgressBar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ProgressBar.cs index 6c14735f77a..587dfaeffa9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ProgressBar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ProgressBar.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices; using System.Windows.Forms.Layout; using Microsoft.Win32; +using static Interop; namespace System.Windows.Forms { @@ -82,9 +83,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Form explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGrid.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGrid.cs index f3fe401a2cc..6211ed2cf04 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGrid.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGrid.cs @@ -4001,31 +4001,18 @@ protected override bool ProcessDialogKey(Keys keyData) // if we're not hosted in a windows forms thing, just give the parent the focus if (!result && Parent == null) { - IntPtr hWndParent = UnsafeNativeMethods.GetParent(new HandleRef(this, Handle)); + IntPtr hWndParent = User32.GetParent(new HandleRef(this, Handle)); if (hWndParent != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, hWndParent)); + User32.SetFocus(hWndParent); } } + return result; } } - return true; - /* This conflicts with VS tab linking - case Keys.Prior: // PAGE_UP - if ((keyData & Keys.Control) != 0) { - SelectPriorView(); - return true; - } - break; - case Keys.Next: //PAGE_DOWN - if ((keyData & Keys.Control) != 0) { - SelectNextView(); - return true; - } - break; - */ + return true; } return base.ProcessDialogKey(keyData); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridToolTip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridToolTip.cs index bb32200da55..1bd941efd63 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridToolTip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/GridToolTip.cs @@ -138,10 +138,10 @@ private void SetupToolTip(Control control) { if (IsHandleCreated) { - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.HWND_TOPMOST, - 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | - NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOPMOST, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOACTIVATE); ComCtl32.ToolInfoWrapper info = GetTOOLINFO(control); if (info.SendMessage(this, WindowMessages.TTM_ADDTOOLW) == IntPtr.Zero) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/PropertyGridView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/PropertyGridView.cs index 32cf544306e..1f8f0a8a536 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/PropertyGridView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/PropertyGridInternal/PropertyGridView.cs @@ -442,8 +442,8 @@ internal bool DrawValuesRightToLeft { if (edit != null && edit.IsHandleCreated) { - int exStyle = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(edit, edit.Handle), NativeMethods.GWL_EXSTYLE))); - return ((exStyle & NativeMethods.WS_EX_RTLREADING) != 0); + int exStyle = unchecked((int)((long)User32.GetWindowLong(new HandleRef(edit, edit.Handle), User32.WindowLong.GWL_EXSTYLE))); + return ((exStyle & User32.WindowStyle.WS_EX_RTLREADING) != 0); } else { @@ -1116,8 +1116,7 @@ private void CommonEditorHide(bool always) if (IsHandleCreated && Visible && Enabled) { - - gotfocus = IntPtr.Zero != UnsafeNativeMethods.SetFocus(new HandleRef(this, Handle)); + gotfocus = IntPtr.Zero != User32.SetFocus(new HandleRef(this, Handle)); } } @@ -1935,9 +1934,9 @@ public virtual int GetValueWidth() // control is a top=level window. standard way of setparent on the control is prohibited for top-level controls. // It is unknown why this control was created as a top-level control. Windows does not recommend this way of setting parent. // We are not touching this for this relase. We may revisit it in next release. - UnsafeNativeMethods.SetWindowLong(new HandleRef(dropDownHolder, dropDownHolder.Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(this, Handle)); + User32.SetWindowLong(new HandleRef(dropDownHolder, dropDownHolder.Handle), User32.WindowLong.GWL_HWNDPARENT, new HandleRef(this, Handle)); dropDownHolder.SetBounds(loc.X, loc.Y, size.Width, size.Height); - SafeNativeMethods.ShowWindow(new HandleRef(dropDownHolder, dropDownHolder.Handle), NativeMethods.SW_SHOWNA); + User32.ShowWindow(new HandleRef(dropDownHolder, dropDownHolder.Handle), User32.ShowWindowCommand.SW_SHOWNA); Edit.Filter = true; dropDownHolder.Visible = true; dropDownHolder.FocusComponent(); @@ -3147,8 +3146,8 @@ private void OnEditLostFocus(object sender, EventArgs e) if (dropDownHolder != null && dropDownHolder.Visible) { bool found = false; - for (IntPtr hwnd = UnsafeNativeMethods.GetForegroundWindow(); - hwnd != IntPtr.Zero; hwnd = UnsafeNativeMethods.GetParent(new HandleRef(null, hwnd))) + for (IntPtr hwnd = User32.GetForegroundWindow(); + hwnd != IntPtr.Zero; hwnd = User32.GetParent(hwnd)) { if (hwnd == dropDownHolder.Handle) { @@ -3749,8 +3748,8 @@ protected override void OnMouseMove(MouseEventArgs me) // Ensure that tooltips don't display when host application is not foreground app. // Assume that we don't want to display the tooltips - IntPtr foregroundWindow = UnsafeNativeMethods.GetForegroundWindow(); - if (UnsafeNativeMethods.IsChild(new HandleRef(null, foregroundWindow), new HandleRef(null, Handle))) + IntPtr foregroundWindow = User32.GetForegroundWindow(); + if (User32.IsChild(foregroundWindow, Handle)) { // Don't show the tips if a dropdown is showing if ((dropDownHolder == null || dropDownHolder.Component == null) || rowMoveCur == selectedRow) @@ -4570,14 +4569,14 @@ protected override bool ProcessDialogKey(Keys keyData) bool forward = (keyData & Keys.Shift) == 0; - Control focusedControl = Control.FromHandle(UnsafeNativeMethods.GetFocus()); + Control focusedControl = Control.FromHandle(User32.GetFocus()); if (focusedControl == null || !IsMyChild(focusedControl)) { if (forward) { TabSelection(); - focusedControl = Control.FromHandle(UnsafeNativeMethods.GetFocus()); + focusedControl = Control.FromHandle(User32.GetFocus()); // make sure the value actually took the focus if (IsMyChild(focusedControl)) { @@ -5697,7 +5696,7 @@ private bool SetScrollbarLength() } } - IntPtr priorFocus = UnsafeNativeMethods.GetFocus(); + IntPtr priorFocus = User32.GetFocus(); IUIService service = (IUIService)GetService(typeof(IUIService)); DialogResult result; @@ -5712,7 +5711,7 @@ private bool SetScrollbarLength() if (priorFocus != IntPtr.Zero) { - UnsafeNativeMethods.SetFocus(new HandleRef(null, priorFocus)); + User32.SetFocus(priorFocus); } return result; @@ -6347,11 +6346,11 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_TOOLWINDOW; - cp.Style |= NativeMethods.WS_POPUP | NativeMethods.WS_BORDER; + cp.ExStyle |= User32.WindowStyle.WS_EX_TOOLWINDOW; + cp.Style |= User32.WindowStyle.WS_POPUP | User32.WindowStyle.WS_BORDER; if (OSFeature.IsPresent(SystemParameter.DropShadow)) { - cp.ClassStyle |= (int)NativeMethods.ClassStyle.CS_DROPSHADOW; + cp.ClassStyle |= (int)User32.ClassStyle.CS_DROPSHADOW; } if (gridView != null) { @@ -6562,7 +6561,7 @@ private bool OwnsWindow(IntPtr hWnd) { while (hWnd != IntPtr.Zero) { - hWnd = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWnd), NativeMethods.GWL_HWNDPARENT); + hWnd = User32.GetWindowLong(hWnd, User32.WindowLong.GWL_HWNDPARENT); if (hWnd == IntPtr.Zero) { return false; @@ -7041,8 +7040,8 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.Style &= ~NativeMethods.WS_BORDER; - cp.ExStyle &= ~NativeMethods.WS_EX_CLIENTEDGE; + cp.Style &= ~User32.WindowStyle.WS_BORDER; + cp.ExStyle &= ~User32.WindowStyle.WS_EX_CLIENTEDGE; return cp; } } @@ -7841,19 +7840,15 @@ private unsafe bool WmNotify(ref Message m) protected override void WndProc(ref Message m) { - - if (filter) + if (filter && psheet.FilterEditWndProc(ref m)) { - if (psheet.FilterEditWndProc(ref m)) - { return; - } } switch (m.Msg) { case WindowMessages.WM_STYLECHANGED: - if ((unchecked((int)(long)m.WParam) & NativeMethods.GWL_EXSTYLE) != 0) + if ((unchecked((User32.WindowLong)(long)m.WParam) & User32.WindowLong.GWL_EXSTYLE) != 0) { psheet.Invalidate(); } @@ -8158,16 +8153,17 @@ private void HookMouse() if (thisProcessID == 0) { - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(control, control.Handle), out thisProcessID); + User32.GetWindowThreadProcessId(new HandleRef(control, control.Handle), out thisProcessID); } - NativeMethods.HookProc hook = new NativeMethods.HookProc(new MouseHookObject(this).Callback); + var hook = new User32.HookProc(new MouseHookObject(this).Callback); mouseHookRoot = GCHandle.Alloc(hook); - mouseHookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE, - hook, - NativeMethods.NullHandleRef, - SafeNativeMethods.GetCurrentThreadId()); + mouseHookHandle = User32.SetWindowsHookExW( + User32.WindowHookProcedure.WH_MOUSE, + hook, + IntPtr.Zero, + Kernel32.GetCurrentThreadId()); Debug.Assert(mouseHookHandle != IntPtr.Zero, "Failed to install mouse hook"); Debug.WriteLineIf(CompModSwitches.DebugGridView.TraceVerbose, "DropDownHolder:HookMouse()"); } @@ -8203,7 +8199,7 @@ private IntPtr MouseHookProc(int nCode, IntPtr wparam, IntPtr lparam) } } - return UnsafeNativeMethods.CallNextHookEx(new HandleRef(this, mouseHookHandle), nCode, wparam, lparam); + return User32.CallNextHookEx(new HandleRef(this, mouseHookHandle), nCode, wparam, lparam); } /// @@ -8217,7 +8213,7 @@ private void UnhookMouse() { if (mouseHookHandle != IntPtr.Zero) { - UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(this, mouseHookHandle)); + User32.UnhookWindowsHookEx(new HandleRef(this, mouseHookHandle)); mouseHookRoot.Free(); mouseHookHandle = IntPtr.Zero; Debug.WriteLineIf(CompModSwitches.DebugGridView.TraceVerbose, "DropDownHolder:UnhookMouse()"); @@ -8251,7 +8247,7 @@ private bool ProcessMouseDown(IntPtr hWnd, int x, int y) Debug.Assert(thisProcessID != 0, "Didn't get our process id!"); // make sure the window is in our process - SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, hWndAtPoint), out int pid); + User32.GetWindowThreadProcessId(hWndAtPoint, out int pid); // if this isn't our process, unhook the mouse. if (pid != thisProcessID) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/RichTextBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/RichTextBox.cs index eab2182eb0f..5188da686d7 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/RichTextBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/RichTextBox.cs @@ -369,7 +369,7 @@ protected override CreateParams CreateParams if (((int)ScrollBars & RichTextBoxConstants.RTB_HORIZ) != 0 && !WordWrap) { // RichEd infers word wrap from the absence of horizontal scroll bars - cp.Style |= NativeMethods.WS_HSCROLL; + cp.Style |= User32.WindowStyle.WS_HSCROLL; if (((int)ScrollBars & RichTextBoxConstants.RTB_FORCE) != 0) { cp.Style |= RichTextBoxConstants.ES_DISABLENOSCROLL; @@ -378,7 +378,7 @@ protected override CreateParams CreateParams if (((int)ScrollBars & RichTextBoxConstants.RTB_VERT) != 0) { - cp.Style |= NativeMethods.WS_VSCROLL; + cp.Style |= User32.WindowStyle.WS_VSCROLL; if (((int)ScrollBars & RichTextBoxConstants.RTB_FORCE) != 0) { cp.Style |= RichTextBoxConstants.ES_DISABLENOSCROLL; @@ -388,10 +388,10 @@ protected override CreateParams CreateParams // Remove the WS_BORDER style from the control, if we're trying to set it, // to prevent the control from displaying the single point rectangle around the 3D border - if (BorderStyle.FixedSingle == BorderStyle && ((cp.Style & NativeMethods.WS_BORDER) != 0)) + if (BorderStyle.FixedSingle == BorderStyle && ((cp.Style & User32.WindowStyle.WS_BORDER) != 0)) { - cp.Style &= (~NativeMethods.WS_BORDER); - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.Style &= (~User32.WindowStyle.WS_BORDER); + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; } return cp; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ScrollBar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ScrollBar.cs index dd4d23b9fcf..9ddc80a04fc 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ScrollBar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ScrollBar.cs @@ -131,7 +131,7 @@ protected override CreateParams CreateParams { CreateParams cp = base.CreateParams; cp.ClassName = "SCROLLBAR"; - cp.Style &= (~NativeMethods.WS_BORDER); + cp.Style &= (~User32.WindowStyle.WS_BORDER); return cp; } } @@ -739,7 +739,7 @@ protected override void WndProc(ref Message m) case WindowMessages.WM_SIZE: // Fixes the scrollbar focus rect - if (UnsafeNativeMethods.GetFocus() == Handle) + if (User32.GetFocus() == Handle) { DefWndProc(ref m); SendMessage(WindowMessages.WM_KILLFOCUS, 0, 0); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ScrollableControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ScrollableControl.cs index 5aadc5b87df..74dcb6db7e3 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ScrollableControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ScrollableControl.cs @@ -181,19 +181,19 @@ protected override CreateParams CreateParams if (HScroll || HorizontalScroll.Visible) { - cp.Style |= NativeMethods.WS_HSCROLL; + cp.Style |= User32.WindowStyle.WS_HSCROLL; } else { - cp.Style &= (~NativeMethods.WS_HSCROLL); + cp.Style &= (~User32.WindowStyle.WS_HSCROLL); } if (VScroll || VerticalScroll.Visible) { - cp.Style |= NativeMethods.WS_VSCROLL; + cp.Style |= User32.WindowStyle.WS_VSCROLL; } else { - cp.Style &= (~NativeMethods.WS_VSCROLL); + cp.Style &= (~User32.WindowStyle.WS_VSCROLL); } return cp; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs b/src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs index 25c3900019f..d223e854948 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/SendKeys.cs @@ -93,7 +93,7 @@ public class SendKeys /// private static IntPtr hhook; - private static NativeMethods.HookProc hook; + private static User32.HookProc hook; /// /// vector of events that we have yet to post to the journaling hook. @@ -239,12 +239,13 @@ private static void InstallHook() { if (hhook == IntPtr.Zero) { - hook = new NativeMethods.HookProc(new SendKeysHookProc().Callback); + hook = new User32.HookProc(new SendKeysHookProc().Callback); stopHook = false; - hhook = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_JOURNALPLAYBACK, - hook, - new HandleRef(null, Kernel32.GetModuleHandleW(null)), - 0); + hhook = User32.SetWindowsHookExW( + User32.WindowHookProcedure.WH_JOURNALPLAYBACK, + hook, + Kernel32.GetModuleHandleW(null), + 0); if (hhook == IntPtr.Zero) { throw new Security.SecurityException(SR.SendKeysHookFailed); @@ -257,18 +258,18 @@ private static void TestHook() hookSupported = false; try { - - NativeMethods.HookProc hookProc = new NativeMethods.HookProc(EmptyHookCallback); - IntPtr hookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_JOURNALPLAYBACK, - hookProc, - new HandleRef(null, Kernel32.GetModuleHandleW(null)), - 0); + var hookProc = new User32.HookProc(EmptyHookCallback); + IntPtr hookHandle = User32.SetWindowsHookExW( + User32.WindowHookProcedure.WH_JOURNALPLAYBACK, + hookProc, + Kernel32.GetModuleHandleW(null), + 0); hookSupported = (hookHandle != IntPtr.Zero); if (hookHandle != IntPtr.Zero) { - UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(null, hookHandle)); + User32.UnhookWindowsHookEx(hookHandle); } } catch { } // ignore any exceptions to keep existing SendKeys behavior @@ -1047,11 +1048,8 @@ private static void UninstallJournalingHook() if (hhook != IntPtr.Zero) { stopHook = false; - if (events != null) - { - events.Clear(); - } - UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(null, hhook)); + events?.Clear(); + User32.UnhookWindowsHookEx(hhook); hhook = IntPtr.Zero; } } @@ -1178,14 +1176,14 @@ public virtual IntPtr Callback(int code, IntPtr wparam, IntPtr lparam) eventmsg.paramL = evt.paramL; eventmsg.paramH = evt.paramH; eventmsg.hwnd = evt.hwnd; - eventmsg.time = SafeNativeMethods.GetTickCount(); + eventmsg.time = Kernel32.GetTickCount(); Marshal.StructureToPtr(eventmsg, lparam, true); break; default: if (code < 0) { - UnsafeNativeMethods.CallNextHookEx(new HandleRef(null, SendKeys.hhook), code, wparam, lparam); + User32.CallNextHookEx(SendKeys.hhook, code, wparam, lparam); } break; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/SplitContainer.cs b/src/System.Windows.Forms/src/System/Windows/Forms/SplitContainer.cs index aafec9a975d..ed8fb4ecf1f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/SplitContainer.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/SplitContainer.cs @@ -586,8 +586,8 @@ private Cursor OverrideCursor // We want to instantly change the cursor if the mouse is within our bounds. var r = new RECT(); UnsafeNativeMethods.GetCursorPos(out Point p); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); - if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || UnsafeNativeMethods.GetCapture() == Handle) + User32.GetWindowRect(new HandleRef(this, Handle), ref r); + if ((r.left <= p.X && p.X < r.right && r.top <= p.Y && p.Y < r.bottom) || User32.GetCapture() == Handle) { SendMessage(WindowMessages.WM_SETCURSOR, Handle, NativeMethods.HTCLIENT); } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Splitter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Splitter.cs index b5657acf55d..a004f57342c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Splitter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Splitter.cs @@ -247,16 +247,16 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); - cp.Style &= (~NativeMethods.WS_BORDER); + cp.ExStyle &= (~User32.WindowStyle.WS_EX_CLIENTEDGE); + cp.Style &= (~User32.WindowStyle.WS_BORDER); switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } return cp; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/StatusBar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/StatusBar.cs index 09ddcb13e99..d8202d447f3 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/StatusBar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/StatusBar.cs @@ -1814,10 +1814,10 @@ protected void CreateHandle() } window.CreateHandle(CreateParams); - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.HWND_TOPMOST, - 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | - NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOPMOST, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOACTIVATE); // Setting the max width has the added benefit of enabling multiline tool tips User32.SendMessageW(this, WindowMessages.TTM_SETMAXTIPWIDTH, IntPtr.Zero, (IntPtr)SystemInformation.MaxWindowTrackSize.Width); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/StatusStrip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/StatusStrip.cs index b653dd5b073..a0b6e6535b2 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/StatusStrip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/StatusStrip.cs @@ -195,7 +195,9 @@ private Control RTLGrip } } - // return whether we should paint the sizing grip. + /// + /// Returns whether we should paint the sizing grip. + /// private bool ShowSizingGrip { get @@ -211,13 +213,15 @@ private bool ShowSizingGrip HandleRef rootHwnd = WindowsFormsUtils.GetRootHWnd(this); if (rootHwnd.Handle != IntPtr.Zero) { - return !UnsafeNativeMethods.IsZoomed(rootHwnd); + return !User32.IsZoomed(rootHwnd); } } } + return false; } } + [ SRCategory(nameof(SR.CatAppearance)), DefaultValue(true), @@ -627,7 +631,7 @@ protected override void WndProc(ref Message m) // if the main window isnt maximized - we should paint a resize grip. // double check that we're at the bottom right hand corner of the window. - if (rootHwnd.Handle != IntPtr.Zero && !UnsafeNativeMethods.IsZoomed(rootHwnd)) + if (rootHwnd.Handle != IntPtr.Zero && !User32.IsZoomed(rootHwnd)) { // get the client area of the topmost window. If we're next to the edge then // the sizing grip is valid. @@ -678,7 +682,7 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; return cp; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/StringSorter.cs b/src/System.Windows.Forms/src/System/Windows/Forms/StringSorter.cs index db8eb0aa8bc..cba0841cc2c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/StringSorter.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/StringSorter.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Globalization; +using static Interop; namespace System.Windows.Forms { @@ -106,7 +107,7 @@ private StringSorter(CultureInfo culture, string[] keys, object[] items, int opt } this.keys = keys; this.items = items; - lcid = culture == null ? SafeNativeMethods.GetThreadLocale() : culture.LCID; + lcid = culture == null ? Kernel32.GetThreadLocale() : culture.LCID; this.options = options & CompareOptions; descending = (options & Descending) != 0; } @@ -129,7 +130,7 @@ internal static int ArrayLength(object[] array) /// public static int Compare(string s1, string s2) { - return Compare(SafeNativeMethods.GetThreadLocale(), s1, s2, 0); + return Compare(Kernel32.GetThreadLocale(), s1, s2, 0); } /// @@ -138,7 +139,7 @@ public static int Compare(string s1, string s2) /// public static int Compare(string s1, string s2, int options) { - return Compare(SafeNativeMethods.GetThreadLocale(), s1, s2, options); + return Compare(Kernel32.GetThreadLocale(), s1, s2, options); } /// diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TabControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TabControl.cs index 77da11f039e..4d95eb7804f 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TabControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TabControl.cs @@ -78,7 +78,7 @@ public class TabControl : Control /// display rectangle. When the message is received, the control calls /// updateTabSelection() to layout the TabPages correctly. /// - private readonly int tabBaseReLayoutMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_TabBaseReLayout"); + private readonly int tabBaseReLayoutMessage = User32.RegisterWindowMessageW(Application.WindowMessagesVersion + "_TabBaseReLayout"); //state private TabPage[] tabPages; @@ -374,9 +374,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Form explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL | NativeMethods.WS_EX_NOINHERITLAYOUT; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL | User32.WindowStyle.WS_EX_NOINHERITLAYOUT; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; @@ -1383,11 +1383,10 @@ protected override void OnHandleCreated(EventArgs e) tooltipHwnd = SendMessage(NativeMethods.TCM_GETTOOLTIPS, 0, 0); if (tooltipHwnd != IntPtr.Zero) { - SafeNativeMethods.SetWindowPos(new HandleRef(this, tooltipHwnd), - NativeMethods.HWND_TOPMOST, - 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | - NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, tooltipHwnd), + User32.HWND_TOPMOST, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOACTIVATE); } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TextBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TextBox.cs index 90aa8563a81..a463ac87cf9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TextBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TextBox.cs @@ -312,7 +312,7 @@ protected override CreateParams CreateParams // Translate for Rtl if necessary // HorizontalAlignment align = RtlTranslateHorizontal(textAlign); - cp.ExStyle &= ~NativeMethods.WS_EX_RIGHT; // WS_EX_RIGHT overrides the ES_XXXX alignment styles + cp.ExStyle &= ~User32.WindowStyle.WS_EX_RIGHT; // WS_EX_RIGHT overrides the ES_XXXX alignment styles switch (align) { case HorizontalAlignment.Left: @@ -333,11 +333,11 @@ protected override CreateParams CreateParams && textAlign == HorizontalAlignment.Left && !WordWrap) { - cp.Style |= NativeMethods.WS_HSCROLL; + cp.Style |= User32.WindowStyle.WS_HSCROLL; } if ((scrollBars & ScrollBars.Vertical) == ScrollBars.Vertical) { - cp.Style |= NativeMethods.WS_VSCROLL; + cp.Style |= User32.WindowStyle.WS_VSCROLL; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxBase.cs index 5769d10337c..aa563b9d163 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TextBoxBase.cs @@ -481,16 +481,16 @@ protected override CreateParams CreateParams cp.Style |= NativeMethods.ES_READONLY; } - cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); - cp.Style &= (~NativeMethods.WS_BORDER); + cp.ExStyle &= (~User32.WindowStyle.WS_EX_CLIENTEDGE); + cp.Style &= (~User32.WindowStyle.WS_BORDER); switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } if (textBoxFlags[multiline]) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Timer.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Timer.cs index 4dc5361c5b5..c5e2e03c523 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Timer.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Timer.cs @@ -225,7 +225,7 @@ private bool EnsureHandle() // Message only windows are cheaper and have fewer issues than // full blown invisible windows. - Parent = (IntPtr)NativeMethods.HWND_MESSAGE + Parent = User32.HWND_MESSAGE }; CreateHandle(cp); @@ -242,8 +242,8 @@ private bool GetInvokeRequired(IntPtr hWnd) { if (hWnd != IntPtr.Zero) { - int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, hWnd), out int pid); - return hwndThread != SafeNativeMethods.GetCurrentThreadId(); + int hwndThread = User32.GetWindowThreadProcessId(new HandleRef(this, hWnd), out int pid); + return hwndThread != Kernel32.GetCurrentThreadId(); } return false; @@ -291,7 +291,7 @@ public void StopTimer(IntPtr hWnd, bool destroyHwnd) // Locking 'this' here is ok since this is an internal class. lock (this) { - if (_stoppingTimer || hWnd == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(new HandleRef(this, hWnd))) + if (_stoppingTimer || hWnd == IntPtr.Zero || !User32.IsWindow(new HandleRef(this, hWnd))) { return; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolBar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolBar.cs index 8356ae0c4cf..60163a81152 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolBar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolBar.cs @@ -397,7 +397,7 @@ protected override CreateParams CreateParams // cp.Style |= NativeMethods.CCS_NOPARENTALIGN | NativeMethods.CCS_NORESIZE; - // | NativeMethods.WS_CHILD was commented out since setTopLevel should be able to work. + // | User32.WindowStyle.WS_CHILD was commented out since setTopLevel should be able to work. if (!Divider) { @@ -414,15 +414,15 @@ protected override CreateParams CreateParams cp.Style |= NativeMethods.TBSTYLE_TOOLTIPS; } - cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); - cp.Style &= (~NativeMethods.WS_BORDER); + cp.ExStyle &= (~User32.WindowStyle.WS_EX_CLIENTEDGE); + cp.Style &= (~User32.WindowStyle.WS_BORDER); switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } @@ -1739,8 +1739,7 @@ protected override void WndProc(ref Message m) case NativeMethods.TTN_SHOW: // Prevent the tooltip from displaying in the upper left corner of the // desktop when the control is nowhere near that location. - NativeMethods.WINDOWPLACEMENT wndPlacement = new NativeMethods.WINDOWPLACEMENT(); - int nRet = UnsafeNativeMethods.GetWindowPlacement(new HandleRef(null, note.hwndFrom), ref wndPlacement); + int nRet = User32.GetWindowPlacement(note.hwndFrom, out User32.WINDOWPLACEMENT wndPlacement); // Is this tooltip going to be positioned in the upper left corner of the display, // but nowhere near the toolbar button? @@ -1791,7 +1790,14 @@ protected override void WndProc(ref Message m) leftTop.X -= (ButtonSize.Width + tooltipWidth + 2); } - SafeNativeMethods.SetWindowPos(new HandleRef(null, note.hwndFrom), NativeMethods.NullHandleRef, leftTop.X, leftTop.Y, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + note.hwndFrom, + IntPtr.Zero, + leftTop.X, + leftTop.Y, + 0, + 0, + User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOZORDER | User32.WindowPosition.SWP_NOACTIVATE); m.Result = (IntPtr)1; return; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStrip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStrip.cs index e2bbe12dd67..cbb02ec3a38 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStrip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStrip.cs @@ -822,7 +822,7 @@ internal virtual NativeWindow DropDownOwnerWindow { CreateParams cp = new CreateParams { - ExStyle = NativeMethods.WS_EX_TOOLWINDOW + ExStyle = User32.WindowStyle.WS_EX_TOOLWINDOW }; dropDownOwnerWindow.CreateHandle(cp); } @@ -2065,7 +2065,7 @@ internal virtual void ChangeSelection(ToolStripItem nextItem) { if (hwndThatLostFocus == IntPtr.Zero) { - SnapFocus(UnsafeNativeMethods.GetFocus()); + SnapFocus(User32.GetFocus()); } controlHost.Control.Select(); controlHost.Control.Focus(); @@ -4501,7 +4501,7 @@ protected virtual void RestoreFocus() if (!focusSuccess) { // clear out the focus, we have focus, we're not supposed to anymore. - UnsafeNativeMethods.SetFocus(NativeMethods.NullHandleRef); + User32.SetFocus(IntPtr.Zero); } } @@ -4953,7 +4953,7 @@ private void SnapFocus(IntPtr otherHwnd) // make sure the otherHandle is not a child of thisHandle if ((thisHandle.Handle != otherHandle.Handle) && - !UnsafeNativeMethods.IsChild(thisHandle, otherHandle)) + !User32.IsChild(thisHandle, otherHandle)) { // make sure the root window of the otherHwnd is the same as @@ -5159,7 +5159,7 @@ protected override void WndProc(ref Message m) { // snap the active window and compare to our root window. - IntPtr hwndActive = UnsafeNativeMethods.GetActiveWindow(); + IntPtr hwndActive = User32.GetActiveWindow(); if (hwndActive != rootHwnd.Handle) { // Activate the window, and discard the mouse message. @@ -5174,7 +5174,7 @@ protected override void WndProc(ref Message m) { // we're setting focus to a child control - remember who gave it to us // so we can restore it on ESC. - SnapFocus(UnsafeNativeMethods.GetFocus()); + SnapFocus(User32.GetFocus()); if (!IsDropDown && !TabStop) { Debug.WriteLineIf(SnapFocusDebug.TraceVerbose, "Installing restoreFocusFilter"); @@ -5654,10 +5654,10 @@ public bool PreFilterMessage(ref Message m) { // if we've clicked on something that's not a child of the toolstrip and we // currently have focus, restore it. - if (!UnsafeNativeMethods.IsChild(new HandleRef(this, ownerToolStrip.Handle), new HandleRef(this, m.HWnd))) + if (!User32.IsChild(new HandleRef(this, ownerToolStrip.Handle), new HandleRef(this, m.HWnd))) { HandleRef rootHwnd = WindowsFormsUtils.GetRootHWnd(ownerToolStrip); - if (rootHwnd.Handle == m.HWnd || UnsafeNativeMethods.IsChild(rootHwnd, new HandleRef(this, m.HWnd))) + if (rootHwnd.Handle == m.HWnd || User32.IsChild(rootHwnd, new HandleRef(this, m.HWnd))) { // Only RestoreFocus if the hwnd is a child of the root window and isnt on the toolstrip. RestoreFocusInternal(); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDown.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDown.cs index b3b6d6c41b6..18ef2c5eb3d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDown.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripDropDown.cs @@ -287,7 +287,7 @@ protected override CreateParams CreateParams // If we're using themes then go ahead if (DropShadowEnabled) { - cp.ClassStyle |= (int)NativeMethods.ClassStyle.CS_DROPSHADOW; + cp.ClassStyle |= (int)User32.ClassStyle.CS_DROPSHADOW; } // we're a borderless menuless control with no min/max boxes // we dont want to show in the taskbar either @@ -296,18 +296,18 @@ protected override CreateParams CreateParams //Give the window the WS_EX_TOOLWINDOW extended style, and remove the WS_EX_APPWINDOW style. As a side effect, the window will have a smaller caption than a normal window. //Give the window the WS_POPUP style and make it owned by a hidden window. (Form) - cp.Style &= ~(NativeMethods.WS_CAPTION | NativeMethods.WS_CLIPSIBLINGS); /* no caption, no siblings */ - cp.ExStyle &= ~(NativeMethods.WS_EX_APPWINDOW); /* show in taskbar = false */ - // | NativeMethods.WS_EX_TOOLWINDOW - cp.Style |= (TopLevel) ? NativeMethods.WS_POPUP : NativeMethods.WS_CHILD; - cp.ExStyle |= (NativeMethods.WS_EX_CONTROLPARENT); /* show in taskbar = false */ + cp.Style &= ~(User32.WindowStyle.WS_CAPTION | User32.WindowStyle.WS_CLIPSIBLINGS); /* no caption, no siblings */ + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_APPWINDOW); /* show in taskbar = false */ + // | User32.WindowStyle.WS_EX_TOOLWINDOW + cp.Style |= (TopLevel) ? User32.WindowStyle.WS_POPUP : User32.WindowStyle.WS_CHILD; + cp.ExStyle |= (User32.WindowStyle.WS_EX_CONTROLPARENT); /* show in taskbar = false */ bool topLevel = TopLevel; // opacity if (topLevel && (state[stateLayered])) { - cp.ExStyle |= NativeMethods.WS_EX_LAYERED; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYERED; } else if (topLevel) { @@ -316,11 +316,11 @@ protected override CreateParams CreateParams //If the display driver has enough memory, it saves the bits for Windows. If the display driver does not have enough memory, Window //saves the bits itself as a bitmap in global memory and also uses some of User's local heap for housekeeping structures for each window. //When the application removes the window, Windows can restore the screen image quickly by using the stored bits. - cp.ClassStyle |= (int)NativeMethods.ClassStyle.CS_SAVEBITS; + cp.ClassStyle |= (int)User32.ClassStyle.CS_SAVEBITS; } else if (!topLevel) { - cp.Style |= NativeMethods.WS_CLIPSIBLINGS; + cp.Style |= User32.WindowStyle.WS_CLIPSIBLINGS; } // We're turning off CLIPSIBLINGS because in the designer the elements of the form beneath @@ -940,13 +940,7 @@ internal Control SourceControlInternal } } - internal override int ShowParams - { - get - { - return NativeMethods.SW_SHOWNOACTIVATE; - } - } + internal override User32.ShowWindowCommand ShowParams => User32.ShowWindowCommand.SW_SHOWNOACTIVATE; [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] new public event EventHandler TabStopChanged @@ -1104,9 +1098,10 @@ private void ApplyTopMost(bool topMost) { if (TopMost) { - HandleRef topMostFlag = (topMost) ? NativeMethods.HWND_TOPMOST : NativeMethods.HWND_NOTOPMOST; - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), topMostFlag, 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, Handle), + topMost ? User32.HWND_TOPMOST : User32.HWND_NOTOPMOST, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOACTIVATE); } } @@ -1719,7 +1714,7 @@ private void ResetDropShadowEnabled() private void ReparentToActiveToolStripWindow() { ToolStripManager.ModalMenuFilter.SetActiveToolStrip(this); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, ToolStripManager.ModalMenuFilter.ActiveHwnd); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, ToolStripManager.ModalMenuFilter.ActiveHwnd); } private void ReparentToDropDownOwnerWindow() @@ -1728,7 +1723,7 @@ private void ReparentToDropDownOwnerWindow() // this prevents a taskbar entry. NativeWindow ownerWindow = DropDownOwnerWindow; HandleRef ownerHandle = new HandleRef(ownerWindow, ownerWindow.Handle); - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_HWNDPARENT, ownerHandle); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_HWNDPARENT, ownerHandle); } internal override void ResetScaling(int newDpi) @@ -1830,14 +1825,14 @@ private void SetTopLevelStyle(bool value) if (value) { // setting toplevel = true - styleFlags &= ~NativeMethods.WS_CHILD; - styleFlags |= NativeMethods.WS_POPUP; + styleFlags &= ~User32.WindowStyle.WS_CHILD; + styleFlags |= User32.WindowStyle.WS_POPUP; } else { // this is a child window - styleFlags &= ~NativeMethods.WS_POPUP; - styleFlags |= NativeMethods.WS_CHILD; + styleFlags &= ~User32.WindowStyle.WS_POPUP; + styleFlags |= User32.WindowStyle.WS_CHILD; } WindowStyle = styleFlags; @@ -1871,7 +1866,7 @@ protected override void SetVisibleCore(bool visible) // Snap the foreground window BEFORE calling any user events so they // dont have a chance to activate something else. This covers the case // where someone handles the opening event and throws up a messagebox. - IntPtr foregroundWindow = UnsafeNativeMethods.GetForegroundWindow(); + IntPtr foregroundWindow = User32.GetForegroundWindow(); // Fire Opening event // Cancellable event in which default value of e.Cancel depends on @@ -1908,10 +1903,12 @@ protected override void SetVisibleCore(bool visible) { ApplyTopMost(true); } - else if (IsHandleCreated && SafeNativeMethods.IsWindowEnabled(new HandleRef(this, Handle))) + else if (IsHandleCreated && User32.IsWindowEnabled(new HandleRef(this, Handle))) { - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.HWND_TOP, 0, 0, 0, 0, - NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOACTIVATE); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOP, + flags: User32.WindowPosition.SWP_NOMOVE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOACTIVATE); } } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripManager.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripManager.cs index 817bc23344c..39761de8e81 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripManager.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolStripManager.cs @@ -947,7 +947,7 @@ private void EnterMenuModeCore() #if DEBUG _justEnteredMenuMode = true; #endif - IntPtr hwndActive = UnsafeNativeMethods.GetActiveWindow(); + IntPtr hwndActive = User32.GetActiveWindow(); if (hwndActive != IntPtr.Zero) { ActiveHwndInternal = new HandleRef(this, hwndActive); @@ -1256,7 +1256,7 @@ private void SetActiveToolStripCore(ToolStrip toolStrip) if (dropDown.AutoClose == false) { // store off the current active hwnd - IntPtr hwndActive = UnsafeNativeMethods.GetActiveWindow(); + IntPtr hwndActive = User32.GetActiveWindow(); if (hwndActive != IntPtr.Zero) { ActiveHwndInternal = new HandleRef(this, hwndActive); @@ -1355,7 +1355,7 @@ private static bool IsChildOrSameWindow(HandleRef hwndParent, HandleRef hwndChil { return true; } - if (UnsafeNativeMethods.IsChild(hwndParent, hwndChild)) + if (User32.IsChild(hwndParent, hwndChild)) { return true; } @@ -1403,7 +1403,7 @@ public bool PreFilterMessage(ref Message m) return false; } HandleRef hwndActiveToolStrip = new HandleRef(activeToolStrip, activeToolStrip.Handle); - HandleRef hwndCurrentActiveWindow = new HandleRef(null, UnsafeNativeMethods.GetActiveWindow()); + HandleRef hwndCurrentActiveWindow = new HandleRef(null, User32.GetActiveWindow()); // if the active window has changed... if (hwndCurrentActiveWindow.Handle != _lastActiveWindow.Handle) @@ -1433,8 +1433,7 @@ public bool PreFilterMessage(ref Message m) return false; } - DpiAwarenessContext context = CommonUnsafeNativeMethods.GetDpiAwarenessContextForWindow(m.HWnd); - + User32.DpiAwarenessContext context = User32.GetDpiAwarenessContextForWindow(m.HWnd); using (DpiHelper.EnterDpiAwarenessScope(context)) { switch (m.Msg) @@ -1536,7 +1535,7 @@ private class HostedWindowsFormsMessageHook { private IntPtr messageHookHandle = IntPtr.Zero; private bool isHooked = false; - private NativeMethods.HookProc hookProc; + private User32.HookProc hookProc; public HostedWindowsFormsMessageHook() { @@ -1588,12 +1587,12 @@ private void InstallMessageHook() return; } - hookProc = new NativeMethods.HookProc(MessageHookProc); - - messageHookHandle = UnsafeNativeMethods.SetWindowsHookEx(NativeMethods.WH_GETMESSAGE, - hookProc, - new HandleRef(null, IntPtr.Zero), - SafeNativeMethods.GetCurrentThreadId()); + hookProc = new User32.HookProc(MessageHookProc); + messageHookHandle = User32.SetWindowsHookExW( + User32.WindowHookProcedure.WH_GETMESSAGE, + hookProc, + IntPtr.Zero, + Kernel32.GetCurrentThreadId()); if (messageHookHandle != IntPtr.Zero) { @@ -1624,7 +1623,7 @@ private unsafe IntPtr MessageHookProc(int nCode, IntPtr wparam, IntPtr lparam) } } - return UnsafeNativeMethods.CallNextHookEx(new HandleRef(this, messageHookHandle), nCode, wparam, lparam); + return User32.CallNextHookEx(new HandleRef(this, messageHookHandle), nCode, wparam, lparam); } private void UninstallMessageHook() @@ -1633,7 +1632,7 @@ private void UninstallMessageHook() { if (messageHookHandle != IntPtr.Zero) { - UnsafeNativeMethods.UnhookWindowsHookEx(new HandleRef(this, messageHookHandle)); + User32.UnhookWindowsHookEx(new HandleRef(this, messageHookHandle)); hookProc = null; messageHookHandle = IntPtr.Zero; isHooked = false; @@ -1969,8 +1968,7 @@ internal static bool ProcessMenuKey(ref Message m) // if we've alt-tabbed away dont snap/restore focus. HandleRef topmostParentOfMenu = WindowsFormsUtils.GetRootHWnd(menuStripToActivate); - IntPtr foregroundWindow = UnsafeNativeMethods.GetForegroundWindow(); - + IntPtr foregroundWindow = User32.GetForegroundWindow(); if (topmostParentOfMenu.Handle == foregroundWindow) { Debug.WriteLineIf(ToolStrip.SnapFocusDebug.TraceVerbose, "[ProcessMenuKey] ToolStripManager call MenuStrip.OnMenuKey"); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs b/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs index 9b46c8c29d7..704dfdb70aa 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/ToolTip.cs @@ -292,10 +292,10 @@ private bool IsWindowActive(IWin32Window window) // for ToolStripDropDown ShowParams returns SW_SHOWNOACTIVATE, in which case we don't // want to check IsWindowActive and hence return true. if (window is Control windowControl && - (windowControl.ShowParams & 0xF) != NativeMethods.SW_SHOWNOACTIVATE) + (windowControl.ShowParams & (User32.ShowWindowCommand)0xF) != User32.ShowWindowCommand.SW_SHOWNOACTIVATE) { - IntPtr hWnd = UnsafeNativeMethods.GetActiveWindow(); - IntPtr rootHwnd = UnsafeNativeMethods.GetAncestor(new HandleRef(window, window.Handle), NativeMethods.GA_ROOT); + IntPtr hWnd = User32.GetActiveWindow(); + IntPtr rootHwnd = User32.GetAncestor(new HandleRef(window, window.Handle), User32.GetAncestorFlag.GA_ROOT); if (hWnd != rootHwnd) { TipInfo tt = (TipInfo)_tools[windowControl]; @@ -763,9 +763,9 @@ private void CreateHandle() // If in OwnerDraw mode, we don't want the default border. if (OwnerDraw) { - int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); - style &= ~NativeMethods.WS_BORDER; - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)style)); + int style = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); + style &= ~User32.WindowStyle.WS_BORDER; + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)style); } // Setting the max width has the added benefit of enabling multiline tool tips. @@ -1007,9 +1007,8 @@ private ComCtl32.ToolInfoWrapper GetWinTOOLINFO(IWin32Window hWnd) // RightToLeft reading order if (TopLevelControl?.RightToLeft == RightToLeft.Yes) { - bool isWindowMirrored = ((unchecked((int)(long)UnsafeNativeMethods.GetWindowLong( - new HandleRef(this, Control.GetSafeHandle(hWnd)), NativeMethods.GWL_STYLE)) & NativeMethods.WS_EX_LAYOUTRTL) == NativeMethods.WS_EX_LAYOUTRTL); - + bool isWindowMirrored = ((unchecked((int)(long)User32.GetWindowLong( + new HandleRef(this, Control.GetSafeHandle(hWnd)), User32.WindowLong.GWL_STYLE)) & User32.WindowStyle.WS_EX_LAYOUTRTL) == User32.WindowStyle.WS_EX_LAYOUTRTL); // Indicates that the ToolTip text will be displayed in the opposite direction // to the text in the parent window. if (!isWindowMirrored) @@ -1309,7 +1308,7 @@ private void ShowTooltip(string text, IWin32Window window, int duration) if (window is Control associatedControl) { var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(associatedControl, associatedControl.Handle), ref r); + User32.GetWindowRect(new HandleRef(associatedControl, associatedControl.Handle), ref r); Cursor currentCursor = Cursor.Current; Point cursorLocation = Cursor.Position; @@ -1418,7 +1417,7 @@ public void Show(string text, IWin32Window window, Point point) { // Set the ToolTips. var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); + User32.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); int pointX = r.left + point.X; int pointY = r.top + point.Y; @@ -1445,7 +1444,7 @@ public void Show(string text, IWin32Window window, Point point, int duration) { // Set the ToolTips. var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); + User32.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); int pointX = r.left + point.X; int pointY = r.top + point.Y; SetTrackPosition(pointX, pointY); @@ -1467,7 +1466,7 @@ public void Show(string text, IWin32Window window, int x, int y) if (IsWindowActive(window)) { var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); + User32.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); int pointX = r.left + x; int pointY = r.top + y; SetTrackPosition(pointX, pointY); @@ -1492,7 +1491,7 @@ public void Show(string text, IWin32Window window, int x, int y, int duration) if (IsWindowActive(window)) { var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); + User32.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); int pointX = r.left + x; int pointY = r.top + y; SetTrackPosition(pointX, pointY); @@ -1945,10 +1944,14 @@ private void Reposition(Point tipPosition, Size tipSize) moveToLocation.Y = screen.WorkingArea.Bottom - tipSize.Height; } - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), - NativeMethods.HWND_TOPMOST, - moveToLocation.X, moveToLocation.Y, tipSize.Width, tipSize.Height, - NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOOWNERZORDER); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOPMOST, + moveToLocation.X, + moveToLocation.Y, + tipSize.Width, + tipSize.Height, + User32.WindowPosition.SWP_NOACTIVATE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOOWNERZORDER); } private IntPtr GetCurrentToolHwnd() @@ -1993,9 +1996,8 @@ private void WmMove() // Reposition the tooltip when its about to be shown since the tooltip can go out of screen // working area bounds Reposition would check the bounds for us. - var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); if (tt.Position != Point.Empty) { @@ -2013,7 +2015,7 @@ private void WmMouseActivate(ref Message msg) return; var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); + User32.GetWindowRect(new HandleRef(window, Control.GetSafeHandle(window)), ref r); Point cursorLocation = Cursor.Position; // Do not activate the mouse if its within the bounds of the @@ -2046,7 +2048,7 @@ private void WmShow() // Get the bounds. var r = new RECT(); - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); Control toolControl = window as Control; @@ -2064,7 +2066,7 @@ private void WmShow() // any of the tooltip attributes/properties could have been updated // during the popup event; in which case the size of the tooltip is // affected. e.ToolTipSize is respected over r.Size - UnsafeNativeMethods.GetWindowRect(new HandleRef(this, Handle), ref r); + User32.GetWindowRect(new HandleRef(this, Handle), ref r); currentTooltipSize = (e.ToolTipSize == currentTooltipSize) ? r.Size : e.ToolTipSize; if (IsBalloon) @@ -2093,23 +2095,23 @@ private void WmShow() if (e.Cancel) { _cancelled = true; - SafeNativeMethods.SetWindowPos( + User32.SetWindowPos( new HandleRef(this, Handle), - NativeMethods.HWND_TOPMOST, - 0, 0, 0, 0, - NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_NOOWNERZORDER); - + User32.HWND_TOPMOST, + flags: User32.WindowPosition.SWP_NOACTIVATE | User32.WindowPosition.SWP_NOOWNERZORDER); } else { _cancelled = false; // Only width/height changes are respected, so set top,left to what we got earlier - SafeNativeMethods.SetWindowPos( - new HandleRef(this, Handle), - NativeMethods.HWND_TOPMOST, - r.left, r.top, currentTooltipSize.Width, currentTooltipSize.Height, - NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_NOOWNERZORDER); + User32.SetWindowPos(new HandleRef(this, Handle), + User32.HWND_TOPMOST, + r.left, + r.top, + currentTooltipSize.Width, + currentTooltipSize.Height, + User32.WindowPosition.SWP_NOACTIVATE | User32.WindowPosition.SWP_NOOWNERZORDER); } } @@ -2122,7 +2124,7 @@ private bool WmWindowPosChanged() { if (_cancelled) { - SafeNativeMethods.ShowWindow(new HandleRef(this, Handle), NativeMethods.SW_HIDE); + User32.ShowWindow(new HandleRef(this, Handle), User32.ShowWindowCommand.SW_HIDE); return true; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TrackBar.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TrackBar.cs index 02a66e690ea..d132f603d0c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TrackBar.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TrackBar.cs @@ -197,9 +197,9 @@ protected override CreateParams CreateParams if (RightToLeft == RightToLeft.Yes && RightToLeftLayout == true) { //We want to turn on mirroring for Trackbar explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL | NativeMethods.WS_EX_NOINHERITLAYOUT; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL | User32.WindowStyle.WS_EX_NOINHERITLAYOUT; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } return cp; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/TreeView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/TreeView.cs index a4e18f708a5..85344289eff 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/TreeView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/TreeView.cs @@ -339,16 +339,16 @@ protected override CreateParams CreateParams // if (IsHandleCreated) { - int currentStyle = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); - cp.Style |= (currentStyle & (NativeMethods.WS_HSCROLL | NativeMethods.WS_VSCROLL)); + int currentStyle = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); + cp.Style |= (currentStyle & (User32.WindowStyle.WS_HSCROLL | User32.WindowStyle.WS_VSCROLL)); } switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } @@ -418,9 +418,9 @@ protected override CreateParams CreateParams if (RightToLeftLayout == true) { //We want to turn on mirroring for TreeView explicitly. - cp.ExStyle |= NativeMethods.WS_EX_LAYOUTRTL; + cp.ExStyle |= User32.WindowStyle.WS_EX_LAYOUTRTL; //Don't need these styles when mirroring is turned on. - cp.ExStyle &= ~(NativeMethods.WS_EX_RTLREADING | NativeMethods.WS_EX_RIGHT | NativeMethods.WS_EX_LEFTSCROLLBAR); + cp.ExStyle &= ~(User32.WindowStyle.WS_EX_RTLREADING | User32.WindowStyle.WS_EX_RIGHT | User32.WindowStyle.WS_EX_LEFTSCROLLBAR); } else { @@ -2016,16 +2016,16 @@ protected override void OnHandleCreated(EventArgs e) // This seems to make the Treeview happy. if (CheckBoxes) { - int style = unchecked((int)(UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); + int style = unchecked((int)(User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); style |= NativeMethods.TVS_CHECKBOXES; - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)style)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)style); } if (ShowNodeToolTips && !DesignMode) { - int style = unchecked((int)(UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE))); + int style = unchecked((int)(User32.GetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE))); style |= NativeMethods.TVS_INFOTIP; - UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)style)); + User32.SetWindowLong(new HandleRef(this, Handle), User32.WindowLong.GWL_STYLE, (IntPtr)style); } Color c; @@ -2079,14 +2079,27 @@ protected override void OnHandleCreated(EventArgs e) treeViewState[TREEVIEWSTATE_stopResizeWindowMsgs] = true; oldSize = Width; - int flags = NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_NOMOVE; - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.NullHandleRef, Left, Top, int.MaxValue, Height, flags); + User32.WindowPosition flags = User32.WindowPosition.SWP_NOZORDER | User32.WindowPosition.SWP_NOACTIVATE | User32.WindowPosition.SWP_NOMOVE; + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOP, + Left, + Top, + int.MaxValue, + Height, + flags); root.Realize(false); if (oldSize != 0) { - SafeNativeMethods.SetWindowPos(new HandleRef(this, Handle), NativeMethods.NullHandleRef, Left, Top, oldSize, Height, flags); + User32.SetWindowPos( + new HandleRef(this, Handle), + User32.HWND_TOP, + Left, Top, + oldSize, + Height, + flags); } } finally @@ -2993,8 +3006,14 @@ private unsafe bool WmShowToolTip(ref Message m) bounds.Location = PointToScreen(bounds.Location); User32.SendMessageW(tooltipHandle, WindowMessages.TTM_ADJUSTRECT, PARAM.FromBool(true), bounds); - SafeNativeMethods.SetWindowPos(new HandleRef(this, tooltipHandle), - NativeMethods.HWND_TOPMOST, bounds.Left, bounds.Top, 0, 0, NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER); + User32.SetWindowPos( + new HandleRef(this, tooltipHandle), + User32.HWND_TOPMOST, + bounds.Left, + bounds.Top, + 0, + 0, + User32.WindowPosition.SWP_NOACTIVATE | User32.WindowPosition.SWP_NOSIZE | User32.WindowPosition.SWP_NOZORDER); return true; } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/UpDownBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/UpDownBase.cs index 42345a1261b..a26d759f9a0 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/UpDownBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/UpDownBase.cs @@ -321,16 +321,16 @@ protected override CreateParams CreateParams { CreateParams cp = base.CreateParams; - cp.Style &= (~NativeMethods.WS_BORDER); + cp.Style &= (~User32.WindowStyle.WS_BORDER); if (!Application.RenderWithVisualStyles) { switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } } @@ -1182,8 +1182,9 @@ protected override void WndProc(ref Message m) { if (TextBox.CanFocus) { - UnsafeNativeMethods.SetFocus(new HandleRef(TextBox, TextBox.Handle)); + User32.SetFocus(new HandleRef(TextBox, TextBox.Handle)); } + base.WndProc(ref m); } break; diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/UserControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/UserControl.cs index c4a51227be4..f354f503363 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/UserControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/UserControl.cs @@ -188,18 +188,18 @@ protected override CreateParams CreateParams get { CreateParams cp = base.CreateParams; - cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT; + cp.ExStyle |= User32.WindowStyle.WS_EX_CONTROLPARENT; - cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE); - cp.Style &= (~NativeMethods.WS_BORDER); + cp.ExStyle &= (~User32.WindowStyle.WS_EX_CLIENTEDGE); + cp.Style &= (~User32.WindowStyle.WS_BORDER); switch (borderStyle) { case BorderStyle.Fixed3D: - cp.ExStyle |= NativeMethods.WS_EX_CLIENTEDGE; + cp.ExStyle |= User32.WindowStyle.WS_EX_CLIENTEDGE; break; case BorderStyle.FixedSingle: - cp.Style |= NativeMethods.WS_BORDER; + cp.Style |= User32.WindowStyle.WS_BORDER; break; } return cp; @@ -279,14 +279,14 @@ private bool FocusInside() return false; } - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); + IntPtr hwndFocus = User32.GetFocus(); if (hwndFocus == IntPtr.Zero) { return false; } IntPtr hwnd = Handle; - if (hwnd == hwndFocus || SafeNativeMethods.IsChild(new HandleRef(this, hwnd), new HandleRef(null, hwndFocus))) + if (hwnd == hwndFocus || User32.IsChild(new HandleRef(this, hwnd), hwndFocus)) { return true; } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowser.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowser.cs index efc23f0153a..9845f81d647 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowser.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowser.cs @@ -1072,10 +1072,6 @@ public void Stop() [Browsable(false), SRCategory(nameof(SR.CatPropertyChanged)), SRDescription(nameof(SR.WebBrowserStatusTextChangedDescr))] public event EventHandler StatusTextChanged; - // - // public overrides: - // - /// /// Returns true if this control (or any of its child windows) has focus. /// @@ -1087,17 +1083,12 @@ public override bool Focused { return true; } - IntPtr hwndFocus = UnsafeNativeMethods.GetFocus(); - return hwndFocus != IntPtr.Zero - && SafeNativeMethods.IsChild(new HandleRef(this, Handle), new HandleRef(null, hwndFocus)); + + IntPtr hwndFocus = User32.GetFocus(); + return hwndFocus != IntPtr.Zero && User32.IsChild(new HandleRef(this, Handle), hwndFocus); } } - // - // protected overrides: - // - // - // protected override void Dispose(bool disposing) { if (disposing) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs index d87977712ba..0b0a9383e7c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserBase.cs @@ -350,7 +350,7 @@ protected internal override bool ProcessMnemonic(char charCode) message = WindowMessages.WM_SYSKEYDOWN, wParam = (IntPtr)char.ToUpper(charCode, CultureInfo.CurrentCulture), lParam = (IntPtr)0x20180001, - time = SafeNativeMethods.GetTickCount() + time = Kernel32.GetTickCount() }; UnsafeNativeMethods.GetCursorPos(out Point p); @@ -830,7 +830,7 @@ internal bool RemoveSelectionHandler() internal void AttachWindow(IntPtr hwnd) { - UnsafeNativeMethods.SetParent(new HandleRef(null, hwnd), new HandleRef(this, Handle)); + User32.SetParent(hwnd, new HandleRef(this, Handle)); if (axWindow != null) { @@ -1270,7 +1270,7 @@ internal ContainerControl FindContainerControlInternal() if (cc == null) { - cc = Control.FromHandle(UnsafeNativeMethods.GetParent(new HandleRef(this, Handle))) as ContainerControl; + cc = Control.FromHandle(User32.GetParent(new HandleRef(this, Handle))) as ContainerControl; } // Never use the parking window for this: its hwnd can be destroyed at any time. diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserHelper.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserHelper.cs index a681f030b44..81df5bdeaac 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserHelper.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserHelper.cs @@ -72,7 +72,7 @@ internal enum SelectionStyle internal static Guid maskEdit_Clsid = new Guid("{c932ba85-4374-101b-a56c-00aa003668dc}"); // Window message to check if we have already sub-classed - internal static readonly int REGMSG_MSG = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_subclassCheck"); + internal static readonly int REGMSG_MSG = User32.RegisterWindowMessageW(Application.WindowMessagesVersion + "_subclassCheck"); internal const int REGMSG_RETVAL = 123; // diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserSiteBase.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserSiteBase.cs index e1b6bbb37d4..013bdbff009 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserSiteBase.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WebBrowserSiteBase.cs @@ -235,7 +235,7 @@ IntPtr UnsafeNativeMethods.IOleInPlaceSite.GetWindow() { try { - return UnsafeNativeMethods.GetParent(new HandleRef(Host, Host.Handle)); + return User32.GetParent(new HandleRef(Host, Host.Handle)); } catch (Exception t) { diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs b/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs index e120b7d2252..2d0c81a1415 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/WinFormsUtils.cs @@ -254,7 +254,7 @@ public static char GetMnemonic(string text, bool convertToUpperCase) /// public static HandleRef GetRootHWnd(HandleRef hwnd) { - IntPtr rootHwnd = UnsafeNativeMethods.GetAncestor(new HandleRef(hwnd, hwnd.Handle), NativeMethods.GA_ROOT); + IntPtr rootHwnd = User32.GetAncestor(new HandleRef(hwnd, hwnd.Handle), User32.GetAncestorFlag.GA_ROOT); return new HandleRef(hwnd.Wrapper, rootHwnd); } diff --git a/src/System.Windows.Forms/tests/IntegrationTests/WinFormsControlsClassicTests/WinFormsControlsClassicTests.csproj b/src/System.Windows.Forms/tests/IntegrationTests/WinFormsControlsClassicTests/WinFormsControlsClassicTests.csproj index 173c60492b3..740cfed002b 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/WinFormsControlsClassicTests/WinFormsControlsClassicTests.csproj +++ b/src/System.Windows.Forms/tests/IntegrationTests/WinFormsControlsClassicTests/WinFormsControlsClassicTests.csproj @@ -73,7 +73,7 @@ Interop.Libraries.cs - Interop.WindowMessages.cs + WindowMessages.cs Gdi32\Interop.GetDeviceCaps.cs