// 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;
}