e08b0979499478dafc198b49ebd82289098e4e51
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / WindowExtensionMethods.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.Runtime.InteropServices;
8     using System.Windows;
9     using System.Windows.Interop;
10     using Microsoft.Tools.Common;
11
12     static class WindowExtensionMethods
13     {
14         public static void ShowContextHelpButton(this Window window)
15         {
16             IntPtr hwnd = new WindowInteropHelper(window).Handle;
17             IntPtr exStyle = Win32Interop.GetWindowLongPtr(hwnd, Win32Interop.GWL_EXSTYLE);
18             if (IntPtr.Size == 4)
19             {
20                 exStyle = new IntPtr(exStyle.ToInt32() | Win32Interop.WS_EX_CONTEXTHELP);
21             }
22             else
23             {
24                 exStyle = new IntPtr(exStyle.ToInt64() | ((long)Win32Interop.WS_EX_CONTEXTHELP));
25             }
26             Win32Interop.SetWindowLongPtr(new HandleRef(window, hwnd), Win32Interop.GWL_EXSTYLE, exStyle);
27         }
28
29         public static void HideMinMaxButton(this Window window)
30         {
31             IntPtr hwnd = new WindowInteropHelper(window).Handle;
32             IntPtr style = Win32Interop.GetWindowLongPtr(hwnd, Win32Interop.GWL_STYLE);
33             if (IntPtr.Size == 4)
34             {
35                 int intValue = style.ToInt32();
36                 intValue = SetBit(Win32Interop.WS_MAXIMIZEBOX, intValue, false);
37                 intValue = SetBit(Win32Interop.WS_MINIMIZEBOX, intValue, false);
38                 style = new IntPtr(intValue);
39             }
40             else
41             {
42                 long longValue = style.ToInt64();
43                 longValue = SetBit((long)Win32Interop.WS_MAXIMIZEBOX, longValue, false);
44                 longValue = SetBit((long)Win32Interop.WS_MINIMIZEBOX, longValue, false);
45                 style = new IntPtr(longValue);
46             }
47             Win32Interop.SetWindowLongPtr(new HandleRef(window, hwnd), Win32Interop.GWL_STYLE, style);
48         }
49
50         public static void AddWindowsHook(this Window window, HwndSourceHook wmHandler)
51         {
52             IntPtr hwnd = new WindowInteropHelper(window).Handle;
53             HwndSource source = HwndSource.FromHwnd(hwnd);
54             source.AddHook(wmHandler);
55         }
56
57         public static void RemoveWindowsHook(this Window window, HwndSourceHook wmHandler)
58         {
59             IntPtr hwnd = new WindowInteropHelper(window).Handle;
60             HwndSource source = HwndSource.FromHwnd(hwnd);
61             source.RemoveHook(wmHandler);
62         }
63
64         public static void HideIcon(this Window window)
65         {
66             IntPtr hwnd = new WindowInteropHelper(window).Handle;
67             IntPtr exStyle = Win32Interop.GetWindowLongPtr(hwnd, Win32Interop.GWL_EXSTYLE);
68             if (IntPtr.Size == 4)
69             {
70                 exStyle = new IntPtr(exStyle.ToInt32() | Win32Interop.WS_EX_DLGMODALFRAME);
71             }
72             else
73             {
74                 exStyle = new IntPtr(exStyle.ToInt64() | ((long)Win32Interop.WS_EX_DLGMODALFRAME));
75             }
76             Win32Interop.SetWindowLongPtr(new HandleRef(window, hwnd), Win32Interop.GWL_EXSTYLE, exStyle);
77
78             Win32Interop.SendMessage(hwnd, Win32Interop.WM_SETICON, new IntPtr(Win32Interop.ICON_SMALL), IntPtr.Zero);
79             Win32Interop.SendMessage(hwnd, Win32Interop.WM_SETICON, new IntPtr(Win32Interop.ICON_BIG), IntPtr.Zero);
80         }
81
82         private static long SetBit(long mask, long value, bool flag)
83         {
84             if (flag)
85             {
86                 return value | mask;
87             }
88             else
89             {
90                 return value & ~mask;
91             }
92         }
93
94         private static int SetBit(int mask, int value, bool flag)
95         {
96             if (flag)
97             {
98                 return value | mask;
99             }
100             else
101             {
102                 return value & ~mask;
103             }
104         }
105     }
106 }