【问题标题】:WndProc does not work when window is out of focus当窗口失焦时,WndProc 不起作用
【发布时间】:2019-07-19 07:56:28
【问题描述】:

我想将SendMessage 用于来自全局挂钩dllWM_COPYDATA,然后将其发送到myMainwindow WndProcWndProc只在活动屏时监听procs,在失焦时不接收dll发送的消息。

这是 WndProc 的限制吗?有没有更好的替代品?

【问题讨论】:

  • WM_COPYMESSAGE 是来自 Windows 11 的一种新消息吗?
  • 我的错,我想说的是 WM_COPYDATA

标签: c# sendmessage wndproc


【解决方案1】:

我发现问题出在我的SendMessage 通话中使用的HWND。它必须由所有 dll 共享,如下所示:

#pragma data_seg("Shared")
//our hook handle which will be returned by calling SetWindowsHookEx function
HHOOK hkKey = NULL;
HINSTANCE hInstHookDll = NULL;  //our global variable to store the instance of our DLL
HWND pHWnd = NULL; // global variable to store the mainwindow handle
#pragma data_seg() //end of our data segment

#pragma comment(linker,"/section:Shared,rws")
// Tell the compiler that Shared section can be read,write and shared

__declspec(dllexport) LRESULT CALLBACK procCharMsg(int nCode, WPARAM wParam, LPARAM lParam)
//this is the hook procedure
{
    //a pointer to hold the MSG structure that is passed as lParam
    MSG* msg;
    //to hold the character passed in the MSG structure's wParam
    char charCode;
    if (nCode >= 0 && nCode == HC_ACTION)
        //if nCode is less than 0 or nCode
        //is not HC_ACTION we will call CallNextHookEx
    {
        //lParam contains pointer to MSG structure.
        msg = (MSG*)lParam;
        if (msg->message == WM_CHAR)
            //we handle only WM_CHAR messages
        {
            SendMessage(pHWnd, WM_CHAR, (WPARAM)msg->message, (LPARAM)0); // This should now work globally
        }
    }
    return CallNextHookEx(hkKey, nCode, wParam, lParam);
}

// called by main app to establish a pointer of itself to the dlls
extern "C" __declspec(dllexport) int SetParentHandle(HWND hWnd) {
    pHWnd = hWnd;
    if (pHWnd == NULL) {
        return 0;
    }

    return 1;
}

我应该将我的代码连同问题一起发布,因为小事情总是很难单独发现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-22
    • 2011-03-07
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多