PhotoShaman

Лаба1.Жесть

Oct 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.49 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4. using System.Diagnostics;
  5. using Microsoft.Win32;
  6.  
  7. namespace OsLR1
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         #region Хуки и прочее(Для блока Alt + Space, Alt + Tab и WinKey)
  12.  
  13.         private const int WH_KEYBOARD_LL = 13;//Keyboard hook;
  14.  
  15.         private delegate IntPtr LowLevelKeyboardProcDelegate(int nCode, IntPtr wParam, IntPtr lParam);
  16.  
  17.         private LowLevelKeyboardProcDelegate m_callback, m_callback_1, m_callback_2;
  18.  
  19.         private IntPtr m_hHook, m_hHook_1, m_hHook_2;
  20.  
  21.         [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
  22.         private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
  23.  
  24.         [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
  25.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  26.  
  27.         [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
  28.         private struct KBDLLHOOKSTRUCT
  29.         {
  30.             public Keys key;
  31.         }
  32.  
  33.         [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
  34.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  35.  
  36.         [System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true)]
  37.         private static extern IntPtr GetModuleHandle(IntPtr lpModuleName);
  38.  
  39.         //Захват alt+tab
  40.         public IntPtr LowLevelKeyboardHookProc_alt_tab(int nCode, IntPtr wParam, IntPtr lParam)
  41.         {
  42.             if (nCode >= 0)
  43.             {
  44.                 KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
  45.                 if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Tab)
  46.                 {
  47.                     return (IntPtr)1;
  48.                 }
  49.             }
  50.             return CallNextHookEx(m_hHook, nCode, wParam, lParam);
  51.         }
  52.  
  53.         //Захват alt+space
  54.         public IntPtr LowLevelKeyboardHookProc_alt_space(int nCode, IntPtr wParam, IntPtr lParam)
  55.         {
  56.             if (nCode >= 0)
  57.             {
  58.                 KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
  59.                 if (objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.Space)
  60.                 {
  61.                     return (IntPtr)1;
  62.                 }
  63.             }
  64.             return CallNextHookEx(m_hHook_2, nCode, wParam, lParam);
  65.         }
  66.  
  67.         //Захват winkey
  68.         public IntPtr LowLevelKeyboardHookProc_win(int nCode, IntPtr wParam, IntPtr lParam)
  69.         {
  70.             if (nCode >= 0)
  71.             {
  72.                 KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
  73.                 if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin)
  74.                 {
  75.                     return (IntPtr)1;
  76.                 }
  77.             }
  78.             return CallNextHookEx(m_hHook_1, nCode, wParam, lParam);
  79.         }
  80.         #endregion
  81.  
  82.         #region Выключение, Перезагрузка
  83.  
  84.         [DllImport("advapi32.dll", EntryPoint = "InitiateSystemShutdownEx")]
  85.         static extern int InitiateSystemShutdown(string lpMachineName, string lpMessage, int dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown);
  86.  
  87.         [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  88.         internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
  89.         ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  90.  
  91.         [DllImport("kernel32.dll", ExactSpelling = true)]
  92.         internal static extern IntPtr GetCurrentProcess();
  93.  
  94.         [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  95.         internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  96.  
  97.         [DllImport("advapi32.dll", SetLastError = true)]
  98.         internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  99.  
  100.         [DllImport("user32.dll", EntryPoint = "LockWorkStation")]
  101.         static extern bool LockWorkStation();
  102.  
  103.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  104.         internal struct TokPriv1Luid
  105.         {
  106.             public int Count;
  107.             public long Luid;
  108.             public int Attr;
  109.         }
  110.  
  111.         internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  112.         internal const int TOKEN_QUERY = 0x00000008;
  113.         internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  114.         internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
  115.  
  116.         private void SetPriv()
  117.         {
  118.             TokPriv1Luid tkp;
  119.             IntPtr htok = IntPtr.Zero;
  120.             if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok))
  121.             {
  122.                 tkp.Count = 1;
  123.                 tkp.Attr = SE_PRIVILEGE_ENABLED;
  124.                 tkp.Luid = 0;
  125.                 LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tkp.Luid);
  126.                 AdjustTokenPrivileges(htok, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero);
  127.             }
  128.         }
  129.  
  130.         public int Halt(bool RSh, bool Force)
  131.         {
  132.             SetPriv();
  133.             return InitiateSystemShutdown(null, null, 0, Force, RSh);
  134.         }
  135.         #endregion
  136.  
  137.         FormEnabled fe;
  138.         public Form1()
  139.         {
  140.             InitializeComponent();
  141.             fe = new FormEnabled();
  142.            
  143.             timer1.Tick += delegate
  144.             {
  145.                 foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
  146.                 {
  147.                     if (GetAsyncKeyState(i) == -32767)
  148.                     {
  149.                         if (Enum.GetName(typeof(Keys), i) == "Home")
  150.                         {
  151.                             Show();
  152.                             timer1.Enabled = false;
  153.                         }
  154.                     }
  155.                 }
  156.             };
  157.         }
  158.  
  159.         //Блокровка кнопки Закрыть
  160.         class FormEnabled
  161.         {
  162.             private const int SC_CLOSE = 0xF060;
  163.             private const int MF_GRAYED = 0x1;
  164.             internal const int MF_ENABLED = 0x0;
  165.             [DllImport("user32.dll")]
  166.             private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  167.             [DllImport("user32.dll")]
  168.             private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);
  169.             [DllImport("user32.dll")]
  170.             public static extern int RemoveMenu(int systemMenu, int itemPosition, int flag);
  171.             [DllImport("user32.dll")]
  172.             public static extern int GetMenuItemCount(int systemMenu);
  173.             [DllImport("user32.dll")]
  174.             public static extern int DrawMenuBar(IntPtr currentWindow);
  175.             public void FormCloseEnabled(Form form, bool enabled)
  176.             {
  177.                 MethodInvoker method = delegate
  178.                 {
  179.                     IntPtr menu;
  180.                     int itemCount;
  181.  
  182.                     EnableMenuItem(GetSystemMenu(form.Handle, false), SC_CLOSE, enabled ? MF_ENABLED : MF_GRAYED);
  183.                     menu = GetSystemMenu(form.Handle, false);
  184.                     itemCount = GetMenuItemCount(menu.ToInt32());
  185.                     RemoveMenu(menu.ToInt32(), itemCount - 1, enabled ? 1 : 2);
  186.                     DrawMenuBar(form.Handle);
  187.                 };
  188.  
  189.                 if (form.InvokeRequired)
  190.                     form.BeginInvoke(method);
  191.                 else
  192.                     method.Invoke();
  193.             }
  194.         }
  195.         private void CheckBox1_CheckedChanged(object sender, EventArgs e)
  196.         {
  197.             fe.FormCloseEnabled(this, !checkBox1.Checked);
  198.         }
  199.         //Блокровка кнопки Восстановить
  200.         private void CheckBox2_CheckedChanged(object sender, EventArgs e)
  201.         {
  202.             MaximizeBox = !checkBox2.Checked;
  203.             fe.FormCloseEnabled(this, !checkBox1.Checked);
  204.         }
  205.         //Блокировка кнопки Скрыть
  206.         private void CheckBox3_CheckedChanged(object sender, EventArgs e)
  207.         {
  208.             MinimizeBox = !checkBox3.Checked;
  209.             fe.FormCloseEnabled(this, !checkBox1.Checked);
  210.         }
  211.  
  212.         //Показать список работающих программ
  213.         private void Button4_Click(object sender, EventArgs e)
  214.         {
  215.             listBox1.Items.Clear();
  216.             foreach (Process process in Process.GetProcesses())
  217.             {
  218.                 if (!string.IsNullOrEmpty(process.MainWindowTitle))
  219.                 {
  220.                     listBox1.Items.Add(process.MainWindowTitle);
  221.                 }
  222.             }
  223.         }
  224.  
  225.         //Смена обоев
  226.         private void Button7_Click(object sender, EventArgs e)
  227.         {
  228.             OpenFileDialog openFile = new OpenFileDialog()
  229.             {
  230.                 Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.JPEG)|*.PNG;*.BMP;*.JPG;*.JPEG"
  231.             };
  232.             if (openFile.ShowDialog() == DialogResult.OK)
  233.             {
  234.                 WinAPI.SystemParametersInfo(WinAPI.SPI_SETDESKWALLPAPER, 1,
  235.                     openFile.FileName, WinAPI.SPIF_UPDATEINIFILE | WinAPI.SPIF_SENDWININICHANGE);
  236.             }
  237.         }
  238.  
  239.         class WinAPI
  240.         {
  241.             [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  242.             public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
  243.             public const int SPI_SETDESKWALLPAPER = 20;
  244.             public const int SPIF_UPDATEINIFILE = 0x1;
  245.             public const int SPIF_SENDWININICHANGE = 0x2;
  246.         }
  247.  
  248.         //Блокировка Alt + F4
  249.         private bool altF4 = false, altF4Block = false;
  250.         private void CheckBox10_CheckedChanged(object sender, EventArgs e)
  251.         {
  252.             altF4Block = checkBox10.Checked;
  253.         }
  254.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  255.         {
  256.             if (altF4 && altF4Block)
  257.             {
  258.                 if (e.CloseReason == CloseReason.UserClosing)
  259.                     e.Cancel = true;
  260.                 altF4 = false;
  261.             }
  262.         }
  263.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  264.         {
  265.             if (e.Alt && e.KeyCode == Keys.F4)
  266.             {
  267.                 altF4 = true;
  268.             }
  269.         }
  270.  
  271.         //Блокировка/разблокировка диспетчера задач
  272.         private void CheckBox5_CheckedChanged(object sender, EventArgs e)
  273.         {
  274.             if (checkBox5.Checked)
  275.             {
  276.                 Microsoft.Win32.RegistryKey regkey;
  277.                 string keyValueInt = "1";
  278.                 string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  279.  
  280.                 try
  281.                 {
  282.                     regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subKey);
  283.                     regkey.SetValue("DisableTaskMgr", keyValueInt);
  284.                     regkey.Close();
  285.                 }
  286.                 catch (Exception ex)
  287.                 {
  288.                     MessageBox.Show(ex.ToString());
  289.                 }
  290.             }
  291.             else
  292.             {
  293.                 Microsoft.Win32.RegistryKey RegKeyDel = Microsoft.Win32.Registry.CurrentUser;
  294.                 try
  295.                 {
  296.                     RegKeyDel.DeleteSubKeyTree("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  297.                     RegKeyDel.Close();
  298.                 }
  299.                 catch (Exception ex)
  300.                 {
  301.                     MessageBox.Show(ex.ToString());
  302.                 }
  303.             }
  304.         }
  305.  
  306.         //Предохранители
  307.         private void CheckBox7_CheckedChanged(object sender, EventArgs e)
  308.         {
  309.             button1.Enabled = checkBox7.Checked;
  310.         }
  311.         private void CheckBox8_CheckedChanged(object sender, EventArgs e)
  312.         {
  313.             button2.Enabled = checkBox8.Checked;
  314.         }
  315.         private void CheckBox9_CheckedChanged(object sender, EventArgs e)
  316.         {
  317.             button3.Enabled = checkBox9.Checked;
  318.         }
  319.  
  320.         //Выключение
  321.         private void Button1_Click(object sender, EventArgs e)
  322.         {
  323.             Halt(false, checkBox14.Checked);
  324.         }
  325.  
  326.         //Сон
  327.         private void Button2_Click(object sender, EventArgs e)
  328.         {
  329.             Application.SetSuspendState(PowerState.Suspend, checkBox14.Checked, false);
  330.         }
  331.  
  332.         //Работа с автозагрузкой
  333.         const string name = "OsLR1";
  334.  
  335.         public bool SetAutorunValue(bool autorun)
  336.         {
  337.             string ExePath = Application.ExecutablePath;
  338.             RegistryKey reg;
  339.             reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
  340.             try
  341.             {
  342.                 if (autorun) reg.SetValue(name, ExePath);
  343.                 else reg.DeleteValue(name);
  344.                 reg.Close();
  345.             }
  346.             catch
  347.             {
  348.                 MessageBox.Show("Ошибка.");
  349.                 return false;
  350.             }
  351.             if (autorun) MessageBox.Show("Программа добавлена в автозагрузку.");
  352.             else MessageBox.Show("Программа удалена из автозагрузки.");
  353.             return true;
  354.         }
  355.  
  356.         //Добавить в автозагрузку
  357.         private void Button5_Click(object sender, EventArgs e)
  358.         {
  359.             SetAutorunValue(true);
  360.         }
  361.  
  362.         //Убрать из автозагрузки
  363.         private void Button6_Click(object sender, EventArgs e)
  364.         {
  365.             SetAutorunValue(false);
  366.         }
  367.  
  368.         //Перезагрузка
  369.         private void Button3_Click(object sender, EventArgs e)
  370.         {
  371.             Halt(true, checkBox14.Checked);
  372.         }
  373.  
  374.         //Скрыть из панели задач
  375.         private void CheckBox6_CheckedChanged(object sender, EventArgs e)
  376.         {
  377.             ShowInTaskbar = !checkBox6.Checked;
  378.         }
  379.  
  380.         //Блокировка Alt + Space
  381.         private void CheckBox12_CheckedChanged(object sender, EventArgs e)
  382.         {
  383.             if (checkBox12.Checked)
  384.             {
  385.                 m_callback_2 = LowLevelKeyboardHookProc_alt_space;
  386.                 m_hHook_2 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_2, GetModuleHandle(IntPtr.Zero), 0);
  387.             }
  388.             else
  389.             {
  390.                 UnhookWindowsHookEx(m_hHook_2);
  391.             }
  392.         }
  393.  
  394.         //Блокировка Alt + Tab
  395.         private void CheckBox4_CheckedChanged(object sender, EventArgs e)
  396.         {
  397.             if (checkBox4.Checked)
  398.             {
  399.                 m_callback_1 = LowLevelKeyboardHookProc_alt_tab;
  400.                 m_hHook_1 = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback_1, GetModuleHandle(IntPtr.Zero), 0);
  401.             }
  402.             else
  403.             {
  404.                 UnhookWindowsHookEx(m_hHook_1);
  405.             }
  406.         }
  407.  
  408.         //Кнопка очистить листбокс
  409.         private void button9_Click(object sender, EventArgs e)
  410.         {
  411.             listBox1.Items.Clear();
  412.         }
  413.  
  414.         //Блокировка WinKey
  415.         private void CheckBox13_CheckedChanged(object sender, EventArgs e)
  416.         {
  417.             if (checkBox13.Checked)
  418.             {
  419.                 m_callback = LowLevelKeyboardHookProc_win;
  420.                 m_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, m_callback, GetModuleHandle(IntPtr.Zero), 0);
  421.             }
  422.             else
  423.             {
  424.                 UnhookWindowsHookEx(m_hHook);
  425.             }
  426.         }
  427.  
  428.         //Скрытие программы из диспетчера задач
  429.         private void checkBox11_CheckedChanged(object sender, EventArgs e)
  430.         {
  431.  
  432.         }
  433.  
  434.         //Скрытие процесса из диспетчера задач
  435.         private void checkBox15_CheckedChanged(object sender, EventArgs e)
  436.         {
  437.  
  438.         }
  439.  
  440.         //Скрытие окна программы
  441.         [DllImport("User32.dll")]
  442.         private static extern short GetAsyncKeyState(System.Int32 vKey);
  443.         private void button8_Click(object sender, EventArgs e)
  444.         {
  445.             Hide();
  446.             timer1.Enabled = true;
  447.         }
  448.     }
  449. }
Advertisement
Add Comment
Please, Sign In to add comment