Merge pull request #2971 from BrzVlad/feature-cross-binprot
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / Microsoft.Tools.Common / Microsoft / Tools / Common / Win32Interop.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace Microsoft.Tools.Common
6 {
7     using System;
8     using System.Runtime.InteropServices;
9     using System.Diagnostics.CodeAnalysis;
10
11     
12     internal static class Win32Interop
13     {
14         public const int WM_SETICON = 0x80;
15         public const int WM_NCHITTEST = 0x84;
16         public const int WM_SYSCOMMAND = 0x0112;
17
18         public const int GWL_STYLE = -16;
19         public const int WS_MAXIMIZEBOX = 0x00010000;
20         public const int WS_MINIMIZEBOX = 0x00020000;
21         public const int WS_CLIPCHILDREN = 0x02000000;
22         public const int WS_CLIPSIBLINGS = 0x04000000;
23
24         public const int GWL_EXSTYLE = -20;
25         public const int WS_EX_DLGMODALFRAME = 0x00000001;
26         public const int WS_EX_CONTEXTHELP = 0x00000400;
27
28         public const int SC_CONTEXTHELP = 0xf180;
29
30         public const int ICON_SMALL = 0;
31         public const int ICON_BIG = 1;
32         
33
34         [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "This class is shared by various projects and may not be used by a specific project")]
35         [StructLayout(LayoutKind.Sequential)]
36         public sealed class POINT
37         {
38             public int x;
39             public int y;
40
41             public POINT()
42             {
43                 this.x = 0;
44                 this.y = 0;
45             }
46
47             public POINT(int x, int y)
48             {
49                 this.x = x;
50                 this.y = y;
51             }
52         }
53
54         internal static void MakeWindowClipSiblingsAndChildren(HandleRef hwnd)
55         {
56             IntPtr windowStyle = Win32Interop.GetWindowLongPtr(hwnd.Handle, Win32Interop.GWL_STYLE);
57             if (IntPtr.Size == 4)
58             {
59                 windowStyle = new IntPtr(windowStyle.ToInt32() | Win32Interop.WS_CLIPSIBLINGS | Win32Interop.WS_CLIPCHILDREN);
60             }
61             else
62             {
63                 windowStyle = new IntPtr(windowStyle.ToInt64() | ((long)Win32Interop.WS_CLIPSIBLINGS) | ((long)Win32Interop.WS_CLIPCHILDREN));
64             }
65             Win32Interop.SetWindowLongPtr(hwnd, Win32Interop.GWL_STYLE, (IntPtr)windowStyle);
66         }
67
68         // This static method is required because legacy OSes do not support
69         // SetWindowLongPtr
70         internal static IntPtr SetWindowLongPtr(HandleRef hWnd, int nIndex, IntPtr dwNewLong)
71         {
72             if (IntPtr.Size == 8)
73                 return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
74             else
75                 return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
76         }
77
78
79         // This static method is required because Win32 does not support
80         // GetWindowLongPtr directly
81         public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
82         {
83             if (IntPtr.Size == 8)
84                 return GetWindowLongPtr64(hWnd, nIndex);
85             else
86                 return GetWindowLongPtr32(hWnd, nIndex);
87         }
88
89         [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
90         private static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong);
91
92         [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
93         private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong);
94
95         [SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "return", Justification = "Calling code is expected to handle the different size of IntPtr")]
96         [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
97         private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
98
99         [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
100         private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
101
102         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This function is shared by various projects and may not be used by a specific project")]
103         [DllImport("user32.dll")]
104         public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
105
106         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This function is shared by various projects and may not be used by a specific project")]
107         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
108         public static extern IntPtr GetActiveWindow();
109
110         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This function is shared by various projects and may not be used by a specific project")]
111         [System.Runtime.InteropServices.DllImport("gdi32.dll")]
112         public static extern bool DeleteObject(IntPtr hObject);
113
114         [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This function is shared by various projects and may not be used by a specific project")]
115         [DllImport("User32", EntryPoint = "ScreenToClient", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
116         public static extern int ScreenToClient(IntPtr hWnd, [In, Out] POINT pt);
117     }
118 }