EaseFilter Demo Project
MessageBoxHelper.cs
Go to the documentation of this file.
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Windows.Forms;
4 
5 namespace EaseFilter.CommonObjects
6 {
7  public static class MessageBoxHelper
8  {
9  // use example
10  // MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
11 
12  public static void PrepToCenterMessageBoxOnForm(Form form)
13  {
14  MessageBoxCenterHelper helper = new MessageBoxCenterHelper();
15  helper.Prep(form);
16  }
17 
18  private class MessageBoxCenterHelper
19  {
20  private int messageHook;
21  private IntPtr parentFormHandle;
22 
23  public void Prep(Form form)
24  {
25  NativeMethods.CenterMessageCallBackDelegate callBackDelegate = new NativeMethods.CenterMessageCallBackDelegate(CenterMessageCallBack);
26  GCHandle.Alloc(callBackDelegate);
27 
28  parentFormHandle = form.Handle;
29  messageHook = NativeMethods.SetWindowsHookEx(5, callBackDelegate, new IntPtr(NativeMethods.GetWindowLong(parentFormHandle, -6)), NativeMethods.GetCurrentThreadId()).ToInt32();
30  }
31 
32  private int CenterMessageCallBack(int message, int wParam, int lParam)
33  {
34  NativeMethods.RECT formRect;
35  NativeMethods.RECT messageBoxRect;
36  int xPos;
37  int yPos;
38 
39  if (message == 5)
40  {
41  NativeMethods.GetWindowRect(parentFormHandle, out formRect);
42  NativeMethods.GetWindowRect(new IntPtr(wParam), out messageBoxRect);
43 
44  xPos = (int)((formRect.Left + (formRect.Right - formRect.Left) / 2) - ((messageBoxRect.Right - messageBoxRect.Left) / 2));
45  yPos = (int)((formRect.Top + (formRect.Bottom - formRect.Top) / 2) - ((messageBoxRect.Bottom - messageBoxRect.Top) / 2));
46 
47  NativeMethods.SetWindowPos(wParam, 0, xPos, yPos, 0, 0, 0x1 | 0x4 | 0x10);
48  NativeMethods.UnhookWindowsHookEx(messageHook);
49  }
50 
51  return 0;
52  }
53  }
54 
55  public static class NativeMethods
56  {
57  internal struct RECT
58  {
59  public int Left;
60  public int Top;
61  public int Right;
62  public int Bottom;
63  }
64 
65  internal delegate int CenterMessageCallBackDelegate(int message, int wParam, int lParam);
66 
67  [DllImport("user32.dll")]
68  [return: MarshalAs(UnmanagedType.Bool)]
69  internal static extern bool UnhookWindowsHookEx(int hhk);
70 
71  [DllImport("user32.dll", SetLastError = true)]
72  internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
73 
74  [DllImport("kernel32.dll")]
75  internal static extern int GetCurrentThreadId();
76 
77  [DllImport("user32.dll", SetLastError = true)]
78  internal static extern IntPtr SetWindowsHookEx(int hook, CenterMessageCallBackDelegate callback, IntPtr hMod, int dwThreadId);
79 
80  [DllImport("user32.dll")]
81  [return: MarshalAs(UnmanagedType.Bool)]
82  internal static extern bool SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
83 
84  [DllImport("user32.dll")]
85  [return: MarshalAs(UnmanagedType.Bool)]
86  internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
87  }
88  }
89 }

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com