using DevExpress.LookAndFeel; using DevExpress.XtraRichEdit.Model; using DW5S.Repostory; using DxHelper; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Loader; using System.Security; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Interop; namespace DW5S.App; public class AppHelper { public const string CultureName = "zh-CN"; public static void InitAppSettings() { Application.ThreadException += Application_ThreadException; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetHighDpiMode(HighDpiMode.SystemAware); ChsLocalizer.UseChs(); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("微软雅黑", 9); DevExpress.Skins.SkinManager.EnableFormSkins(); Application.ApplicationExit += (sender, e) => { AppConfigHelper.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName); AppConfigHelper.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName); AppConfigHelper.SetAppSetting("CompactMode", UserLookAndFeel.Default.CompactUIModeForced); }; string skinName = AppConfigHelper.Get("SkinName","WXI"); string paletteName = AppConfigHelper.Get("PaletteName"); bool compactMode = AppConfigHelper.Get("CompactMode",true); if (compactMode) UserLookAndFeel.ForceCompactUIMode(true, false); if (!string.IsNullOrEmpty(paletteName)) UserLookAndFeel.Default.SetSkinStyle(skinName, paletteName); else UserLookAndFeel.Default.SetSkinStyle(skinName); CultureInfo culture = CultureInfo.CreateSpecificCulture(CultureName); Application.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; Thread.CurrentThread.CurrentCulture = culture; CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; UserLookAndFeel.Default.StyleChanged += (sender, e) => { AppConfigHelper.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName); AppConfigHelper.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName); }; } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string errMsg = "发生未知错误,请联系管理员!" + e.Exception?.Message; if (e.GetType().ToString().EndsWith("OracleException")) IocContainer.Logger.Error(e.Exception,"数据库连接异常"); else IocContainer.Logger.Error(e.Exception,errMsg); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; IocContainer.Logger.Error(ex,"发生未知错误,程序即将退出!"); MsgBoxHelper.ShowError("发生未知错误,程序即将退出!"); } private static Assembly ResolveAssembly(AssemblyLoadContext alc, AssemblyName assemblyName) { string pathMaybe1 = Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll"); if (File.Exists(pathMaybe1)) return alc.LoadFromAssemblyPath(pathMaybe1); string pathMaybe2 = Path.Combine(AppContext.BaseDirectory, "AddIns", $"{assemblyName.Name}.dll"); if (File.Exists(pathMaybe2)) return alc.LoadFromAssemblyPath(pathMaybe2); return null; } public static void OneStart(string appName, Action act) { InitAppSettings(); AssemblyLoadContext.Default.Resolving += ResolveAssembly; //c++dll加入环境变量 string paths = Environment.GetEnvironmentVariable("PATH"); var dirs = Directory.EnumerateDirectories("AddIns", "*", SearchOption.AllDirectories); List list = new List { Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns") }; foreach (var item in dirs) { list.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, item)); } Environment.SetEnvironmentVariable("PATH", $"{paths};{string.Join(";", list)}"); using (Mutex mutex = new Mutex(true, appName)) { if (!string.IsNullOrWhiteSpace(appName) && !mutex.WaitOne(0, false)) { Process currentProcess = Process.GetCurrentProcess(); Process[] processesByName = Process.GetProcessesByName(currentProcess.ProcessName); bool isShow = false; for (int i = 0; i < processesByName.Length; i++) { Process process = processesByName[i]; if (process.Id != currentProcess.Id && process.MainWindowHandle != IntPtr.Zero) { isShow = true; ToolWinApi.SetForegroundWindow(process.MainWindowHandle); ToolWinApi.RestoreWindowAsync(process.MainWindowHandle); break; } } if (!isShow) { MsgBoxHelper.ShowWarning("已有相同程序在运行,不能重复启动!"); } } else { act(); } } } public static void MultStart(Action act) { act(); } } #pragma warning disable CS0649 // 从未对字段“ToolWinApi.Import.WINDOWPOS.hwnd”赋值,字段将一直保持其默认值 public static class ToolWinApi { public class WinProcHook { public event Action OnDataReceived; public event Action OnWindowPosChanged; public bool ProcessWmCopyData { get; set; } public bool ProcessWmWindowPosChanged { get; set; } [SecuritySafeCritical] public static ToolWinApi.WinProcHook Get(IntPtr hwnd) { ToolWinApi.WinProcHook winProcHook; if (!ToolWinApi.winProcs.TryGetValue(hwnd, out winProcHook)) { winProcHook = new ToolWinApi.WinProcHook(hwnd); ToolWinApi.winProcs.Add(hwnd, winProcHook); } return winProcHook; } private WinProcHook(IntPtr hwnd) { HwndSource hwndSource = HwndSource.FromHwnd(hwnd); hwndSource.AddHook(new HwndSourceHook(this.HookProc)); } [SecuritySafeCritical] private IntPtr HookProc(IntPtr handle, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (this.ProcessWmWindowPosChanged && msg == 71) { ToolWinApi.Import.WINDOWPOS wINDOWPOS = (ToolWinApi.Import.WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(ToolWinApi.Import.WINDOWPOS)); ToolWinApi.WindowPosition windowPosition = new ToolWinApi.WindowPosition(); windowPosition.Hwnd = wINDOWPOS.hwnd; windowPosition.HwndInsertAfter = wINDOWPOS.hwndInsertAfter; windowPosition.Left = wINDOWPOS.x; windowPosition.Top = wINDOWPOS.y; windowPosition.Width = wINDOWPOS.cx; windowPosition.Height = wINDOWPOS.cy; windowPosition.Flags = (ToolWinApi.SetWindowPosFlags)wINDOWPOS.flags; if (this.OnWindowPosChanged != null) { this.OnWindowPosChanged(windowPosition); } } if (this.ProcessWmCopyData && msg == 74) { ToolWinApi.Import.COPYDATASTRUCT cOPYDATASTRUCT = (ToolWinApi.Import.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(ToolWinApi.Import.COPYDATASTRUCT)); byte[] array = new byte[1]; int num = cOPYDATASTRUCT.cbData / Marshal.SizeOf(array[0]); array = new byte[num]; Marshal.Copy(cOPYDATASTRUCT.lpData, array, 0, array.Length); if (this.OnDataReceived != null) { this.OnDataReceived(array); } handled = true; } return IntPtr.Zero; } } public class WindowPosition { public IntPtr Hwnd { get; set; } public IntPtr HwndInsertAfter { get; set; } public int Left { get; set; } public int Top { get; set; } public int Width { get; set; } public int Height { get; set; } public ToolWinApi.SetWindowPosFlags Flags { get; set; } } [Flags] public enum SetWindowPosFlags { NoSize = 1, NoMove = 2, NoZOrder = 4, NoRedraw = 8, NoActivate = 16, DrawFrame = 32, FrameChanged = 32, ShowWindow = 64, HideWindow = 128, NoCopyBits = 256, NoOwnerZOrder = 512, NoReposition = 512, NoSendChanging = 1024, DeferErase = 8192, AsyncWindowPos = 16384 } public enum SpecialFolderType { CommonDocuments = 46 } private static class Import { public enum SpecialFolderType { AdministrativeTools = 48, CommonAdministrativeTools = 47, ApplicationData = 26, CommonAppData = 35, CommonDocuments = 46, Cookies = 33, CreateFlag = 32768, History = 34, InternetCache = 32, LocalApplicationData = 28, MyPictures = 39, Personal = 5, ProgramFiles = 38, CommonProgramFiles = 43, System = 37, Windows = 36, Fonts = 20 } public enum RFlags { Any = 65535, RegNone = 1, Noexpand = 268435456, RegBinary = 8, Dword = 24, RegDword = 16, Qword = 72, RegQword = 64, RegSz = 2, RegMultiSz = 32, RegExpandSz = 4, RrfZeroonfailure = 536870912 } public enum RType { RegNone, RegSz, RegExpandSz, RegMultiSz = 7, RegBinary = 3, RegDword, RegQword = 11, RegQwordLittleEndian = 11, RegDwordLittleEndian = 4, RegDwordBigEndian, RegLink, RegResourceList = 8, RegFullResourceDescriptor, RegResourceRequirementsList } public enum HookType { WH_JOURNALRECORD, WH_JOURNALPLAYBACK, WH_KEYBOARD, WH_GETMESSAGE, WH_CALLWNDPROC, WH_CBT, WH_SYSMSGFILTER, WH_MOUSE, WH_HARDWARE, WH_DEBUG, WH_SHELL, WH_FOREGROUNDIDLE, WH_CALLWNDPROCRET, WH_KEYBOARD_LL, WH_MOUSE_LL } public struct WINDOWPLACEMENT { public int length; public int flags; public ToolWinApi.Import.ShowWindowCommands showCmd; public System.Drawing.Point ptMinPosition; public System.Drawing.Point ptMaxPosition; public Rectangle rcNormalPosition; } public enum ShowWindowCommands { Hide, Normal, ShowMinimized, ShowMaximized, ShowNoActivate, Show, Minimize, ShowMinNoActive, ShowNA, Restore, ShowDefault, ForceMinimize } public enum GetWindowCmd : uint { GW_HWNDFIRST, GW_HWNDLAST, GW_HWNDNEXT, GW_HWNDPREV, GW_OWNER, GW_CHILD, GW_ENABLEDPOPUP } public enum SetWindowPosFlags : uint { NOSIZE = 1u, NOMOVE, NOZORDER = 4u, NOREDRAW = 8u, NOACTIVATE = 16u, DRAWFRAME = 32u, FRAMECHANGED = 32u, SHOWWINDOW = 64u, HIDEWINDOW = 128u, NOCOPYBITS = 256u, NOOWNERZORDER = 512u, NOREPOSITION = 512u, NOSENDCHANGING = 1024u, DEFERERASE = 8192u, ASYNCWINDOWPOS = 16384u } public struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public int flags; } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; public IntPtr lpData; } public enum WM : uint { WM_COPYDATA = 74u, WM_WINDOWPOSCHANGED = 71u, WM_WINDOWPOSCHANGING = 70u } public enum GWL { GWL_WNDPROC = -4, GWL_HINSTANCE = -6, GWL_HWNDPARENT = -8, GWL_STYLE = -16, GWL_EXSTYLE = -20, GWL_USERDATA = -21, GWL_ID = -12 } [Flags] public enum WS : uint { WS_OVERLAPPED = 0u, WS_POPUP = 2147483648u, WS_CHILD = 1073741824u, WS_MINIMIZE = 536870912u, WS_VISIBLE = 268435456u, WS_DISABLED = 134217728u, WS_CLIPSIBLINGS = 67108864u, WS_CLIPCHILDREN = 33554432u, WS_MAXIMIZE = 16777216u, WS_BORDER = 8388608u, WS_DLGFRAME = 4194304u, WS_VSCROLL = 2097152u, WS_HSCROLL = 1048576u, WS_SYSMENU = 524288u, WS_THICKFRAME = 262144u, WS_GROUP = 131072u, WS_TABSTOP = 65536u, WS_MINIMIZEBOX = 131072u, WS_MAXIMIZEBOX = 65536u, WS_CAPTION = 12582912u, WS_TILED = 0u, WS_ICONIC = 536870912u, WS_SIZEBOX = 262144u, WS_TILEDWINDOW = 13565952u, WS_OVERLAPPEDWINDOW = 13565952u, WS_POPUPWINDOW = 2156396544u, WS_CHILDWINDOW = 1073741824u, WS_EX_DLGMODALFRAME = 1u, WS_EX_NOPARENTNOTIFY = 4u, WS_EX_TOPMOST = 8u, WS_EX_ACCEPTFILES = 16u, WS_EX_TRANSPARENT = 32u, WS_EX_MDICHILD = 64u, WS_EX_TOOLWINDOW = 128u, WS_EX_WINDOWEDGE = 256u, WS_EX_CLIENTEDGE = 512u, WS_EX_CONTEXTHELP = 1024u, WS_EX_RIGHT = 4096u, WS_EX_LEFT = 0u, WS_EX_RTLREADING = 8192u, WS_EX_LTRREADING = 0u, WS_EX_LEFTSCROLLBAR = 16384u, WS_EX_RIGHTSCROLLBAR = 0u, WS_EX_CONTROLPARENT = 65536u, WS_EX_STATICEDGE = 131072u, WS_EX_APPWINDOW = 262144u, WS_EX_OVERLAPPEDWINDOW = 768u, WS_EX_PALETTEWINDOW = 392u, WS_EX_LAYERED = 524288u, WS_EX_NOINHERITLAYOUT = 1048576u, WS_EX_LAYOUTRTL = 4194304u, WS_EX_COMPOSITED = 33554432u, WS_EX_NOACTIVATE = 134217728u } public const int MAX_PATH = 260; public const int KEY_QUERY_VALUE = 1; public const int KEY_SET_VALUE = 2; public const int KEY_CREATE_SUB_KEY = 4; public const int KEY_ENUMERATE_SUB_KEYS = 8; public const int KEY_NOTIFY = 16; public const int KEY_CREATE_LINK = 32; public const int KEY_WOW64_32KEY = 512; public const int KEY_WOW64_64KEY = 256; public const int KEY_WOW64_RES = 768; public const int KEY_READ = 131097; public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(2147483650u); public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(2147483649u); public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); public static readonly IntPtr HWND_TOP = new IntPtr(0); public static readonly IntPtr HWND_BOTTOM = new IntPtr(1); [DllImport("shell32.dll")] public static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, [Out] StringBuilder pszPath); [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)] public static extern uint RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, ref ToolWinApi.Import.RType lpType, byte[] pvData, ref uint pcbData); [DllImport("advapi32.dll", SetLastError = true)] public static extern int RegCloseKey(IntPtr hKey); [DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, out IntPtr hkResult); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShowWindow(IntPtr hWnd, ToolWinApi.Import.ShowWindowCommands nCmdShow); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref ToolWinApi.Import.WINDOWPLACEMENT lpwndpl); [DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr onlyZero); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(ToolWinApi.Import.HookType hookType, IntPtr lpfn, IntPtr hMod, uint dwThreadId); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr LoadLibrary(string lpFileName); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowPlacement(IntPtr hWnd, ref ToolWinApi.Import.WINDOWPLACEMENT lpwndpl); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern IntPtr SetFocus(IntPtr hWnd); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, ToolWinApi.Import.SetWindowPosFlags uFlags); [DllImport("user32.dll", SetLastError = true)] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", SetLastError = true)] public static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindow(IntPtr hWnd, ToolWinApi.Import.GetWindowCmd uCmd); [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); } private static Dictionary winProcs = new Dictionary(); public static readonly IntPtr HwndTopmost = ToolWinApi.Import.HWND_TOPMOST; public static readonly IntPtr HwndNoTopmost = ToolWinApi.Import.HWND_NOTOPMOST; public static readonly IntPtr HwndTop = ToolWinApi.Import.HWND_TOP; public static readonly IntPtr HwndBottom = ToolWinApi.Import.HWND_BOTTOM; [SecuritySafeCritical] public static IntPtr LoadLibrary(string filePath) { return ToolWinApi.Import.LoadLibrary(filePath); } [SecuritySafeCritical] public static void FreeLibrary(IntPtr hmodule) { ToolWinApi.Import.FreeLibrary(hmodule); } [SecuritySafeCritical] public static IntPtr GetProcAddress(IntPtr hmodule, string procName) { return ToolWinApi.Import.GetProcAddress(hmodule, procName); } [SecuritySafeCritical] public static void MakeWindowChild(IntPtr hwnd) { ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -16); wS |= ToolWinApi.Import.WS.WS_CHILD; wS &= ~ToolWinApi.Import.WS.WS_POPUP; ToolWinApi.Import.SetWindowLong(hwnd, -16, (int)wS); } [SecuritySafeCritical] public static void SetWindowParent(IntPtr hwnd, IntPtr newParentHwnd) { ToolWinApi.Import.SetParent(hwnd, newParentHwnd); } [SecuritySafeCritical] public static void MoveWindow(IntPtr hwnd, int x, int y, int width, int height, bool repaint) { ToolWinApi.Import.MoveWindow(hwnd, x, y, width, height, repaint); } [SecuritySafeCritical] public static IntPtr GetPrevWindow(IntPtr hwnd) { return ToolWinApi.Import.GetWindow(hwnd, ToolWinApi.Import.GetWindowCmd.GW_HWNDPREV); } [SecuritySafeCritical] public static IntPtr GetNextWindow(IntPtr hwnd) { return ToolWinApi.Import.GetWindow(hwnd, ToolWinApi.Import.GetWindowCmd.GW_HWNDNEXT); } [SecuritySafeCritical] public static void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int width, int height, ToolWinApi.SetWindowPosFlags flags) { ToolWinApi.Import.SetWindowPos(hwnd, hwndInsertAfter, x, y, width, height, (ToolWinApi.Import.SetWindowPosFlags)flags); } [SecuritySafeCritical] public static IntPtr SetFocus(IntPtr hwnd) { return ToolWinApi.Import.SetFocus(hwnd); } [SecuritySafeCritical] public static bool SetForegroundWindow(IntPtr hwnd) { return ToolWinApi.Import.SetForegroundWindow(hwnd); } [SecuritySafeCritical] public static bool RestoreWindowAsync(IntPtr hwnd) { return ToolWinApi.Import.ShowWindowAsync(hwnd, 9); } [SecuritySafeCritical] public static void SendData(IntPtr hwnd, byte[] data) { int cbData; IntPtr intPtr = ToolWinApi.IntPtrAlloc(data, out cbData); IntPtr intPtr2 = ToolWinApi.IntPtrAlloc(new ToolWinApi.Import.COPYDATASTRUCT { dwData = (IntPtr)49329, lpData = intPtr, cbData = cbData }); ToolWinApi.Import.SendMessage(hwnd, 74, IntPtr.Zero, intPtr2); ToolWinApi.IntPtrFree(intPtr2); intPtr2 = IntPtr.Zero; ToolWinApi.IntPtrFree(intPtr); intPtr = IntPtr.Zero; } [SecuritySafeCritical] public static IntPtr IntPtrAlloc(T param) { IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(param)); Marshal.StructureToPtr(param, intPtr, false); return intPtr; } [SecuritySafeCritical] public static IntPtr IntPtrAlloc(byte[] data, out int size) { size = Marshal.SizeOf(data[0]) * data.Length; IntPtr intPtr = Marshal.AllocHGlobal(size); Marshal.Copy(data, 0, intPtr, data.Length); return intPtr; } [SecuritySafeCritical] public static void IntPtrFree(IntPtr preAllocated) { if (IntPtr.Zero == preAllocated) { return; } Marshal.FreeHGlobal(preAllocated); preAllocated = IntPtr.Zero; } [SecuritySafeCritical] public static IntPtr SetWndProcHook(IntPtr hwnd, IntPtr module, IntPtr hookPtr) { return ToolWinApi.Import.SetWindowsHookEx(ToolWinApi.Import.HookType.WH_CALLWNDPROC, hookPtr, module, (uint)ToolWinApi.GetWindowThreadProcessId(hwnd)); } [SecuritySafeCritical] public static bool UnhookWindowsHookEx(IntPtr hhook) { return ToolWinApi.Import.UnhookWindowsHookEx(hhook); } [SecuritySafeCritical] public static int GetWindowThreadProcessId(IntPtr hwnd) { return (int)ToolWinApi.Import.GetWindowThreadProcessId(hwnd, IntPtr.Zero); } [SecuritySafeCritical] public static Rectangle? GetWindowNormalPlacement(IntPtr hwnd) { ToolWinApi.Import.WINDOWPLACEMENT wINDOWPLACEMENT = default(ToolWinApi.Import.WINDOWPLACEMENT); wINDOWPLACEMENT.length = Marshal.SizeOf(wINDOWPLACEMENT); if (!ToolWinApi.Import.GetWindowPlacement(hwnd, ref wINDOWPLACEMENT)) { return null; } return new Rectangle?(wINDOWPLACEMENT.rcNormalPosition); } [SecuritySafeCritical] public static bool CopyWindowPlacement(IntPtr source, IntPtr dest) { ToolWinApi.Import.WINDOWPLACEMENT wINDOWPLACEMENT = default(ToolWinApi.Import.WINDOWPLACEMENT); wINDOWPLACEMENT.length = Marshal.SizeOf(wINDOWPLACEMENT); return ToolWinApi.Import.GetWindowPlacement(source, ref wINDOWPLACEMENT) && ToolWinApi.Import.SetWindowPlacement(dest, ref wINDOWPLACEMENT); } [SecuritySafeCritical] public static bool SetWindowState(IntPtr hwnd,System.Windows.WindowState state) { ToolWinApi.Import.ShowWindowCommands nCmdShow; switch (state) { case System.Windows.WindowState.Minimized: nCmdShow = ToolWinApi.Import.ShowWindowCommands.ShowMinimized; break; case System.Windows.WindowState.Maximized: nCmdShow = ToolWinApi.Import.ShowWindowCommands.ShowMaximized; break; default: nCmdShow = ToolWinApi.Import.ShowWindowCommands.Normal; break; } return ToolWinApi.Import.ShowWindow(hwnd, nCmdShow); } [SecuritySafeCritical] public static int SetWindowNoMaximize(IntPtr hwnd) { ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -16); wS &= ~ToolWinApi.Import.WS.WS_TABSTOP; return ToolWinApi.Import.SetWindowLong(hwnd, -16, (int)wS); } [SecuritySafeCritical] public static void MakeWindowTransparent(IntPtr hwnd) { ToolWinApi.Import.WS wS = (ToolWinApi.Import.WS)ToolWinApi.Import.GetWindowLong(hwnd, -20); wS |= (ToolWinApi.Import.WS.WS_SYSMENU | ToolWinApi.Import.WS.WS_EX_TRANSPARENT); ToolWinApi.Import.SetWindowLong(hwnd, -20, (int)wS); } [SecuritySafeCritical] public static string GetFolderPath(ToolWinApi.SpecialFolderType folderType) { StringBuilder stringBuilder = new StringBuilder(260); int num = ToolWinApi.Import.SHGetFolderPath(IntPtr.Zero, (int)folderType, IntPtr.Zero, 0u, stringBuilder); if (num != 0) { throw new Win32Exception(num); } return stringBuilder.ToString(); } } #pragma warning restore CS0649 // 从未对字段“ToolWinApi.Import.WINDOWPOS.hwnd”赋值,字段将一直保持其默认值