Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using Microsoft.Win32;
- namespace OsLR1
- {
- public partial class Form1 : Form
- {
- #region Хуки и прочее(Для блока Alt + Space, Alt + Tab и WinKey)
- private const int WH_KEYBOARD_LL = 13;//Keyboard hook;
- private delegate IntPtr LowLevelKeyboardProcDelegate(int nCode, IntPtr wParam, IntPtr lParam);
- private LowLevelKeyboardProcDelegate m_callback, m_callback_1, m_callback_2;
- private IntPtr m_hHook, m_hHook_1, m_hHook_2;
- [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
- private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
- [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
- private static extern bool UnhookWindowsHookEx(IntPtr hhk);
- [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
- private struct KBDLLHOOKSTRUCT
- {
- public Keys key;
- }
- [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
- private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
- [System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true)]
- private static extern IntPtr GetModuleHandle(IntPtr lpModuleName);
- //Захват alt+tab
- public IntPtr LowLevelKeyboardHookProc_alt_tab(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
- if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Tab)
- {
- return (IntPtr)1;
- }
- }
- return CallNextHookEx(m_hHook, nCode, wParam, lParam);
- }
- //Захват alt+space
- public IntPtr LowLevelKeyboardHookProc_alt_space(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
- if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Space)
- {
- return (IntPtr)1;
- }
- }
- return CallNextHookEx(m_hHook_2, nCode, wParam, lParam);
- }
- //Захват winkey
- public IntPtr LowLevelKeyboardHookProc_win(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0)
- {
- KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
- if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin)
- {
- return (IntPtr)1;
- }
- }
- return CallNextHookEx(m_hHook_1, nCode, wParam, lParam);
- }
- #endregion
- #region Выключение, Перезагрузка
- [DllImport("advapi32.dll", EntryPoint = "InitiateSystemShutdownEx")]
- static extern int InitiateSystemShutdown(string lpMachineName, string lpMessage, int dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown);
- [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
- internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
- ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
- [DllImport("kernel32.dll", ExactSpelling = true)]
- internal static extern IntPtr GetCurrentProcess();
- [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
- internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
- [DllImport("advapi32.dll", SetLastError = true)]
- internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
- [DllImport("user32.dll", EntryPoint = "LockWorkStation")]
- static extern bool LockWorkStation();
- [StructLayout(LayoutKind.Sequential, Pack = 1)]
- internal struct TokPriv1Luid
- {
- public int Count;
- public long Luid;
- public int Attr;
- }
- internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
- internal const int TOKEN_QUERY = 0x00000008;
- internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
- internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
- private void SetPriv()
- {
- TokPriv1Luid tkp;
- IntPtr htok = IntPtr.Zero;
- if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok))
- {
- tkp.Count = 1;
- tkp.Attr = SE_PRIVILEGE_ENABLED;
- tkp.Luid = 0;
- LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tkp.Luid);
- AdjustTokenPrivileges(htok, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero);
- }
- }
- public int Halt(bool RSh, bool Force)
- {
- SetPriv();
- return InitiateSystemShutdown(null, null, 0, Force, RSh);
- }
- #endregion
- FormEnabled fe;
- public Form1()
- {
- InitializeComponent();
- fe = new FormEnabled();
- timer1.Tick += delegate
- {
- foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
- {
- if (GetAsyncKeyState(i) == -32767)
- {
- if (Enum.GetName(typeof(Keys), i) == "Home")
- {
- Show();
- timer1.Enabled = false;
- }
- }
- }
- };
- }
- //Блокровка кнопки Закрыть
- class FormEnabled
- {
- private const int SC_CLOSE = 0xF060;
- private const int MF_GRAYED = 0x1;
- internal const int MF_ENABLED = 0x0;
- [DllImport("user32.dll")]
- private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
- [DllImport("user32.dll")]
- private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);
- [DllImport("user32.dll")]
- public static extern int RemoveMenu(int systemMenu, int itemPosition, int flag);
- [DllImport("user32.dll")]
- public static extern int GetMenuItemCount(int systemMenu);
- [DllImport("user32.dll")]
- public static extern int DrawMenuBar(IntPtr currentWindow);
- public void FormCloseEnabled(Form form, bool enabled)
- {
- MethodInvoker method = delegate
- {
- IntPtr menu;
- int itemCount;
- EnableMenuItem(GetSystemMenu(form.Handle, false), SC_CLOSE, enabled ? MF_ENABLED : MF_GRAYED);
- menu = GetSystemMenu(form.Handle, false);
- itemCount = GetMenuItemCount(menu.ToInt32());
- RemoveMenu(menu.ToInt32(), itemCount - 1, enabled ? 1 : 2);
- DrawMenuBar(form.Handle);
- };
- if (form.InvokeRequired)
- form.BeginInvoke(method);
- else
- method.Invoke();
- }
- }
- private void CheckBox1_CheckedChanged(object sender, EventArgs e)
- {
- fe.FormCloseEnabled(this, !checkBox1.Checked);
- }
- //Блокровка кнопки Восстановить
- private void CheckBox2_CheckedChanged(object sender, EventArgs e)
- {
- MaximizeBox = !checkBox2.Checked;
- fe.FormCloseEnabled(this, !checkBox1.Checked);
- }
- //Блокировка кнопки Скрыть
- private void CheckBox3_CheckedChanged(object sender, EventArgs e)
- {
- MinimizeBox = !checkBox3.Checked;
- fe.FormCloseEnabled(this, !checkBox1.Checked);
- }
- //Показать список работающих программ
- private void Button4_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- foreach (Process process in Process.GetProcesses())
- {
- if (!string.IsNullOrEmpty(process.MainWindowTitle))
- {
- listBox1.Items.Add(process.MainWindowTitle);
- }
- }
- }
- //Смена обоев
- private void Button7_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFile = new OpenFileDialog()
- {
- Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.JPEG)|*.PNG;*.BMP;*.JPG;*.JPEG"
- };
- if (openFile.ShowDialog() == DialogResult.OK)
- {
- WinAPI.SystemParametersInfo(WinAPI.SPI_SETDESKWALLPAPER, 1,
- openFile.FileName, WinAPI.SPIF_UPDATEINIFILE | WinAPI.SPIF_SENDWININICHANGE);
- }
- }
- class WinAPI
- {
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
- public const int SPI_SETDESKWALLPAPER = 20;
- public const int SPIF_UPDATEINIFILE = 0x1;
- public const int SPIF_SENDWININICHANGE = 0x2;
- }
- //Блокировка Alt + F4
- private bool altF4 = false, altF4Block = false;
- private void CheckBox10_CheckedChanged(object sender, EventArgs e)
- {
- altF4Block = checkBox10.Checked;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (altF4 && altF4Block)
- {
- if (e.CloseReason == CloseReason.UserClosing)
- e.Cancel = true;
- altF4 = false;
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Alt && e.KeyCode == Keys.F4)
- {
- altF4 = true;
- }
- }
- //Блокировка/разблокировка диспетчера задач
- private void CheckBox5_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox5.Checked)
- {
- Microsoft.Win32.RegistryKey regkey;
- string keyValueInt = "1";
- string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
- try
- {
- regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subKey);
- regkey.SetValue("DisableTaskMgr", keyValueInt);
- regkey.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- else
- {
- Microsoft.Win32.RegistryKey RegKeyDel = Microsoft.Win32.Registry.CurrentUser;
- try
- {
- RegKeyDel.DeleteSubKeyTree("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
- RegKeyDel.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- }
- //Предохранители
- private void CheckBox7_CheckedChanged(object sender, EventArgs e)
- {
- button1.Enabled = checkBox7.Checked;
- }
- private void CheckBox8_CheckedChanged(object sender, EventArgs e)
- {
- button2.Enabled = checkBox8.Checked;
- }
- private void CheckBox9_CheckedChanged(object sender, EventArgs e)
- {
- button3.Enabled = checkBox9.Checked;
- }
- //Выключение
- private void Button1_Click(object sender, EventArgs e)
- {
- Halt(false, checkBox14.Checked);
- }
- //Сон
- private void Button2_Click(object sender, EventArgs e)
- {
- Application.SetSuspendState(PowerState.Suspend, checkBox14.Checked, false);
- }
- //Работа с автозагрузкой
- const string name = "OsLR1";
- public bool SetAutorunValue(bool autorun)
- {
- string ExePath = Application.ExecutablePath;
- RegistryKey reg;
- reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
- try
- {
- if (autorun) reg.SetValue(name, ExePath);
- else reg.DeleteValue(name);
- reg.Close();
- }
- catch
- {
- MessageBox.Show("Ошибка.");
- return false;
- }
- if (autorun) MessageBox.Show("Программа добавлена в автозагрузку.");
- else MessageBox.Show("Программа удалена из автозагрузки.");
- return true;
- }
- //Добавить в автозагрузку
- private void Button5_Click(object sender, EventArgs e)
- {
- SetAutorunValue(true);
- }
- //Убрать из автозагрузки
- private void Button6_Click(object sender, EventArgs e)
- {
- SetAutorunValue(false);
- }
- //Перезагрузка
- private void Button3_Click(object sender, EventArgs e)
- {
- Halt(true, checkBox14.Checked);
- }
- //Скрыть из панели задач
- private void CheckBox6_CheckedChanged(object sender, EventArgs e)
- {
- ShowInTaskbar = !checkBox6.Checked;
- }
- //Блокировка Alt + Space
- private void CheckBox12_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox12.Checked)
- {
- m_callback_2 = LowLevelKeyboardHookProc_alt_space;
- m_hHook_2 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_2, GetModuleHandle(IntPtr.Zero), 0);
- }
- else
- {
- UnhookWindowsHookEx(m_hHook_2);
- }
- }
- //Блокировка Alt + Tab
- private void CheckBox4_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox4.Checked)
- {
- m_callback_1 = LowLevelKeyboardHookProc_alt_tab;
- m_hHook_1 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_1, GetModuleHandle(IntPtr.Zero), 0);
- }
- else
- {
- UnhookWindowsHookEx(m_hHook_1);
- }
- }
- //Кнопка очистить листбокс
- private void button9_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- }
- //Блокировка WinKey
- private void CheckBox13_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox13.Checked)
- {
- m_callback = LowLevelKeyboardHookProc_win;
- m_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback, GetModuleHandle(IntPtr.Zero), 0);
- }
- else
- {
- UnhookWindowsHookEx(m_hHook);
- }
- }
- //Скрытие программы из диспетчера задач
- private void checkBox11_CheckedChanged(object sender, EventArgs e)
- {
- }
- //Скрытие процесса из диспетчера задач
- private void checkBox15_CheckedChanged(object sender, EventArgs e)
- {
- }
- //Скрытие окна программы
- [DllImport("User32.dll")]
- private static extern short GetAsyncKeyState(System.Int32 vKey);
- private void button8_Click(object sender, EventArgs e)
- {
- Hide();
- timer1.Enabled = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment