SendMessage与PostMessage

这个主题关注两件事

1.SendMessage 和PostMessage 的区别

2.如何用SendMessage,PostMessage使用WM_KEYDOWN消息。找到LParam 和 WParam参数的方法


为了理解SendMessage和PostMessage,请看下面的表


SendMessage PostMessage
向窗口发送消息。对于特定的窗口SendMessage函数调用窗口过程,
直到窗口过程已经处理完成后才返回。
在和创建特定窗口的线程相关的消息队列中放置一个消息并返回,
不等待线程处理消息
顺序的 非顺序的
同步的 异步

如何使用SendMessage,PostMessage

//
// ::PostMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//
//
// ::SendMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//

参数

HWND  窗口句柄

WPARAM  参数

LPARAM  参数

如何找到LParam

首先,你必须下载微软的spy++. 如果你安装VS2012, spy++集成

我将做一个例子演示,如何得到VK_KEYDOWN 的LParam 参数值,通过一些基础步骤

现在打开 Notepad 通过 运行->notepad

打开你的 Spy++(在vs2012中)

SendMessage与PostMessage_第1张图片

下一步,点击菜单 Spy->FindWindows,使用红框中的十字架圆圈

SendMessage与PostMessage_第2张图片

拖拽这个十字圆到Notepad的编辑区域

SendMessage与PostMessage_第3张图片

单击 选择消息属性如下面的图片。点击OK按钮

SendMessage与PostMessage_第4张图片

让我们按下回车键在编辑区域,你可以轻松的看到WM_KEYDOWN 消息在Spy++控制板。然后,右击该消息并选择属性,

你可以看到LPARAM和WPARAM值

我的LPARAM 值为:0x0001C001 WPARAM值为 0x0000000D

SendMessage与PostMessage_第5张图片

::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

::SendMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

使用的源码

使用FindWindowA 和 FindWindowEx

得到句柄的API

HWND hwnd = FindWindowA("Notepad", NULL );
HWND hWndChild = ::FindWindowEx(hwnd, NULL, L"Edit", NULL);
 
 

* *******************************************************************************
 Developer: zidane ([email protected])
 Last Modified: 2015-09-01
 * ******************************************************************************/
BOOL CReadInfomationGameDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);   // Set big icon
    SetIcon(m_hIcon, FALSE);  // Set small icon
    
    // TODO: Add extra initialization here
    CString strCommand[] =  
    {

        L"learn-tech-tips",
        L"http://learn-tech-tips.blogspot.com",
        L"[email protected]",
        L"zidane"
        L"http://www.codeproject.com/Tips/1025019/Get-Server-Side-Data-time-with-Cplusplus",
    };

    int length = sizeof(strCommand) / sizeof(strCommand[0]);
    for (int i=0; i<length; i++)
        m_cbCommand.AddString(strCommand[i]);
 
 
    return TRUE;  // return TRUE  unless you set the focus to a control
}

HWND CReadInfomationGameDlg::GetHandleOfWindow(void)
{ 
    HWND hwnd = FindWindowA("Windows abc Class", NULL ); 

    if (!hwnd) 
    {
        MessageBox(_T("Window abc not found"), _T("Message"), MB_OK 
                                                    | MB_ICONINFORMATION);  
        return NULL;
    } 
 
    return hwnd;
}

void CReadInfomationGameDlg::OnBnClickedButtonAddmessage()
{
    // TODO: Add your control notification handler code here
    HWND hHandle = GetHandleOfWindow();

    CString strCBText;
    int nIndex = m_cbCommand.GetCurSel();
 
    if (nIndex == -1) // cannot found text in comboBox, post NULL
    {
        TCHAR strTemp[255];  
        memset(strTemp, 0x00, sizeof(strTemp));
        GetDlgItemText(IDC_COMBO_COMMAND, strTemp, sizeof(strTemp));
        PostChatMessage(hHandle, strTemp);
    }
    else // found text in comboBox
    {
        m_cbCommand.GetLBText(nIndex, strCBText); 
        PostChatMessage(hHandle, strCBText);
    }
}

void CReadInfomationGameDlg::PostChatMessage(HWND hHandle, LPCTSTR szChatMsg)
{
     ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
     ::PostMessage(hHandle, WM_KEYDOWN, VK_DOWN, 0x00500001);

    while (szChatMsg[0])
    {
        ::PostMessage(hHandle, WM_CHAR, LOBYTE(szChatMsg[0]),0);
        szChatMsg++;
    }

    ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
}

示例源码:http://download.csdn.net/detail/zang141588761/9224163



你可能感兴趣的:(SendMessage与PostMessage)