Advertisement
parabola949

Lock Program Full Screen

Jan 17th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. //MainWindow.xaml
  2. <Window x:Class="YOUR_NAMESPACE.MainWindow"
  3.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.         Title="MainWindow" ResizeMode="NoResize" AllowsTransparency="True" WindowStartupLocation="Manual" WindowState="Normal" WindowStyle="None" Background="#FF000000" Topmost="True" Left="0" Top="0" Width="1000" Height="500" LostFocus="Window_LostFocus_1" Cursor="No" MouseLeave="Window_MouseLeave_1">
  6.     <Grid>
  7.        
  8.     </Grid>
  9. </Window>
  10.  
  11. //MainWindow.xaml.cs
  12. using System;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Runtime.InteropServices;
  16. using System.Windows.Forms;
  17.  
  18. namespace YOUR_NAMESPACE
  19. {
  20.    
  21.     public partial class MainWindow
  22.     {
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.             Topmost = true;
  27.             Rectangle totalSize = Screen.AllScreens.Length <= 1 ? Screen.PrimaryScreen.Bounds : Screen.AllScreens.Aggregate(System.Drawing.Rectangle.Empty, (current, s) => System.Drawing.Rectangle.Union(current, s.Bounds));
  28.             Width = totalSize.Width / 2;
  29.             Height = totalSize.Height;
  30.             Left = totalSize.Left;
  31.             Top = totalSize.Top;
  32.             (new Thread(KillTaskMgr) {IsBackground = true}).Start();
  33.             ptrHook = SetWindowsHookEx(13, CaptureKey, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
  34.             //System.Windows.Forms.Cursor.Hide();
  35.  
  36.         }
  37.     public void KillTaskMgr()
  38.         {
  39.             while (true)
  40.             {
  41.                 try
  42.                 {
  43.                     Process.GetProcessesByName("taskmgr")[0].Kill();
  44.                 }
  45.                 catch
  46.                 {
  47.                 }
  48.             }
  49.         }
  50.  
  51.         #region Code to Disable WinKey, Alt+Tab, Ctrl+Esc Starts Here
  52.  
  53.  
  54.         // Structure contain information about low-level keyboard input event
  55.         [StructLayout(LayoutKind.Sequential)]
  56.         private struct KBDLLHOOKSTRUCT
  57.         {
  58.             public readonly Keys key;
  59.             private readonly int scanCode;
  60.             public readonly int flags;
  61.             private readonly int time;
  62.             private readonly IntPtr extra;
  63.         }
  64.         //System level functions to be used for hook and unhook keyboard input  
  65.         private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  66.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  67.         private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
  68. /*
  69.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  70.         private static extern bool UnhookWindowsHookEx(IntPtr hook);
  71. */
  72.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  73.         private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
  74.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  75.         private static extern IntPtr GetModuleHandle(string name);
  76. /*
  77.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  78.         private static extern short GetAsyncKeyState(Keys key);
  79. */
  80.         //Declaring Global objects    
  81.         private readonly IntPtr ptrHook;
  82.         private readonly LowLevelKeyboardProc objKeyboardProcess;
  83.  
  84.         private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
  85.         {
  86.             if (nCode >= 0)
  87.             {
  88.                 if (nCode >= 0)
  89.                 {
  90.                     KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));
  91.                     if (objKeyInfo.key == Keys.End)
  92.                         Environment.Exit(0);
  93.                 }
  94.             //return CallNextHookEx(ptrHook, nCode, wp, lp);
  95.             return (IntPtr) 1;
  96.             }
  97.     }
  98.        
  99.  
  100.     private void Window_LostFocus_1(object sender, System.Windows.RoutedEventArgs e)
  101.         {
  102.             Focus();
  103.             Topmost = true;
  104.         }
  105.  
  106.         private void Window_MouseLeave_1(object sender, System.Windows.Input.MouseEventArgs e)
  107.         {
  108.             Focus();
  109.             Topmost = true;
  110.         }
  111.         #endregion
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement