2 * Copyright (c) 2007-2009 SlimDX Group
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 using System.Runtime.InteropServices;
25 using System.Security;
27 namespace SampleFramework
29 static class NativeMethods
31 [SuppressUnmanagedCodeSecurityAttribute]
32 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
33 [return: MarshalAs(UnmanagedType.Bool)]
34 public static extern bool PeekMessage(out NativeMessage message, IntPtr hwnd, uint messageFilterMin, uint messageFilterMax, uint flags);
36 [SuppressUnmanagedCodeSecurityAttribute]
37 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
38 [return: MarshalAs(UnmanagedType.Bool)]
39 static extern bool GetClientRect(IntPtr hWnd, out NativeRectangle lpRect);
41 [SuppressUnmanagedCodeSecurityAttribute]
42 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
43 [return: MarshalAs(UnmanagedType.Bool)]
44 static extern bool GetWindowRect(IntPtr hWnd, out NativeRectangle lpRect);
46 [SuppressUnmanagedCodeSecurityAttribute]
47 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
48 [return: MarshalAs(UnmanagedType.Bool)]
49 public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
51 [SuppressUnmanagedCodeSecurityAttribute]
52 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
53 [return: MarshalAs(UnmanagedType.Bool)]
54 public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
56 [SuppressUnmanagedCodeSecurityAttribute]
57 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
58 public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
60 [SuppressUnmanagedCodeSecurityAttribute]
61 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
62 public static extern uint GetWindowLong(IntPtr hWnd, int nIndex);
64 [SuppressUnmanagedCodeSecurityAttribute]
65 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
66 [return: MarshalAs(UnmanagedType.Bool)]
67 public static extern bool IsIconic(IntPtr hWnd);
69 [SuppressUnmanagedCodeSecurityAttribute]
70 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
71 [return: MarshalAs(UnmanagedType.Bool)]
72 public static extern bool IsZoomed(IntPtr hWnd);
74 [SuppressUnmanagedCodeSecurityAttribute]
75 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
76 [return: MarshalAs(UnmanagedType.Bool)]
77 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
79 [SuppressUnmanagedCodeSecurityAttribute]
80 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
81 [return: MarshalAs(UnmanagedType.Bool)]
82 public static extern bool AdjustWindowRect(ref NativeRectangle lpRect, uint dwStyle, [MarshalAs(UnmanagedType.Bool)]bool bMenu);
84 [SuppressUnmanagedCodeSecurityAttribute]
85 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = false)]
86 public static extern uint SetThreadExecutionState(uint esFlags);
88 [SuppressUnmanagedCodeSecurityAttribute]
89 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
90 [return: MarshalAs(UnmanagedType.Bool)]
91 public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
93 [SuppressUnmanagedCodeSecurityAttribute]
94 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
95 public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
97 public static Rectangle GetClientRectangle(IntPtr handle)
100 if (!GetClientRect(handle, out rect))
101 return Rectangle.Empty;
103 return Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom);
106 public static Rectangle GetWindowRectangle(IntPtr handle)
108 NativeRectangle rect;
109 if (!GetWindowRect(handle, out rect))
110 return Rectangle.Empty;
112 return Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom);