/MsgBoxCheck/MsgBoxCheck.cs

# · C# · 211 lines · 170 code · 31 blank · 10 comment · 13 complexity · 9a72e5e3ecb19544944fe384ed3a668d MD5 · raw file

  1. using System;
  2. using System.Text;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Runtime.InteropServices;
  6. using MsdnMag; // For LocalCbtHook
  7. using Microsoft.Win32; // For RegKey
  8. namespace MsgBoxCheck
  9. {
  10. public class MessageBox
  11. {
  12. protected LocalCbtHook m_cbt;
  13. protected IntPtr m_hwnd = IntPtr.Zero;
  14. protected IntPtr m_hwndBtn = IntPtr.Zero;
  15. protected bool m_bInit = false;
  16. protected bool m_bCheck = false;
  17. protected string m_strCheck;
  18. public MessageBox()
  19. {
  20. m_cbt = new LocalCbtHook();
  21. m_cbt.WindowCreated += new LocalCbtHook.CbtEventHandler(WndCreated);
  22. m_cbt.WindowDestroyed += new LocalCbtHook.CbtEventHandler(WndDestroyed);
  23. m_cbt.WindowActivated += new LocalCbtHook.CbtEventHandler(WndActivated);
  24. }
  25. public DialogResult Show(string strKey, string strValue, DialogResult dr, string strCheck, string strText, string strTitle, MessageBoxButtons buttons, MessageBoxIcon icon)
  26. {
  27. RegistryKey regKey = Registry.CurrentUser.CreateSubKey(strKey);
  28. try
  29. {
  30. if(Convert.ToBoolean(regKey.GetValue(strValue,false)))
  31. return dr;
  32. }
  33. catch
  34. {
  35. // No processing needed...the convert might throw an exception,
  36. // but if so we proceed as if the value was false.
  37. }
  38. m_strCheck = strCheck;
  39. m_cbt.Install();
  40. dr = System.Windows.Forms.MessageBox.Show(strText, strTitle, buttons, icon);
  41. m_cbt.Uninstall();
  42. regKey.SetValue(strValue,m_bCheck);
  43. return dr;
  44. }
  45. public DialogResult Show(string strKey, string strValue, DialogResult dr, string strCheck, string strText, string strTitle, MessageBoxButtons buttons)
  46. {
  47. return Show(strKey, strValue, dr, strCheck, strText, strTitle, buttons, MessageBoxIcon.None);
  48. }
  49. public DialogResult Show(string strKey, string strValue, DialogResult dr, string strCheck, string strText, string strTitle)
  50. {
  51. return Show(strKey, strValue, dr, strCheck, strText, strTitle, MessageBoxButtons.OK, MessageBoxIcon.None);
  52. }
  53. public DialogResult Show(string strKey, string strValue, DialogResult dr, string strCheck, string strText)
  54. {
  55. return Show(strKey, strValue, dr, strCheck, strText, "", MessageBoxButtons.OK, MessageBoxIcon.None);
  56. }
  57. private void WndCreated(object sender, CbtEventArgs e)
  58. {
  59. if (e.IsDialogWindow)
  60. {
  61. m_bInit = false;
  62. m_hwnd = e.Handle;
  63. }
  64. }
  65. private void WndDestroyed(object sender, CbtEventArgs e)
  66. {
  67. if (e.Handle == m_hwnd)
  68. {
  69. m_bInit = false;
  70. m_hwnd = IntPtr.Zero;
  71. if(BST_CHECKED == (int)SendMessage(m_hwndBtn,BM_GETCHECK,IntPtr.Zero,IntPtr.Zero))
  72. m_bCheck = true;
  73. }
  74. }
  75. private void WndActivated(object sender, CbtEventArgs e)
  76. {
  77. if (m_hwnd != e.Handle)
  78. return;
  79. // Not the first time
  80. if (m_bInit)
  81. return;
  82. else
  83. m_bInit = true;
  84. // Get the current font, either from the static text window
  85. // or the message box itself
  86. IntPtr hFont;
  87. IntPtr hwndText = GetDlgItem(m_hwnd, 0xFFFF);
  88. if(hwndText != IntPtr.Zero)
  89. hFont = SendMessage(hwndText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
  90. else
  91. hFont = SendMessage(m_hwnd, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
  92. Font fCur = Font.FromHfont(hFont);
  93. // Get the x coordinate for the check box. Align it with the icon if possible,
  94. // or one character height in
  95. int x = 0;
  96. IntPtr hwndIcon = GetDlgItem(m_hwnd, 0x0014);
  97. if(hwndIcon != IntPtr.Zero)
  98. {
  99. RECT rcIcon = new RECT();
  100. GetWindowRect(hwndIcon, rcIcon);
  101. POINT pt = new POINT();
  102. pt.x = rcIcon.left;
  103. pt.y = rcIcon.top;
  104. ScreenToClient(m_hwnd, pt);
  105. x = pt.x;
  106. }
  107. else
  108. x = (int)fCur.GetHeight();
  109. // Get the y coordinate for the check box, which is the bottom of the
  110. // current message box client area
  111. RECT rc = new RECT();
  112. GetClientRect(m_hwnd, rc);
  113. int y = rc.bottom - rc.top;
  114. // Resize the message box with room for the check box
  115. GetWindowRect(m_hwnd, rc);
  116. MoveWindow(m_hwnd,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top + (int)fCur.GetHeight()*2,true);
  117. m_hwndBtn = CreateWindowEx(0, "button", m_strCheck, BS_AUTOCHECKBOX|WS_CHILD|WS_VISIBLE|WS_TABSTOP,
  118. x, y , rc.right-rc.left-x, (int)fCur.GetHeight(),
  119. m_hwnd, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  120. SendMessage(m_hwndBtn, WM_SETFONT, hFont, new IntPtr(1));
  121. }
  122. #region Win32 Imports
  123. private const int WS_VISIBLE = 0x10000000;
  124. private const int WS_CHILD = 0x40000000;
  125. private const int WS_TABSTOP = 0x00010000;
  126. private const int WM_SETFONT = 0x00000030;
  127. private const int WM_GETFONT = 0x00000031;
  128. private const int BS_AUTOCHECKBOX = 0x00000003;
  129. private const int BM_GETCHECK = 0x00F0;
  130. private const int BST_CHECKED = 0x0001;
  131. [DllImport("user32.dll")]
  132. protected static extern void DestroyWindow(IntPtr hwnd);
  133. [DllImport("user32.dll")]
  134. protected static extern IntPtr GetDlgItem(IntPtr hwnd, int id);
  135. [DllImport("user32.dll")]
  136. protected static extern int GetWindowRect(IntPtr hwnd, RECT rc);
  137. [DllImport("user32.dll")]
  138. protected static extern int GetClientRect(IntPtr hwnd, RECT rc);
  139. [DllImport("user32.dll")]
  140. protected static extern void MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
  141. [DllImport("user32.dll")]
  142. protected static extern int ScreenToClient(IntPtr hwnd, POINT pt);
  143. [DllImport("user32.dll", EntryPoint="MessageBox")]
  144. protected static extern int _MessageBox(IntPtr hwnd, string text, string caption,
  145. int options);
  146. [DllImport("user32.dll")]
  147. protected static extern IntPtr SendMessage(IntPtr hwnd,
  148. int msg, IntPtr wParam, IntPtr lParam);
  149. [DllImport("user32.dll")]
  150. protected static extern IntPtr CreateWindowEx(
  151. int dwExStyle, // extended window style
  152. string lpClassName, // registered class name
  153. string lpWindowName, // window name
  154. int dwStyle, // window style
  155. int x, // horizontal position of window
  156. int y, // vertical position of window
  157. int nWidth, // window width
  158. int nHeight, // window height
  159. IntPtr hWndParent, // handle to parent or owner window
  160. IntPtr hMenu, // menu handle or child identifier
  161. IntPtr hInstance, // handle to application instance
  162. IntPtr lpParam // window-creation data
  163. );
  164. [StructLayout(LayoutKind.Sequential)]
  165. public class POINT
  166. {
  167. public int x;
  168. public int y;
  169. }
  170. [StructLayout(LayoutKind.Sequential)]
  171. public class RECT
  172. {
  173. public int left;
  174. public int top;
  175. public int right;
  176. public int bottom;
  177. }
  178. #endregion
  179. }
  180. }