C++ 全局热键注册及监听隐藏呼出托盘图标(老板键功能)

LRESULT OnHotKey(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

LRESULT CMainFrame::OnHotKey(UINT uMsg, WPARAM wParam,LPARAM lParam, BOOL& bHandled) {

    LRESULT lRes = 0;

if ( hot_key_state == "0" && wParam == MYKEY1 ) //如果玩家开启老板键并按下的定义的热键

{

//标记按下键次数

key_state_num += 1;

if(key_state_num%2==0)//呼出程序

{

ShowTrayIcon("360",true);

}

else//隐藏游戏

{

ShowTrayIcon("360",false);

}

writeTxt(key_state_num%2==0);

}

    return TRUE;

}


typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);


BOOL IsWow64()

{

BOOL bIsWow64 = FALSE;

LPFN_ISWOW64PROCESS

fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(

GetModuleHandle(_T("kernel32")),"IsWow64Process");

if (NULL != fnIsWow64Process)

{

if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))

{

// handle error

}

}

return bIsWow64;

}

void ShowTrayIcon(char szIcon[] ,BOOL show)

{

setlocale(LC_ALL, "chs");//加上这句,没加的话可能乱码

    HWND hWnd,hWndPaper;

    unsigned long lngPID;

    long ret,lngButtons;

    HANDLE hProcess;

    LPVOID lngAddress;

    long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;

BYTE strBuff[1024]={0};

//WCHAR strBuff[1024]={0};

    char* str = NULL;

    char *pp = NULL;

//ofstream in;

CString strFilePath;

    hWnd = FindWindow(TEXT("Shell_TrayWnd"), NULL);

    hWnd = FindWindowEx(hWnd, 0, TEXT("TrayNotifyWnd"), NULL);

    hWndPaper = FindWindowEx(hWnd, 0, TEXT("SysPager"), NULL);

    if(!hWndPaper)

        hWnd = FindWindowEx(hWnd, 0, TEXT("ToolbarWindow32"), NULL);

    else

        hWnd = FindWindowEx(hWndPaper, 0, TEXT("ToolbarWindow32"), NULL);

    ret = GetWindowThreadProcessId(hWnd, &lngPID);

    hProcess = OpenProcess(PROCESS_ALL_ACCESS

                            |PROCESS_VM_OPERATION

                            |PROCESS_VM_READ

                            |PROCESS_VM_WRITE,

                            0,

                            lngPID);

    lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);

    lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);

int nDataOffset = sizeof(TBBUTTON) - sizeof(INT_PTR) - sizeof(DWORD_PTR);

int nStrOffset = 18;

if ( IsWow64() ){

nDataOffset+=4;

nStrOffset+=6;

}

    for(int i=0 ;i< lngButtons - 1;i++)

    {

        ret = SendMessage(hWnd,TB_GETBUTTON,i,long(lngAddress));

        ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16),&lngTextAdr,4,0);

        if(lngTextAdr != -1)

        {

            ret = ReadProcessMemory(hProcess, LPVOID(lngTextAdr),strBuff,1024,0);

            ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0);

            ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);

            ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4),&lngButtonID,4,0);

strFilePath = (WCHAR *)strBuff + nStrOffset;

CString strgjqt("test");

if(strFilePath.Find(strgjqt)!= -1)

{

                if(show)

                {

                    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,0);

                }

                else

                {

                    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,1);

                }

            }

        }

    }

    VirtualFreeEx( hProcess,  lngAddress,  0X4096, MEM_RELEASE);

    CloseHandle(hProcess);

}

RegisterHotKey(m_hWnd, MYKEY1, MOD_ALT, ‘D’);//注册热键

UnregisterHotKey(m_hWnd,MYKEY1);//卸载热键

你可能感兴趣的:(C++ 全局热键注册及监听隐藏呼出托盘图标(老板键功能))