示例#1
0
            public IntPtr Callback(IntPtr hWnd, User32.WindowMessage msg, IntPtr wparam, IntPtr lparam)
            {
                Debug.Assert(hWnd != IntPtr.Zero, "Windows called us with an HWND of 0");

                // Set the window procedure to the default window procedure
                User32.SetWindowLong(hWnd, User32.GWL.WNDPROC, _defaultWindProc);
                _targetWindow.AssignHandle(hWnd);
                return(_targetWindow.Callback(hWnd, msg, wparam, lparam));
            }
            public unsafe IntPtr SendMessage(IHandle sender, User32.WindowMessage message, BOOL state = BOOL.FALSE)
            {
                Info.cbSize = (uint)sizeof(TTOOLINFOW);
                fixed(char *c = Text)
                fixed(void *i = &Info)
                {
                    if (Text != null)
                    {
                        Info.lpszText = c;
                    }
                    IntPtr result = User32.SendMessageW(sender, message, (IntPtr)state, (IntPtr)i);

                    GC.KeepAlive(_handle);
                    return(result);
                }
            }
示例#3
0
        public void ThreadContext_MultipleProcessFiltersProcesses()
        {
            // Test that multiple filters work
            Application.ThreadContext threadContext = new Application.ThreadContext();

            User32.WindowMessage filterId2 = TestMessageId2;
            var mockContext2 = new Mock <IMessageFilter>(MockBehavior.Strict);

            mockContext2.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny))
            .Returns((MessageCallback)((ref Message m) => m.Msg == (int)filterId2));
            threadContext.AddMessageFilter(mockContext2.Object);

            User32.WindowMessage filterId3 = TestMessageId3;
            var mockContext3 = new Mock <IMessageFilter>(MockBehavior.Strict);

            mockContext3.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny))
            .Returns((MessageCallback)((ref Message m) => m.Msg == (int)filterId3));
            threadContext.AddMessageFilter(mockContext3.Object);

            var msg = new User32.MSG
            {
                message = TestMessageId1
            };

            Assert.False(threadContext.PreTranslateMessage(ref msg));

            mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1));
            mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1));

            msg = new User32.MSG
            {
                message = TestMessageId2
            };
            Assert.True(threadContext.PreTranslateMessage(ref msg));

            mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(2));
            mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1));

            msg = new User32.MSG
            {
                message = TestMessageId3
            };
            Assert.True(threadContext.PreTranslateMessage(ref msg));

            mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(3));
            mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(2));
        }
示例#4
0
        public override IntPtr WndProc(
            IntPtr hWnd, User32.WindowMessage msg,
            IntPtr wParam, IntPtr lParam)
        {
            switch (msg)
            {
            case User32.WindowMessage.WM_PAINT:
                _swap?.Present(1, PresentFlags.None);
                break;

            case User32.WindowMessage.WM_CLOSE:
                Environment.Exit(0);
                return(IntPtr.Zero);
            }

            return(base.WndProc(hWnd, msg, wParam, lParam));
        }
示例#5
0
        public void ThreadContext_CorrectProcessFiltersProcesses()
        {
            // Test that a filter with the correct ID returns true
            Application.ThreadContext threadContext = new Application.ThreadContext();

            User32.WindowMessage filterId = TestMessageId2;
            var mockContext = new Mock <IMessageFilter>(MockBehavior.Strict);

            mockContext.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny))
            .Returns((MessageCallback)((ref Message m) => m.Msg == (int)filterId));

            threadContext.AddMessageFilter(mockContext.Object);
            var msg = new User32.MSG
            {
                message = filterId
            };

            Assert.True(threadContext.PreTranslateMessage(ref msg));
            mockContext.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1));
        }
示例#6
0
        public void ThreadContext_WrongProcessFiltersPassesThrough()
        {
            // Test that a filter for the wrong ID returns false, but does get called
            Application.ThreadContext threadContext = new Application.ThreadContext();

            User32.WindowMessage filterId = TestMessageId2;
            var mockContext = new Mock <IMessageFilter>(MockBehavior.Strict);

            mockContext.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny))
            .Returns((MessageCallback)((ref Message m) => m.Msg == (int)filterId));

            threadContext.AddMessageFilter(mockContext.Object);
            var msg = new User32.MSG
            {
                message = TestMessageId1
            };

            Assert.False(threadContext.PreTranslateMessage(ref msg));
            mockContext.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1));
        }
示例#7
0
        /// <summary>
        ///  Window message callback method. Control arrives here when a window
        ///  message is sent to this Window. This method packages the window message
        ///  in a Message object and invokes the wndProc() method. A WM_NCDESTROY
        ///  message automatically causes the releaseHandle() method to be called.
        /// </summary>
        private IntPtr Callback(IntPtr hWnd, User32.WindowMessage msg, IntPtr wparam, IntPtr lparam)
        {
            // Note: if you change this code be sure to change the
            // corresponding code in DebuggableCallback below!

            Message m = Message.Create(hWnd, msg, wparam, lparam);

            try
            {
                if (_weakThisPtr.IsAlive && _weakThisPtr.Target != null)
                {
                    WndProc(ref m);
                }
                else
                {
                    DefWndProc(ref m);
                }
            }
            catch (Exception e)
            {
                if (WndProcShouldBeDebuggable)
                {
                    throw;
                }
                OnThreadException(e);
            }
            finally
            {
                if (msg == User32.WindowMessage.WM_NCDESTROY)
                {
                    ReleaseHandle(handleValid: false);
                }

                if (msg == User32.RegisteredMessage.WM_UIUNSUBCLASS)
                {
                    ReleaseHandle(handleValid: true);
                }
            }

            return(m.Result);
        }
示例#8
0
        private unsafe IntPtr NewWindowProc(IntPtr hWnd, User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam)
        {
            switch (Msg)
            {
            case User32.WindowMessage.WM_SETTINGCHANGE:
            case User32.WindowMessage.WM_THEMECHANGED:
                HRESULT hr = UpdateTitleBarTheme();
                hr.ThrowIfFailed();
                break;

            case User32.WindowMessage.WM_GETMINMAXINFO:
                var   dpi           = User32.GetDpiForWindow(hWnd);
                float scalingFactor = (float)dpi / 96;

                //MINMAXINFO minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(lParam);
                //minMaxInfo.ptMinTrackSize.x = (int)(MinWidth * scalingFactor);
                //minMaxInfo.ptMinTrackSize.y = (int)(MinHeight * scalingFactor);
                //Marshal.StructureToPtr(minMaxInfo, lParam, true);
                break;
            }
            return(CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WindowMessage"/> class.
 /// </summary>
 /// <param name="msg">Window Message ID</param>
 /// <param name="wParam">The wParam value</param>
 /// <param name="lParam">The lparam value</param>
 internal WindowMessage(User32.WindowMessage msg, IntPtr wParam, IntPtr lParam)
 {
     this.Message = msg;
     this.WParam  = wParam;
     this.LParam  = lParam;
 }
示例#10
0
 /// <summary>
 /// This is the replaces Window Procedure which will be used to track all window messages,
 /// and generate events.
 /// </summary>
 /// <param name="hWnd">The window handle.</param>
 /// <param name="msg">Message ID.</param>
 /// <param name="wParam">The wParam value.</param>
 /// <param name="lParam">The lParam value.</param>
 /// <returns>Message specific return value.</returns>
 internal unsafe IntPtr HookProc(IntPtr hWnd, User32.WindowMessage msg, void* wParam, void* lParam)
 {
     this.WindowMessage?.Invoke(this, new WindowMessage(msg, new IntPtr(wParam), new IntPtr(lParam)));
     return User32.DefWindowProc(hWnd, msg, new IntPtr(wParam), new IntPtr(lParam));
 }
示例#11
0
 internal static Message Create(IntPtr hWnd, User32.WindowMessage msg, IntPtr wparam, IntPtr lparam)
 => Create(hWnd, (int)msg, wparam, lparam);
示例#12
0
 static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
示例#13
0
        private void SendNCWinMessage(User32.WindowMessage msg, IntPtr parameter1, IntPtr parameter2)
        {
            Message message = Message.Create(this.Handle, (int)msg, parameter1, parameter2);

            this.WndProc(ref message);
        }
示例#14
0
 public WindowMessage(User32.WindowMessage message, IntPtr wParam, IntPtr lParam)
 {
     Message = message;
     WParam  = wParam;
     LParam  = lParam;
 }
示例#15
0
 public virtual IntPtr WndProc(
     IntPtr hWnd, User32.WindowMessage msg,
     IntPtr wParam, IntPtr lParam)
 {
     return(DefWindowProc(hWnd, msg, wParam, lParam));
 }
示例#16
0
 public static extern IntPtr DefWindowProc(IntPtr hWnd, User32.WindowMessage msg, IntPtr wParam, IntPtr lParam);