zoukankan      html  css  js  c++  java
  • Message Demo

    一、 User defined message
    A::窗口消息----------------
    1. #define WM_MY_MSG WM_USER + 0x100
    2. afx_msg LRESULT OnMyMsg(WPARAM, LPARAM);
    3. LRESULT CTestDlg::OnMyMsg(WPARAM wParam, LPARAM lParam)
       {...}
    4. 
    BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
        ...
        ON_MESSAGE(WM_MY_MSG, &CTestDlg::OnMyMsg)
    END_MESSAGE_MAP()
    
    B::线程消息----------------
    1. #define  WM_MY_THRD_MSG  WM_USER + 100
    2. afx_msg void OnMyThrdMsg(WPARAM, LPARAM);
    3. void CTestApp::OnMyThrdMsg(WPARAM wParam, LPARAM lParam)
       {...}
    4. 
    BEGIN_MESSAGE_MAP(CTestApp, CWinAppEx)
        ...
        ON_THREAD_MESSAGE(WM_MY_THRD_MSG, &CTestApp::OnMyThrdMsg)
    END_MESSAGE_MAP()
    ***How to trigger in current thread***
    ::PostMessage(NULL, WM_MY_THRD_MSG, 0, 0);
    ::PostThreadMessage(::GetCurrentThreadId(), WM_MY_THRD_MSG, 0, 0);
    
    
    
    二、 Register Window Message 
    A::窗口消息----------------
    1. UINT nMsgRegistered = ::RegisterWindowMessage(TEXT("RegisterMsgName"));
    2. afx_msg LRESULT OnMyRegisteredMsg(WPARAM, LPARAM);
    3. LRESULT CTestDlg::OnMyRegisteredMsg(WPARAM wParam, LPARAM lParam)
       {...}
    4. 
    BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
        ...
        ON_REGISTERED_MESSAGE(nMsgRegistered, &CTestDlg::OnMyRegisteredMsg)
    END_MESSAGE_MAP()
    
    B::线程消息----------------
    1. UINT nThrdMsgRegistered = ::RegisterWindowMessage(TEXT("MyThrdMsg"));
    2. afx_msg void OnMyRegisterdThrdMsg(WPARAM, LPARAM);
    3. void CTestApp::OnMyRegisterdThrdMsg(WPARAM wParam, LPARAM lParam)
       {...}
    4. 
    BEGIN_MESSAGE_MAP(CTestApp, CWinAppEx)
        ...
        ON_REGISTERED_THREAD_MESSAGE(nThrdMsgRegistered, &CTestApp::OnMyRegisterdThrdMsg)
    END_MESSAGE_MAP()
    ***How to trigger in current thread***
    ::PostMessage(NULL, nThrdMsgRegistered, 0, 0);
    ::PostThreadMessage(::GetCurrentThreadId(), nThrdMsgRegistered, 0, 0);
    
    
    
    三、 PostThreadMessage
    A::同一进程不同线程之间-----------------
    1. 编写线程函数
    UINT AFX_CDECL ThrdTest(LPVOID lpParam)
    {
        MSG msg;
        while (::GetMessage(&msg, NULL, 0, 0))
        {
            switch(msg.message)
            {
            case WM_XXX:
            {...}
                break;
            ......
            default:
                break;
            }
        }
    
        return 0;
    }
    2. 启动线程
       UINT AFX_CDECL ThrdTest(LPVOID);
       AfxBeginThread(ThrdTest, ...);
    3. 向线程发送消息
       ::PostThreadMessage(nThrdId, WM_XXX, wParam, lParam);
       
    B::不同进程之间--------------------------
    1. 进程I
    #include <iostream>
    #include <Windows.h>
    
    using namespace std;
    
    int main()
    {
        MSG msg;
        while (::GetMessage(&msg, NULL, 0, 0))
        {
            switch(msg.message)
            {
            case WM_XXX:
            {...}
                break;
            ......
            default:
                break;
            }
        }
    
        return 0;
    }
    
    2. 进程II
    CreateProcess(...);
    ::PostThreadMessage(nThrdId, WM_XXX, wParam, lParam);
  • 相关阅读:
    当物联网遇上云原生:K8s向边缘计算渗透中
    NLP预训练发展小结一(Bert之前)
    netty系列之:搭建HTTP上传文件服务器
    netty系列之:搭建自己的下载文件服务器
    HTTP系列之:HTTP中的cookies
    HTTP系列之:HTTP缓存
    netty系列之:自建客户端和HTTP服务器交互
    [源码解析] 深度学习流水线并行 PipeDream(3)--- 转换模型
    [源码解析] 深度学习流水线并行 PipeDream(2)--- 计算分区
    [源码解析] 深度学习流水线并行之PipeDream(1)--- Profile阶段
  • 原文地址:https://www.cnblogs.com/Hisin/p/2508339.html
Copyright © 2011-2022 走看看