Exemple #1
0
 /// <summary>
 /// Sends the specified message to a window or windows. The SendMessage function calls the
 /// window procedure for the specified window and does not return until the window procedure
 /// has processed the message.
 /// </summary>
 /// <param name="hwnd">The window handle of the sending window</param>
 /// <param name="windowMessage">The message to be sent.</param>
 /// <param name="wParam">Additional message-specific information.</param>
 /// <param name="lParam">Additional message-specific information.</param>
 public static void SendMessage(IntPtr hwnd, WindowsMessages windowMessage, IntPtr wParam, IntPtr lParam)
 {
     if (UnsafeNative.SendMessage(hwnd, (int)windowMessage, wParam, lParam) != 0)
     {
         throw new Win32Exception(Marshal.GetLastWin32Error());
     }
 }
Exemple #2
0
        /// <summary>
        /// Sends the specified message string to a window. The SendMessage function calls the window
        /// procedure for the specified window and does not return until the window procedure has
        /// processed the message.
        /// </summary>
        /// <param name="hwnd">A handle to the window whose window procedure will receive the message.</param>
        /// <param name="message">The message to be sent to the window</param>
        /// <exception cref="Win32Exception">Win32 error occures</exception>
        /// <exception cref="ArgumentException">HWND is <see cref="IntPtr.Zero"/></exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is empty</exception>
        public static void SendMessage(IntPtr hwnd, string message)
        {
            if (hwnd == IntPtr.Zero)
            {
                throw new ArgumentException("HWND cannot be IntPtr.Zero");
            }

            if (message == null)
            {
                throw new ArgumentNullException("The message cannot be null");
            }

            if (message == "")
            {
                throw new ArgumentException("The message cannot be empty");
            }

            var messageBytes = Encoding.Default.GetBytes(message); /* ANSII encoding */
            var data         = new UnsafeNative.COPYDATASTRUCT
            {
                dwData = (IntPtr)100,
                lpData = message,
                cbData = messageBytes.Length + 1 /* +1 because of 0 termination */
            };

            if (UnsafeNative.SendMessage(hwnd, UnsafeNative.WM_COPYDATA, IntPtr.Zero, ref data) != 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Exemple #3
0
        /// <summary>
        /// The message is sent to all top-level windows in the system, including disabled or
        /// invisible unowned windows, overlapped windows, and pop-up windows; but the message is not
        /// sent to child windows.
        /// </summary>
        /// <param name="registeredWindowMessage">
        /// The registered window message. use <see cref="Win32Api.RegisterWindowMessage(string)"/>
        /// to register a message.
        /// </param>
        /// <param name="wParam">Additional message-specific information.</param>
        /// <param name="lParam">Additional message-specific information.</param>
        /// <exception cref="ArgumentException">Invalid registered window message</exception>
        /// <exception cref="Win32Exception">Win32 error occures</exception>
        public static void BroadcastMessage(uint registeredWindowMessage, IntPtr wParam, IntPtr lParam)
        {
            if (registeredWindowMessage < 0xC000)
            {
                throw new ArgumentException("Not a valid registered window message");
            }

            if (UnsafeNative.SendMessage(UnsafeNative.HWND_BROADCAST, (int)registeredWindowMessage, wParam, lParam) != 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }