zoukankan      html  css  js  c++  java
  • C# 进程通信SendMessage和有关消息参数

    SendMessage是啥?

    函数原型:

    LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)
    SendMessage(窗口句柄,消息,参数1,参数2)

    函数功能:

    该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。

    在C#中,我们可以这样用:

    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    各个参数作用:

    hWnd 发送消息总得有个目标,这个参数就是这样用的。寻找要发送的窗口句柄有很多中方式,以下就是其中一种

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    Msg 指定发送的消息,这里以WM_SYSCOMMAND为例,大家可以在找到相关用法:https://docs.microsoft.com/zh-cn/windows/desktop/menurc/wm-syscommand

    接下来的两个参数都可以看以上的连接看到了。

    因此,以下的代码含义为向hwnd发送一个最小化的消息。

    SendMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0)

    怎么接收消息?

    HwndSource hWndSource;
    WindowInteropHelper wih = new WindowInteropHelper(this);
    hWndSource = HwndSource.FromHwnd(wih.Handle);
    //添加处理程序 
    hWndSource.AddHook(MainWindowProc);
    private static IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        
        return IntPtr.Zero;
    }

    以上的两个代码结合,就能处理发送过来的消息了。

     

  • 相关阅读:
    jedis 连接 虚拟机内redis服务
    数据库存储过程项目总结
    前端jsp联系项目相关经验
    学习做项目的一些随笔
    循环
    字典
    集合
    元组
    列表
    字符串
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/10451906.html
Copyright © 2011-2022 走看看