#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 );
}