将消息从c#应用程序发布到活动窗口/应用程序

时间:2017-01-07 09:18:46

标签: c# .net windows visual-studio winapi

我有一个应用程序(总是以最小化模式运行),它不断地监听键盘上键入的键。 当按下某个键时,我希望C#应用程序在活动窗口的消息队列上发布命令以最小化,关闭,最大化等。 我知道有可能获得活动窗口的句柄,但是,我如何在其消息队列上发布消息(就像我们在win32中所做的那样)。 感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用interop来调用本机WINAPI函数。使用p/invoke网站,我创建了以下解决方案:

var proc = Process.GetProcesses().First(p => p.ProcessName.Contains("firefox"));

PostMessageSafe(
    new HandleRef(proc, proc.MainWindowHandle), 
     (uint) WM.WM_SYSCOMMAND, 
    new IntPtr((int) SysCommands.SC_MAXIMIZE), 
    IntPtr.Zero);

我在这里基本上做的是找到我感兴趣的WindowHandle,然后在Msg中使用WM_SYSCOMMAND调用PostMessage,在wParam中调用相应的syscommand,在本例中为Maximize,值为0xF030。 lParam设置为0。

请注意,如果您的唯一目标是更改窗口状态,那么最好使用专门的API端点,它称为ShowWindow

它的signature看起来像这样:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

,它被调用如下:

ShowWindow(proc.MainWindowHandle, ShowWindowCommands.Maximize);

当调用这些方法中的任何一个时,将最大化来自名为firefox的进程的窗口。

以下是PostMessage

的包装器所需的辅助方法和枚举
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

// http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html
public enum WM : uint
{
    WM_SYSCOMMAND = 0x0112
}

// http://www.pinvoke.net/default.aspx/Enums/SysCommands.html
enum SysCommands : int
{
    SC_SIZE = 0xF000,
    SC_MOVE = 0xF010,
    SC_MINIMIZE = 0xF020,
    SC_MAXIMIZE = 0xF030,
    SC_NEXTWINDOW = 0xF040,
    SC_PREVWINDOW = 0xF050,
    SC_CLOSE = 0xF060,
    SC_VSCROLL = 0xF070,
    SC_HSCROLL = 0xF080,
    SC_MOUSEMENU = 0xF090,
    SC_KEYMENU = 0xF100,
    SC_ARRANGE = 0xF110,
    SC_RESTORE = 0xF120,
    SC_TASKLIST = 0xF130,
    SC_SCREENSAVE = 0xF140,
    SC_HOTKEY = 0xF150,
    //#if(WINVER >= 0x0400) //Win95
    SC_DEFAULT = 0xF160,
    SC_MONITORPOWER = 0xF170,
    SC_CONTEXTHELP = 0xF180,
    SC_SEPARATOR = 0xF00F,
    //#endif /* WINVER >= 0x0400 */

    //#if(WINVER >= 0x0600) //Vista
    SCF_ISSECURE = 0x00000001,
    //#endif /* WINVER >= 0x0600 */

    /*
        * Obsolete names
        */
    SC_ICON = SC_MINIMIZE,
    SC_ZOOM = SC_MAXIMIZE,
}


// http://www.pinvoke.net/default.aspx/Enums/ShowWindowCommand.html
enum ShowWindowCommands
{
    /// <summary>
    /// Hides the window and activates another window.
    /// </summary>
    Hide = 0,
    /// <summary>
    /// Activates and displays a window. If the window is minimized or 
    /// maximized, the system restores it to its original size and position.
    /// An application should specify this flag when displaying the window 
    /// for the first time.
    /// </summary>
    Normal = 1,
    /// <summary>
    /// Activates the window and displays it as a minimized window.
    /// </summary>
    ShowMinimized = 2,
    /// <summary>
    /// Maximizes the specified window.
    /// </summary>
    Maximize = 3, // is this the right value?
                  /// <summary>
                  /// Activates the window and displays it as a maximized window.
                  /// </summary>       
    ShowMaximized = 3,
    /// <summary>
    /// Displays a window in its most recent size and position. This value 
    /// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except 
    /// the window is not activated.
    /// </summary>
    ShowNoActivate = 4,
    /// <summary>
    /// Activates the window and displays it in its current size and position. 
    /// </summary>
    Show = 5,
    /// <summary>
    /// Minimizes the specified window and activates the next top-level 
    /// window in the Z order.
    /// </summary>
    Minimize = 6,
    /// <summary>
    /// Displays the window as a minimized window. This value is similar to
    /// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the 
    /// window is not activated.
    /// </summary>
    ShowMinNoActive = 7,
    /// <summary>
    /// Displays the window in its current size and position. This value is 
    /// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the 
    /// window is not activated.
    /// </summary>
    ShowNA = 8,
    /// <summary>
    /// Activates and displays the window. If the window is minimized or 
    /// maximized, the system restores it to its original size and position. 
    /// An application should specify this flag when restoring a minimized window.
    /// </summary>
    Restore = 9,
    /// <summary>
    /// Sets the show state based on the SW_* value specified in the 
    /// STARTUPINFO structure passed to the CreateProcess function by the 
    /// program that started the application.
    /// </summary>
    ShowDefault = 10,
    /// <summary>
    ///  <b>Windows 2000/XP:</b> Minimizes a window, even if the thread 
    /// that owns the window is not responding. This flag should only be 
    /// used when minimizing windows from a different thread.
    /// </summary>
    ForceMinimize = 11
}