SVXX

Closing A Messagebox

May 28th, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. //In the Form class
  2. private void CloseTheBox(string caption) //Closing a messagebox by caption
  3. {
  4.        IntPtr hWnd = CloseMessageBox.FindWindowByCaption(null, caption);
  5.        if (hWnd != IntPtr.Zero)
  6.            CloseMessageBox.SendMessage(new HandleRef(null, hWnd), CloseMessageBox.CloseNumber, IntPtr.Zero, IntPtr.Zero);
  7. }
  8.  
  9. //Class containing the members required.
  10. public class CloseMessageBox
  11. {
  12.     [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  13.     public static extern IntPtr FindWindowByCaption(string lpClassName, string lpWindowName);
  14.     [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  15.     public static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
  16.     const UInt32 WM_CLOSE = 0x0010;
  17.     public static UInt32 CloseNumber
  18.     {
  19.         get
  20.         {
  21.             return WM_CLOSE;
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment