ah-shellext / HookSystemMenu

Add some common function into window's system menu
1 stars 0 forks source link

记录 #1

Open Aoi-hosizora opened 5 years ago

Aoi-hosizora commented 5 years ago
Aoi-hosizora commented 5 years ago
DLLEXPORT bool __stdcall InitGetMsgHook(int threadId, HWND destination) {
    if (hMod == NULL) 
        return false;
    if (hwndMain != NULL) {
        UINT msg = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG_REPLACE");
        if (msg != 0)
            SendNotifyMessage(hwndMain, msg, 0, 0);
    }
    hwndMain = destination;
    hookGetMsg = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) GetMsgHookCb, hMod, threadId);
    return hookGetMsg != NULL;
}

static LRESULT CALLBACK GetMsgHookCb(int code, WPARAM wParam, LPARAM lParam) {
    if (code >= 0) {
        UINT msg = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG");
        UINT msg2 = RegisterWindowMessage(L"AH_SYSTEM_MENU_HOOK_GETMSG_PARAMS");

        auto pMsg = (MSG *) lParam;
        if (msg != 0 && pMsg->message != msg && pMsg->message != msg2 && wParam == PM_REMOVE) {
            if (pMsg->message == WM_SYSCOMMAND) {
                SendNotifyMessage(hwndMain, msg, (WPARAM) pMsg->hwnd, pMsg->message);
                SendNotifyMessage(hwndMain, msg2, pMsg->wParam, pMsg->lParam);
            }
        }
    }
    return CallNextHookEx(hookGetMsg, code, wParam, lParam);
}
if (m.Msg == HookMessage.MSG_HGETMSG_GETMSG) {
    addToList(m.WParam, "GetMsg");
    WinUtil.cacheHandle = m.WParam;
    WinUtil.cacheMessage = m.LParam;
} else if (m.Msg == HookMessage.MSG_HGETMSG_GETMSG_PARAMS) {
    if (WinUtil.cacheHandle != IntPtr.Zero && WinUtil.cacheMessage != IntPtr.Zero) {
        addToList(WinUtil.cacheHandle, "GetMsgParam");
        onWinProcMsg(WinUtil.cacheHandle, WinUtil.cacheMessage, m.WParam, m.LParam);
        WinUtil.cacheHandle = WinUtil.cacheMessage = IntPtr.Zero;
    }
}
private void onWinProcMsg(IntPtr hwnd, IntPtr message, IntPtr WParam, IntPtr LParam) {
    if (message.ToInt64() == NativeConstant.WM_SYSCOMMAND) {
        uint menuid = (uint)(WParam.ToInt64() & 0x0000FFFF);
        if (menuid == WinUtil.MENU_ID_TOPMOST)
            WinUtil.OnTopMostMenuItemClick(hwnd);
        else if (menuid == WinUtil.MENU_ID_PRTSC)
            WinUtil.OnPrtScMenuItemClick(hwnd);
        else if (menuid == WinUtil.MENU_ID_PATH)
            WinUtil.OnPathMenuItemClick(hwnd);
    }
}