Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NativeMethods.cs
Go to the documentation of this file.
1using System;
3
4namespace ReLogic.OS.Windows;
5
6internal static class NativeMethods
7{
8 public delegate IntPtr WndProcCallback(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
9
10 public enum StdHandleType
11 {
12 Input = -10,
13 Output = -11,
14 Error = -12
15 }
16
17 [Flags]
18 public enum ConsoleMode
19 {
21 LineInput = 2,
22 EchoInput = 4,
23 WindowInput = 8,
24 MouseInput = 0x10,
25 InsertMode = 0x20,
26 QuickEditMode = 0x40,
27 ExtendedFlags = 0x80,
28 AutoPosition = 0x100,
30 }
31
32 [Flags]
33 public enum FlashFlags : uint
34 {
35 Stop = 0u,
36 Caption = 1u,
37 Tray = 2u,
38 CaptionAndTray = 3u,
39 Timer = 4u,
40 UntilFocused = 0xCu
41 }
42
43 public struct FlashInfo
44 {
45 private uint _cbSize;
46
47 private IntPtr _hWnd;
48
50
51 private uint _uCount;
52
53 private uint _dwTimeout;
54
55 public static FlashInfo CreateStart(IntPtr hWnd)
56 {
57 FlashInfo result = default(FlashInfo);
58 result._cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FlashInfo)));
59 result._hWnd = hWnd;
60 result._dwFlags = FlashFlags.CaptionAndTray | FlashFlags.UntilFocused;
61 result._uCount = uint.MaxValue;
62 result._dwTimeout = 0u;
63 return result;
64 }
65
66 public static FlashInfo CreateStop(IntPtr hWnd)
67 {
68 FlashInfo result = default(FlashInfo);
69 result._cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FlashInfo)));
70 result._hWnd = hWnd;
71 result._dwFlags = FlashFlags.Stop;
72 result._uCount = uint.MaxValue;
73 result._dwTimeout = 0u;
74 return result;
75 }
76 }
77
78 public enum DeviceCap
79 {
80 VertRes = 10,
81 DesktopVertRes = 117
82 }
83
84 [Flags]
85 private enum FileOperationFlags : ushort
86 {
87 FOF_SILENT = 4,
88 FOF_NOCONFIRMATION = 0x10,
89 FOF_ALLOWUNDO = 0x40,
90 FOF_SIMPLEPROGRESS = 0x100,
91 FOF_NOERRORUI = 0x400,
92 FOF_WANTNUKEWARNING = 0x4000
93 }
94
95 private enum FileOperationType : uint
96 {
97 FO_MOVE = 1u,
98 FO_COPY,
101 }
102
103 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
104 private struct SHFILEOPSTRUCT
105 {
106 public IntPtr hwnd;
107
108 [MarshalAs(UnmanagedType.U4)]
110
111 public string pFrom;
112
113 public string pTo;
114
116
117 [MarshalAs(UnmanagedType.Bool)]
119
121
122 public string lpszProgressTitle;
123 }
124
125 [DllImport("user32.dll", CharSet = CharSet.Unicode)]
126 public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
127
128 [DllImport("user32.dll", CharSet = CharSet.Unicode)]
129 public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
130
131 [DllImport("user32.dll", CharSet = CharSet.Unicode)]
132 public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
133
134 [DllImport("user32.dll")]
135 public static extern IntPtr GetDC(IntPtr hWnd);
136
137 [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
138 public static extern bool TranslateMessage(ref Message message);
139
140 [DllImport("user32.dll", CharSet = CharSet.Unicode)]
141 public static extern IntPtr GetForegroundWindow();
142
143 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
144 public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out ConsoleMode lpMode);
145
146 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
147 public static extern bool SetConsoleMode(IntPtr hConsoleHandle, ConsoleMode dwMode);
148
149 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
150 public static extern IntPtr GetStdHandle(StdHandleType nStdHandle);
151
152 [DllImport("user32.dll")]
153 [return: MarshalAs(UnmanagedType.Bool)]
154 public static extern bool FlashWindowEx(ref FlashInfo flashInfo);
155
156 [DllImport("gdi32.dll")]
157 public static extern int GetDeviceCaps(IntPtr hdc, DeviceCap nIndex);
158
159 [DllImport("shell32.dll", CharSet = CharSet.Auto)]
160 private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
161
162 private static bool Send(string path, FileOperationFlags flags)
163 {
164 try
165 {
166 SHFILEOPSTRUCT sHFILEOPSTRUCT = default(SHFILEOPSTRUCT);
167 sHFILEOPSTRUCT.wFunc = FileOperationType.FO_DELETE;
168 sHFILEOPSTRUCT.pFrom = path + "\0\0";
169 sHFILEOPSTRUCT.fFlags = FileOperationFlags.FOF_ALLOWUNDO | flags;
170 SHFILEOPSTRUCT FileOp = sHFILEOPSTRUCT;
171 SHFileOperation(ref FileOp);
172 return true;
173 }
174 catch (Exception)
175 {
176 return false;
177 }
178 }
179
180 private static bool Send(string path)
181 {
182 return Send(path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_WANTNUKEWARNING);
183 }
184
185 public static bool MoveToRecycleBin(string path)
186 {
187 return Send(path, FileOperationFlags.FOF_SILENT | FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI);
188 }
189
190 private static bool DeleteFile(string path, FileOperationFlags flags)
191 {
192 try
193 {
194 SHFILEOPSTRUCT sHFILEOPSTRUCT = default(SHFILEOPSTRUCT);
195 sHFILEOPSTRUCT.wFunc = FileOperationType.FO_DELETE;
196 sHFILEOPSTRUCT.pFrom = path + "\0\0";
197 sHFILEOPSTRUCT.fFlags = flags;
198 SHFILEOPSTRUCT FileOp = sHFILEOPSTRUCT;
199 SHFileOperation(ref FileOp);
200 return true;
201 }
202 catch (Exception)
203 {
204 return false;
205 }
206 }
207
208 private static bool DeleteCompletelySilent(string path)
209 {
210 return DeleteFile(path, FileOperationFlags.FOF_SILENT | FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI);
211 }
212
213 [DllImport("kernel32.dll")]
214 private static extern IntPtr GetConsoleWindow();
215
216 [DllImport("user32.dll")]
217 private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
218
219 public static void HideConsole()
220 {
222 }
223}
static bool GetConsoleMode(IntPtr hConsoleHandle, out ConsoleMode lpMode)
static IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
static IntPtr GetDC(IntPtr hWnd)
static int GetDeviceCaps(IntPtr hdc, DeviceCap nIndex)
static IntPtr GetStdHandle(StdHandleType nStdHandle)
static IntPtr GetForegroundWindow()
static bool SetConsoleMode(IntPtr hConsoleHandle, ConsoleMode dwMode)
delegate IntPtr WndProcCallback(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
static bool TranslateMessage(ref Message message)
static bool DeleteFile(string path, FileOperationFlags flags)
static bool Send(string path)
static bool DeleteCompletelySilent(string path)
static bool Send(string path, FileOperationFlags flags)
static bool MoveToRecycleBin(string path)
static bool ShowWindow(IntPtr hWnd, int nCmdShow)
static bool FlashWindowEx(ref FlashInfo flashInfo)
static IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
static int SHFileOperation(ref SHFILEOPSTRUCT FileOp)
static uint ToUInt32(object? value)
Definition Convert.cs:1470
static int SizeOf(object structure)
Definition Marshal.cs:697
static FlashInfo CreateStop(IntPtr hWnd)
static FlashInfo CreateStart(IntPtr hWnd)
static IntPtr MaxValue
Definition IntPtr.cs:30