public WindowSubclass(HWND hwnd)
{
if (!IsCorrectThread(hwnd)) {
throw new InvalidOperationException("May not hook a window created by a different thread.");
}
_hwnd = hwnd;
_wndproc = WndProcStub;
_wndprocPtr = Marshal.GetFunctionPointerForDelegate(_wndproc);
// Because our window proc is an instance method, it is connected
// to this instance of WindowSubclass, where we can store state.
// We do not need to store any extra state in native memory.
NativeMethods.SetWindowSubclass(hwnd, _wndproc, IntPtr.Zero, IntPtr.Zero);
}