andrew4582

LockWindow

Mar 19th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace System.Windows.Forms {
  8.     /// <summary>
  9.     /// Locks Window's Update with the LockWindowUpdate winapi
  10.     /// </summary>
  11.     public class LockWindow:IDisposable {
  12.  
  13.         [System.Runtime.InteropServices.DllImport("User32")]
  14.         public static extern bool LockWindowUpdate(IntPtr hWndLock);
  15.  
  16.         #region static
  17.         public static bool LockWindowUpdate(IWin32Window window) {
  18.             return LockWindowUpdate(window.Handle);
  19.         }
  20.         public static bool LockWindowUpdate() {
  21.             return LockWindowUpdate(IntPtr.Zero);
  22.         }
  23.         public static LockWindow Lock(IntPtr winHandle) {
  24.             return new LockWindow(winHandle);
  25.         }
  26.         public static LockWindow Lock(IntPtr winHandle,bool lockwindow) {
  27.             return new LockWindow(winHandle,lockwindow);
  28.         }
  29.         public static LockWindow Lock(IWin32Window window) {
  30.             return new LockWindow(window);
  31.         }
  32.         #endregion
  33.  
  34.         readonly IntPtr _handle;
  35.         bool _windowlocked;
  36.  
  37.         #region ctr
  38.         public LockWindow(IntPtr winHandle)
  39.             : this(winHandle,true) {
  40.         }
  41.         public LockWindow(IntPtr winHandle,bool lockwindow) {
  42.             _windowlocked = false;
  43.             _handle = winHandle;
  44.             if(lockwindow)
  45.                 LockUpdate();
  46.         }
  47.         public LockWindow(IWin32Window window)
  48.             : this(window.Handle) {
  49.         }
  50.         #endregion
  51.  
  52.         public bool LockUpdate() {
  53.             if(!_windowlocked && _handle != IntPtr.Zero)
  54.                 _windowlocked = LockWindowUpdate(_handle);
  55.             return _windowlocked;
  56.         }
  57.         public void UnLockUpdate() {
  58.             if(_windowlocked && _handle != IntPtr.Zero)
  59.                 LockWindowUpdate(IntPtr.Zero);
  60.         }
  61.  
  62.         #region IDisposable Pattern
  63.  
  64.         protected bool IsDisposed { get; private set; }
  65.         public virtual void Dispose() {
  66.             if(IsDisposed)
  67.                 throw new ObjectDisposedException(this.GetType().Name);
  68.  
  69.             try {
  70.                 this.Dispose(true);
  71.             }
  72.             finally {
  73.                 GC.SuppressFinalize(this);
  74.             }
  75.         }
  76.         protected virtual void Dispose(bool disposing) {
  77.             try {
  78.                 if(!IsDisposed) {
  79.                     if(disposing) {
  80.                         UnLockUpdate();
  81.                     }
  82.                 }
  83.             }
  84.             finally {
  85.                 this.IsDisposed = true;
  86.             }
  87.         }
  88.         #endregion
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment