|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Forms; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | + |
| 12 | +namespace TimeControl |
| 13 | +{ |
| 14 | + public partial class ControlPanel : Form |
| 15 | + { |
| 16 | + #region Dllimport |
| 17 | + |
| 18 | + [Flags] |
| 19 | + public enum ACCESS_MASK : uint |
| 20 | + { |
| 21 | + DELETE = 0x00010000, |
| 22 | + READ_CONTROL = 0x00020000, |
| 23 | + WRITE_DAC = 0x00040000, |
| 24 | + WRITE_OWNER = 0x00080000, |
| 25 | + SYNCHRONIZE = 0x00100000, |
| 26 | + |
| 27 | + STANDARD_RIGHTS_REQUIRED = 0x000F0000, |
| 28 | + |
| 29 | + STANDARD_RIGHTS_READ = 0x00020000, |
| 30 | + STANDARD_RIGHTS_WRITE = 0x00020000, |
| 31 | + STANDARD_RIGHTS_EXECUTE = 0x00020000, |
| 32 | + |
| 33 | + STANDARD_RIGHTS_ALL = 0x001F0000, |
| 34 | + |
| 35 | + SPECIFIC_RIGHTS_ALL = 0x0000FFFF, |
| 36 | + |
| 37 | + ACCESS_SYSTEM_SECURITY = 0x01000000, |
| 38 | + |
| 39 | + MAXIMUM_ALLOWED = 0x02000000, |
| 40 | + |
| 41 | + GENERIC_READ = 0x80000000, |
| 42 | + GENERIC_WRITE = 0x40000000, |
| 43 | + GENERIC_EXECUTE = 0x20000000, |
| 44 | + GENERIC_ALL = 0x10000000, |
| 45 | + |
| 46 | + DESKTOP_READOBJECTS = 0x00000001, |
| 47 | + DESKTOP_CREATEWINDOW = 0x00000002, |
| 48 | + DESKTOP_CREATEMENU = 0x00000004, |
| 49 | + DESKTOP_HOOKCONTROL = 0x00000008, |
| 50 | + DESKTOP_JOURNALRECORD = 0x00000010, |
| 51 | + DESKTOP_JOURNALPLAYBACK = 0x00000020, |
| 52 | + DESKTOP_ENUMERATE = 0x00000040, |
| 53 | + DESKTOP_WRITEOBJECTS = 0x00000080, |
| 54 | + DESKTOP_SWITCHDESKTOP = 0x00000100, |
| 55 | + |
| 56 | + WINSTA_ENUMDESKTOPS = 0x00000001, |
| 57 | + WINSTA_READATTRIBUTES = 0x00000002, |
| 58 | + WINSTA_ACCESSCLIPBOARD = 0x00000004, |
| 59 | + WINSTA_CREATEDESKTOP = 0x00000008, |
| 60 | + WINSTA_WRITEATTRIBUTES = 0x00000010, |
| 61 | + WINSTA_ACCESSGLOBALATOMS = 0x00000020, |
| 62 | + WINSTA_EXITWINDOWS = 0x00000040, |
| 63 | + WINSTA_ENUMERATE = 0x00000100, |
| 64 | + WINSTA_READSCREEN = 0x00000200, |
| 65 | + |
| 66 | + WINSTA_ALL_ACCESS = 0x0000037F |
| 67 | + } |
| 68 | + |
| 69 | + [StructLayout(LayoutKind.Sequential)] |
| 70 | + public struct SECURITY_ATTRIBUTES |
| 71 | + { |
| 72 | + public int nLength; |
| 73 | + public IntPtr lpSecurityDescriptor; |
| 74 | + public int bInheritHandle; |
| 75 | + } |
| 76 | + |
| 77 | + [DllImport("kernel32.dll")] |
| 78 | + private static extern uint GetCurrentThreadId(); |
| 79 | + |
| 80 | + [DllImport("user32.dll", EntryPoint = "CreateDesktop", CharSet = CharSet.Unicode, SetLastError = true)] |
| 81 | + public static extern IntPtr CreateDesktop( |
| 82 | + [MarshalAs(UnmanagedType.LPWStr)] string desktopName, |
| 83 | + [MarshalAs(UnmanagedType.LPWStr)] string device, // must be null. |
| 84 | + [MarshalAs(UnmanagedType.LPWStr)] string deviceMode, // must be null, |
| 85 | + [MarshalAs(UnmanagedType.U4)] int flags, // use 0 |
| 86 | + [MarshalAs(UnmanagedType.U4)] ACCESS_MASK accessMask, |
| 87 | + [MarshalAs(UnmanagedType.LPStruct)] SECURITY_ATTRIBUTES attributes); |
| 88 | + |
| 89 | + [DllImport("user32.dll", EntryPoint = "CreateDesktop", CharSet = CharSet.Unicode, SetLastError = true)] |
| 90 | + public static extern IntPtr CreateDesktop( |
| 91 | + [MarshalAs(UnmanagedType.LPWStr)] string desktopName, |
| 92 | + [MarshalAs(UnmanagedType.LPWStr)] string device, // must be null. |
| 93 | + [MarshalAs(UnmanagedType.LPWStr)] string deviceMode, // must be null, |
| 94 | + [MarshalAs(UnmanagedType.U4)] int flags, // use 0 |
| 95 | + [MarshalAs(UnmanagedType.U4)] ACCESS_MASK accessMask, |
| 96 | + IntPtr attributes); |
| 97 | + |
| 98 | + [DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)] |
| 99 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 100 | + public static extern bool CloseDesktop(IntPtr handle); |
| 101 | + |
| 102 | + [DllImport("user32.dll")] |
| 103 | + public static extern bool SwitchDesktop(IntPtr hDesktop); |
| 104 | + |
| 105 | + |
| 106 | + [DllImport("user32.dll", SetLastError = true)] |
| 107 | + public static extern bool SetThreadDesktop(IntPtr hDesktop); |
| 108 | + |
| 109 | + [DllImport("user32.dll", SetLastError = true)] |
| 110 | + public static extern IntPtr GetThreadDesktop(uint dwThreadId); |
| 111 | + #endregion |
| 112 | + public ControlPanel() |
| 113 | + { |
| 114 | + InitializeComponent(); |
| 115 | + } |
| 116 | + |
| 117 | + private void startButton_Click(object sender, EventArgs e) |
| 118 | + { |
| 119 | + |
| 120 | + IntPtr nowDesktop = GetThreadDesktop(GetCurrentThreadId()); |
| 121 | + IntPtr newDesktop = CreateDesktop("Lock", null, null, 0, ACCESS_MASK.GENERIC_ALL, IntPtr.Zero); |
| 122 | + SwitchDesktop(newDesktop); |
| 123 | + System.Threading.Tasks.Task.Factory.StartNew(() => |
| 124 | + { |
| 125 | + SetThreadDesktop(newDesktop); |
| 126 | + Lock _lock = new Lock(Convert.ToInt32(timeBox.Value)); |
| 127 | + Application.Run(_lock); |
| 128 | + }).Wait(); |
| 129 | + SwitchDesktop(nowDesktop); |
| 130 | + CloseDesktop(newDesktop); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments