别人的程序加入自己的菜单

来源:互联网 发布:腊肠的吃法 知乎 编辑:程序博客网 时间:2024/04/30 12:57

CSDN 经常看到有人问, 我把代码贴出来

已经编译好的程序 http://download.csdn.net/source/267091

#include "StdAfx.h"
#include 
<tchar.h>
#include 
"ComFun/ComFun.h"

///////////////////////////////////////////////////////////////////////////////////
//PPSYS.DLL
//Coder: Yofoo
///////////////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK CallWndProcRetFunc(
int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK GetMsgProcFunc(
int nCode, WPARAM wParam, LPARAM lParam);

#define SC_WNDTOP        0x0C01
#define MF_MYTOPMENU    0xFFFF
//#define DLG_NEEDSTYLE    WS_OVERLAPPEDWINDOW
#define DLG_NEEDSTYLE    (WS_CAPTION | WS_SYSMENU)
#define MENUTOP_STR        _T("Always On Top(&T)")

#pragma data_seg(".SHARE")
_declspec(allocate(
".SHARE"))
HHOOK        hCallWndRet    
= NULL;
HHOOK        hGetMsg        
= NULL;
HWND        g_hThisWnd    
= NULL;
#pragma data_seg ()
#pragma comment(linker, "/SECTION:.SHARE,RWS")

//std::map<HWND, BOOL>    WindowTop;

extern    HINSTANCE        hins;

BOOL    bMenuInserted    
= FALSE;
BOOL    bWndToped        
= FALSE;
HMENU    hSysMenu        
= NULL;
UINT    uMenuID            
= -1;

//    10001020
BOOL InstallMenu(HWND hThisWnd = NULL)
{
    g_hThisWnd 
= hThisWnd;
    hCallWndRet 
= SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndProcRetFunc, hins, 0);
    hGetMsg    
= SetWindowsHookEx(WH_GETMESSAGE, GetMsgProcFunc, hins, 0);
    
return TRUE;
}

//    10001070
BOOL RemoveTopMenu()
{
    BOOL    bRetVal 
= TRUE;
    bRetVal 
&= UnhookWindowsHookEx(hGetMsg);
    bRetVal 
&= UnhookWindowsHookEx(hCallWndRet);
    
return bRetVal;
}

//    10001120
BOOL DeleteTopMenu(HWND hWnd)
{
    
int        nMenuItemCount, i, nRetVal;
    TCHAR    buf[
1024];

    nMenuItemCount 
= GetMenuItemCount(hSysMenu);
    
for(i=nMenuItemCount-1; i>=0; i--)
    {
        nRetVal 
= GetMenuString(hSysMenu, i, buf, sizeof(buf), MF_BYPOSITION);
        
if(nRetVal == 0)
            
continue;
        
if(_tcscmp(MENUTOP_STR, buf) == 0)
        {
            RemoveMenu(hSysMenu, i, MF_BYPOSITION);
            i
--;
            RemoveMenu(hSysMenu, i, MF_BYPOSITION);
            DrawMenuBar(hWnd);
            bMenuInserted 
= FALSE;
            
return TRUE;
        }
    }
    bMenuInserted 
= FALSE;
    
return FALSE;
}

//    100010A0
BOOL InsertTopMenu(HWND hWnd, BOOL bToped)
{
    UINT    uFlags;

    
if(bMenuInserted)
        
return FALSE;
    hSysMenu 
= GetSystemMenu(hWnd, FALSE);
    uMenuID 
= GetMenuItemID(hSysMenu, 0);
    AppendMenu(hSysMenu, MF_SEPARATOR, NULL, NULL);

    
if(bToped)
        uFlags 
= MF_CHECKED;
    
else
        uFlags 
= MF_UNCHECKED;
    AppendMenu(hSysMenu, uFlags, SC_WNDTOP, MENUTOP_STR);
    DrawMenuBar(hWnd);
    bMenuInserted 
= TRUE;
    
return TRUE;
}



//    10001290
LRESULT CALLBACK CallWndProcRetFunc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LRESULT            lRetVal;
    PCWPRETSTRUCT    pCwpret;
    
if(nCode < 0 )
    {
        lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
        
return lRetVal;
    }
    pCwpret 
= (PCWPRETSTRUCT)lParam;
    
if(pCwpret->hwnd == g_hThisWnd)
    {
        lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
        
return lRetVal;
    }
    
if(pCwpret->message == WM_INITMENU)
    {
        
if(bMenuInserted)
        {
            lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
            
return lRetVal;
        }
        LONG    lStyle;
        lStyle 
= GetWindowLong(pCwpret->hwnd, GWL_STYLE);
        
if((lStyle & DLG_NEEDSTYLE) == 0 )
        {
            lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
            
return lRetVal;
        }
//        HWND    hParWnd;
//        hParWnd = GetParent(pCwpret->hwnd);
//        if( hParWnd != NULL)
//        {
//        }
        InsertTopMenu(pCwpret->hwnd, bWndToped);
    }
    
else if(pCwpret->message == WM_MENUSELECT)
    {
        HMENU        hMenu;
        UINT        uFlags;
        uFlags 
= pCwpret->wParam >> 16;
        hMenu 
= (HMENU)pCwpret->lParam;

        
if(uFlags != MF_MYTOPMENU)
        {
            lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
            
return lRetVal;
        }
        
if(hMenu != NULL)
        {
            lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
            
return lRetVal;
        }
        DeleteTopMenu(pCwpret
->hwnd);
    }
    lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
    
return lRetVal;
}

//    100011D0
LRESULT CALLBACK GetMsgProcFunc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LRESULT        lRetVal;
    PMSG        pMsg;
    HWND        hWndInsertAfter;

    
if(nCode < 0 || wParam == PM_NOREMOVE)
    {
        lRetVal 
= CallNextHookEx(hGetMsg, nCode, wParam, lParam);
        
return lRetVal;
    }
    pMsg 
= (PMSG)lParam;
    
if(pMsg->hwnd == g_hThisWnd || 
        pMsg
->message != WM_SYSCOMMAND || 
        pMsg
->wParam != SC_WNDTOP)
    {
        lRetVal 
= CallNextHookEx(hCallWndRet, nCode, wParam, lParam);
        
return lRetVal;
    }

    
if(bWndToped)
    {
        hWndInsertAfter 
= HWND_NOTOPMOST;
        bWndToped        
= FALSE;
    }
    
else
    {
        hWndInsertAfter 
= HWND_TOPMOST;
        bWndToped        
= TRUE;
    }
    SetWindowPos(pMsg
->hwnd, hWndInsertAfter, 0000
        SWP_NOSIZE 
| SWP_NOMOVE);
    DeleteTopMenu(pMsg
->hwnd);
    InsertTopMenu(pMsg
->hwnd, bWndToped);
    
return 0;
}












 
原创粉丝点击