Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C++

Manipulating Windows using messages and simple CBT hooking

Rate me:
Please Sign up or sign in to vote.
4.68/5 (33 votes)
7 Aug 2003Ms-PL8 min read 179.4K   2.1K   73  
Demonstrates techniques using windows messages and hooks, which allow us to automate a windows properties dialog or even custom applications
// DemoDll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "DemoDll.h"
#include "shellapi.h"
#include "tchar.h"

HINSTANCE g_hModule =  NULL;

#pragma data_seg(".HookDll")
HWND g_hWndEffects = NULL;
HHOOK g_hook = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.HookDll,rws")



BOOL APIENTRY DllMain( HANDLE hModule, 
					  DWORD  ul_reason_for_call, 
					  LPVOID lpReserved
					  )
{
	g_hModule = (HINSTANCE)hModule;
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}


DEMODLL_API BOOL ToggleMenuUnderline(void)
{
	HWND hWndAppearance = NULL;
	BOOL ret = TRUE;

	g_hook = SetWindowsHookEx(WH_CBT, CBTProc, g_hModule, 0); 

	ret = BringUpDisplayAppearance();
	Sleep(500);//Wait for the window to come up
	if(ret)
	{
		hWndAppearance = FindWindow(NULL,
			_T("Display Properties"));
		ret = hWndAppearance != NULL;		

		if(ret)
		{
			HWND hWndEffectsButton = GetEffectsButton(hWndAppearance);
			ret = hWndEffectsButton != NULL;

			if(ret)
			{
				PostMessage(hWndEffectsButton,BM_CLICK,0,0);	

				//Wait for the Effects window to come up	
				while(!IsWindow(g_hWndEffects))
					Sleep(100);			

				HWND hWndCheck = GetMenuUnderlineCheck(g_hWndEffects);

				ret = hWndCheck != NULL;

				if(ret)
				{						
					PostMessage(hWndCheck,BM_CLICK,0,0);										
					PostMessage(g_hWndEffects,WM_COMMAND,IDOK,NULL);
					PostMessage(hWndAppearance,WM_COMMAND,IDOK,NULL);

					//Wait for the window to be dismissed
					while(IsWindow(hWndAppearance))
						Sleep(100);				
				}
			}
		}
	}

	//Final checks in case of error conditions
	if(IsWindow(g_hWndEffects))
		PostMessage(g_hWndEffects,WM_CLOSE,0,0);
	if(IsWindow(hWndAppearance))
		PostMessage(hWndAppearance,WM_CLOSE,0,0);

	return ret;
}

BOOL BringUpDisplayAppearance()
{
	return reinterpret_cast<int>(ShellExecute(GetDesktopWindow(),
		"open","control.exe","desk.cpl Display,@Appearance",
		"",SW_SHOW )) > 32 ? TRUE : FALSE;	
}

HWND GetEffectsButton(HWND hWndParent)
{
	HWND hWnd = NULL;

	EnumChildWindows(hWndParent, EnumAppearanceChildProc, (LPARAM)&hWnd);
	return hWnd;
}

BOOL CALLBACK EnumAppearanceChildProc(HWND hwnd, LPARAM lParam)
{
	TCHAR buff[512];
	GetWindowText(hwnd,buff,512);	
	if(_tcscmp(buff,_T("&Effects...")) == 0)
	{		
		*reinterpret_cast<HWND*>(lParam) = hwnd;
		return FALSE;
	}
	return TRUE;
}

HWND GetMenuUnderlineCheck(HWND hWndParent)
{
	HWND hWnd = NULL;	
	EnumChildWindows(hWndParent, EnumEffectsChildProc, (LPARAM)&hWnd);
	return hWnd;
}

BOOL CALLBACK EnumEffectsChildProc(HWND hwnd, LPARAM lParam)
{
	TCHAR buff[512];
	GetWindowText(hwnd,buff,512);	
	if(_tcsstr(buff,_T("&Hide underlined")))
	{		
		*reinterpret_cast<HWND*>(lParam) = hwnd;
		return FALSE;
	}
	return TRUE;
}

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(nCode == HCBT_ACTIVATE)
	{
		HWND hWnd = (HWND) wParam;
		TCHAR buff[512];
		GetWindowText(hWnd,buff,512);
		if(_tcscmp(buff,_T("Effects")) == 0)
		{	
			ShowWindow(hWnd,SW_HIDE);
			g_hWndEffects = hWnd;		
			UnhookWindowsHookEx(g_hook);						
		}
		if(_tcscmp(buff,_T("Display Properties")) == 0)
		{				
			ShowWindow(hWnd,SW_HIDE);											
		}
	}

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions