Exemplo n.º 1
0
        public WndProcClient()
        {
            Unmanaged.WNDCLASSEX wndClassEx = new Unmanaged.WNDCLASSEX
            {
                cbSize      = Marshal.SizeOf <Unmanaged.WNDCLASSEX>(),
                lpfnWndProc = delegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) {
                    var message = new WindowMessage()
                    {
                        hWnd   = hWnd,
                        Msg    = (WindowsMessage)msg,
                        wParam = wParam,
                        lParam = lParam
                    };

                    MessageReceived?.Invoke(this, message);

                    return(Unmanaged.DefWindowProc(hWnd, msg, wParam, lParam));
                },
                hInstance     = Unmanaged.GetModuleHandle(null),
                lpszClassName = "MessageWindow " + Guid.NewGuid(),
            };

            ushort atom = Unmanaged.RegisterClassEx(ref wndClassEx);

            if (atom == 0)
            {
                Log.Error("Interop.Win32.WndProcClient: atom is null");
                throw new Win32Exception();
            }

            _hwnd = Unmanaged.CreateWindowEx(0, atom, null, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, Unmanaged.GetModuleHandle(null), IntPtr.Zero);

            if (_hwnd == IntPtr.Zero)
            {
                Log.Error("Interop.Win32.WndProcClient: nWnd is null");
                throw new Win32Exception();
            }
        }