窗口 超类化 子类化 HOOK

时间:2020-12-09 20:42:35

body
{
font-family: Bitstream Vera Sans Mono;
font-size: 11pt;
line-height: 1.5;
}
html, body
{
color: #000000;
background-color: #C2E7C7;
}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}

窗口 超类化 子类化 HOOK

#include "StdAfx.h"
#include "MyGameWnd.h"
#include <strsafe.h>
WNDPROC g_pSubclassOldProc ;   // 保存窗口默认的消息响应函数指针
WNDPROC g_pSuperOldProc ;// 保存窗口默认消息处理函数
HWND g_hMySuperWnd = NULL;
HWND g_hMySubWnd = NULL;
// 用于替换子类化窗口的消息响应函数
LRESULT CALLBACK MyProcSubClass( HWND hWnd , UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message )
    {
    case WM_CHAR :
        {
            :: MessageBox(hWnd , "WM_CHAR响应", "子类化" , MB_OK);
            return 0;
        }
    case WM_USERMESSAGE :
        OutputDebugStringA("自定义消息响应" );
        :: MessageBox(hWnd , "自定义消息响应", "子类化" , MB_OK);
        break;
    default: return ::CallWindowProc( g_pSubclassOldProc, hWnd , message, wParam , lParam);
    }
}
// 对创建好的窗体进行子类化代码
void creat_my_sub_wnd (HINSTANCE hInstance,HWND hWnd, int nCmdShow)
{
    // 创建
    g_hMySubWnd = CreateWindowEx (NULL, "EDIT", "MyGameSubClassWnd" ,WS_CHILD, 0,0, 0, 0, hWnd, NULL , hInstance, NULL);//注意此处的EDIT必须是现有的 窗口类型名称,不能是随便的值
    if (g_hMySubWnd == NULL)
    {
//         char buf[100] = {0};
//         sprintf(buf,"%x",GetLastError());
//         OutputDebugStringA(buf);
    }
    g_pSubclassOldProc = (WNDPROC )::SetWindowLong( g_hMySubWnd, GWL_WNDPROC, (DWORD )MyProcSubClass);
    // 显示
    // ShowWindow(hMyWnd, nCmdShow);
    UpdateWindow(hWnd );
    :: PostMessage(g_hMySubWnd ,WM_USERMESSAGE, NULL,NULL );
}
// 用于替换的超类化消息响应函数
LRESULT CALLBACK my_super_wnd_proc( HWND hWnd , UINT message, WPARAM wParam , LPARAM lParam)
{
    switch(message )
    {
    case WM_CHAR :
        {
            :: MessageBox(hWnd , "WM_CHAR响应", "超类化" , MB_OK);
            return 0;
        }
    case WM_USERMESSAGE :
        :: MessageBox(hWnd , "自定义消息响应", "超类化" , MB_OK);
        break;
    default:
        return ::CallWindowProc (g_pSuperOldProc, hWnd, message , wParam, lParam );
    }
}
// 创建超类化控件代码
void creat_my_super_wnd (HINSTANCE hInstance,HWND hWnd, int nCmdShow)
{
    // 取得原控件信息
    WNDCLASSEX myeditClass ;
    :: GetClassInfoEx(hInstance , "EDIT", & myeditClass); //注意此处的EDIT必须是现有的 窗口类型名称,不能是随便的值
    // 保存原控件默认消息处理函数
    g_pSuperOldProc = myeditClass .lpfnWndProc;
    // 设置替换的消息处理函数
    myeditClass.lpfnWndProc = my_super_wnd_proc;
    // 指定新的窗口类名字
    myeditClass.lpszClassName = "MyGameSuperClass";
    // 设置结构体大小
    myeditClass.cbSize = sizeof( WNDCLASSEX);
    // 注册新信息
    RegisterClassEx(&myeditClass );
    // 创建
    g_hMySuperWnd = CreateWindowEx (NULL, myeditClass.lpszClassName , "MyGameSuperClassWnd",     WS_CHILD, 0,0, 0, 0, hWnd, NULL , hInstance, NULL);
    if (g_hMySuperWnd == NULL)
    {
        char buf [100] = {0};
        sprintf(buf ,"%x", GetLastError());
        OutputDebugStringA(buf );
    }
    // 显示
    //ShowWindow(hEdit, nCmdShow);
    UpdateWindow(hWnd );
    :: PostMessage(g_hMySuperWnd ,WM_USERMESSAGE, NULL,NULL );
}