簡體   English   中英

如何避免重復的SendMessage

[英]How to avoid repeated SendMessage

我有一個問題:我通過DLL中的過程使用SendMessage與主窗口進行通信; procedure是一個掛鈎過程,它使主窗口知道何時在編輯框中單擊鼠標右鍵; 它也發送編輯框的句柄。 除此錯誤外,它運行良好:當程序運行時沒有斷點時,主窗口會收到兩次相同的消息(在本例中為WM_APP),而如果我在掛鈎過程或處理WM_APP消息的塊中放入斷點,則消息為考慮一次。 有關更多說明,請問我。 遵循掛鈎過程和處理WM_APP消息的塊的代碼。 謝謝

掛鈎程序

MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
    // if user clicked with mouse right button
    if(wParam != NULL && (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP))
    {
        wchar_t *s = (wchar_t*) malloc(CLASSNAMELEN*sizeof(wchar_t));
        //MessageBox(mainHwnd, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        GetClassName(m->hwnd, (LPWSTR) s, CLASSNAMELEN);
        //MessageBox(mainHwnd, (LPCWSTR) s, (LPCWSTR)L"Test", MB_OK);
        // only if user clicked on a edit box
        if(wcsncmp(s, L"Edit", 4) == 0)
            SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam);
        free(s);
        s = NULL;
    }
}

// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

處理WM_APP消息的主程序中的塊

case WM_APP:
    {
        //MessageBox(hWnd, (LPCWSTR)L"Received WM_APP", (LPCWSTR)L"Test", MB_OK);
        // copies text from the edit box
        MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
        int n = GetWindowTextLength(m->hwnd);
        // if text has been inserted
        if(n > 0 && n < 1024)
        {
            wchar_t *s = (wchar_t*) malloc((n+1)*sizeof(wchar_t));
            // gets text
            GetWindowText(m->hwnd, (LPWSTR) s, n+1);
            s[n] = (wchar_t) 0;
            //MessageBox(hWnd, (LPCWSTR)s, (LPCWSTR)L"Test", MB_OK);
            // saves text in database
            stateClassPointer->insertInList(s); 
        }
    }
    break;

可能是因為您要發送有關WM_RBUTTONDOWN和WM_RBUTTONUP的消息,即當按下右按鈕並釋放它時。

調試時,調試器會吃掉WM_RBUTTONUP,所以您不會得到它。

PS:為了安全起見,您不應該使用PostMessage()而不是SendMessage()嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM