현재 포커스가있는 창을 하위 클래스로 만들려고합니다. CBT 후크를 사용하여 HCBT_ACTIVATE
이벤트를 모니터링하고 초점을 맞추고 이전에 포커스를 맞춘 창의 WndProc
을 설정 및 설정 해제합니다.이 창 서브 클래 싱 코드가 충돌하는 이유는 무엇입니까?
문제는 코드가 인 어딘가에 설정된 중단 점이있을 때만 작동한다는 것입니다.
중단 점이 없으면 서브 클래 싱을 제거하고 원래 WndProc를 복원 했어도 내 응용 프로그램이 종료되면 서브 클래 싱 된 모든 윈도우가 순서대로 충돌합니다.
내 응용 프로그램이 종료 될 때마다 Unsubclass()
이 호출됨을 확인했습니다.
// code extracts
HINSTANCE hInst;
HHOOK hHook;
#pragma data_seg(".shared")
HWND hWndSubclass = 0;
FARPROC lpfnOldWndProc = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.shared,rws")
void Unsubclass()
{
// if the window still exists
if (hWndSubclass != 0 && IsWindow(hWndSubclass))
{
SetWindowLongPtr(hWndSubclass, GWLP_WNDPROC, (LPARAM)lpfnOldWndProc);
hWndSubclass = 0;
}
}
static LRESULT CALLBACK SubClassFunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_MOVING)
{
// this is just test code so I can see it works (it does)
RECT* r = (RECT*)lParam;
r->right = r->left + 500;
r->bottom = r->top + 500;
return TRUE;
}
else if (message == WM_DESTROY)
{
Unsubclass();
}
return CallWindowProc((WNDPROC)lpfnOldWndProc, hWndSubclass, message, wParam, lParam);
}
void SubclassWindow(HWND hWnd)
{
// remove the subclassing for the old window
Unsubclass();
// subclass the new window
lpfnOldWndProc = (FARPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LPARAM)SubClassFunc);
hWndSubclass = hWnd;
}
static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE)
{
SubclassWindow((HWND)wParam);
}
return 0;
}
// ... code that initializes the CBT proc
__declspec(dllexport) BOOL Setup()
{
hHook = SetWindowsHookEx(WH_CBT, CBTProc, hInst, 0);
}
__declspec(dllexport) BOOL Teardown()
{
UnhookWindowsHookEx(hHook);
Unsubclass();
}
BOOL APIENTRY DllMain(HINSTANCE hInstance,
DWORD Reason,
LPVOID Reserved
)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
hInst = hInstance;
return TRUE;
case DLL_PROCESS_DETACH:
Unsubclass();
return TRUE;
}
return TRUE;
}
hWndServer는 누구입니까? – EFraim
hWndServer는 관련이 없습니다 (CBT 후크를 설정하는 프로세스입니다) . 내가 그것에 대한 참조를 제거했습니다. –
최종 Unsubclass 후 훅이 여전히 활성화되어 있고 종료하는 동안 여전히 윈도우를 서브 클래스화할 수 있습니까? –