Skip to content

Commit

Permalink
#2
Browse files Browse the repository at this point in the history
- Implemented the close to tray feature.
  • Loading branch information
Dakoon committed Apr 7, 2017
1 parent fddeb35 commit 652e014
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 70 deletions.
131 changes: 123 additions & 8 deletions WhatsappTray/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@

static HHOOK _hMouse = NULL;
static HHOOK _hWndProc = NULL;
static HHOOK _mouseProc = NULL;
static HHOOK _cbtProc = NULL;
static HWND _hLastHit = NULL;

// Only works for 32-bit apps or 64-bit apps depending on whether this is complied as 32-bit or 64-bit (Whatsapp is a 64Bit-App)
LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
CWPRETSTRUCT *msg = (CWPRETSTRUCT*)lParam;
CWPSTRUCT *msg = (CWPSTRUCT*)lParam;

if (msg->message == WM_SYSCOMMAND)
{
if (msg->wParam == 61472)
// Description for WM_SYSCOMMAND: https://msdn.microsoft.com/de-de/library/windows/desktop/ms646360(v=vs.85).aspx
if (msg->wParam == 0xF020 || msg->wParam == 0xF060)
{
// Ich prüfe hier noch ob der Fenstertitel übereinstimmt. Vorher hatte ich das Problem das sich Chrome auch minimiert hat.
// Ich könnte hier auch noch die klasse checken, das hat dann den vorteil, das es noch genauer ist.
Expand All @@ -57,12 +60,37 @@ LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
}

LRESULT CALLBACK CallWndRetProcDebug(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0) {
CWPRETSTRUCT *msg = (CWPRETSTRUCT*)lParam;
//if (nCode >= 0)
{
CWPSTRUCT *msg = (CWPSTRUCT*)lParam;

if (msg->hwnd == (HWND)0x10404)
//if (msg->hwnd == (HWND)0x00120A42)
if (msg->hwnd == FindWindow(NULL, WHATSAPP_CLIENT_NAME))
{
if (msg->message == WM_SYSCOMMAND)
//
if (msg->message == 0x2 || msg->message == 528 || msg->message == 70 || msg->message == 71 || msg->message == 28 || msg->message == 134 || msg->message == 6 || msg->message == 641 || msg->message == 642 || msg->message == 7 || msg->message == 533 || msg->message == 144 || msg->message == 70 || msg->message == 71 || msg->message == 134 || msg->message == 6 || msg->message == 28 || msg->message == 8 || msg->message == 641 || msg->message == 642 || msg->message == 626 || msg->message == 2)
{
// WM_DESTROY

//std::ostringstream filename2;
//filename2 << "C:/hooktest/HWND_" << msg->hwnd << ".txt";

//std::ofstream myfile;
//myfile.open(filename2.str().c_str(), std::ios::app);

////LONG wndproc = GetWindowLong(msg->hwnd, -4);

//myfile << "\nblocked (" << msg->message << ")" << msg->wParam;



//MSG msgr;
//PeekMessage(&msgr, msg->hwnd, 0, 0, PM_REMOVE);

return 0;
}

//if (msg->message == WM_SYSCOMMAND)
{
std::ostringstream filename2;
filename2 << "C:/hooktest/HWND_" << msg->hwnd << ".txt";
Expand Down Expand Up @@ -90,15 +118,96 @@ LRESULT CALLBACK CallWndRetProcDebug(int nCode, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(_hWndProc, nCode, wParam, lParam);
}

BOOL DLLIMPORT RegisterHook(HMODULE hLib)
LRESULT CALLBACK CBTProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
_hWndProc = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, hLib, 0);

if (nCode == HCBT_DESTROYWND)
{
std::ostringstream filename2;
filename2 << "C:/hooktest/HWND_" << (HWND)wParam << ".txt";

std::ofstream myfile;
myfile.open(filename2.str().c_str(), std::ios::app);

myfile << "\nblocked (" ;


return 1;
}
return CallNextHookEx(_cbtProc, nCode, wParam, lParam);
}

LRESULT CALLBACK MouseProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
if (nCode >= 0)
{
if (wParam == WM_LBUTTONDOWN)
{
//std::ostringstream filename2;
//filename2 << "C:/hooktest/HWND_" << (HWND)wParam << ".txt";

//std::ofstream myfile;
//myfile.open(filename2.str().c_str(), std::ios::app);

MOUSEHOOKSTRUCT *mhs = (MOUSEHOOKSTRUCT*)lParam;

RECT rect;
GetWindowRect(mhs->hwnd, &rect);

// Modify rect to cover the X(close) button.
rect.left = rect.right - 46;
rect.bottom = rect.top + 35;

bool mouseOnClosebutton = PtInRect(&rect, mhs->pt);

if (mouseOnClosebutton)
{
//myfile << "\nMinimize";
//myfile << "\nHWND to Hookwindow:" << FindWindow(NAME, NAME);

PostMessage(FindWindow(NAME, NAME), WM_ADDTRAY, 0, (LPARAM)mhs->hwnd);

// Returning nozero blocks the message frome being sent to the application.
return 1;
}
}
}

return CallNextHookEx(_cbtProc, nCode, wParam, lParam);
}

// Wenn ich als threadId 0 übergeben, ist es ein Globaler Hook.
BOOL DLLIMPORT RegisterHook(HMODULE hLib, DWORD threadId, bool closeToTray)
{
_hWndProc = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndRetProcDebug, hLib, threadId);
if (_hWndProc == NULL)
{
OutputDebugString(L"RegisterHook() - Error Creation Hook _hWndProc\n");
UnRegisterHook();
return FALSE;
}

if (closeToTray)
{
_mouseProc = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, hLib, threadId);
if (_mouseProc == NULL)
{
OutputDebugString(L"RegisterHook() - Error Creation Hook _hWndProc\n");
UnRegisterHook();
return FALSE;
}
}

//_cbtProc = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hLib, threadId);

return TRUE;
}

Expand All @@ -109,4 +218,10 @@ void DLLIMPORT UnRegisterHook()
UnhookWindowsHookEx(_hWndProc);
_hWndProc = NULL;
}
if (_mouseProc)
{
UnhookWindowsHookEx(_mouseProc);
_mouseProc = NULL;
}
}

9 changes: 6 additions & 3 deletions WhatsappTray/Hook.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
Expand All @@ -22,13 +22,13 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand All @@ -49,6 +49,9 @@
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">x64\</IntDir>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<Optimization>MaxSpeed</Optimization>
Expand Down
Binary file added WhatsappTray/RCa09256
Binary file not shown.
Binary file added WhatsappTray/RCb09256
Binary file not shown.
130 changes: 75 additions & 55 deletions WhatsappTray/WhatsappTray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,57 +193,57 @@ LRESULT CALLBACK HookWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
OutputDebugString(L"HookWndProc() - Message Received\n");

case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_RESTORE:
RestoreWindowFromTray(_hwndForMenu);
break;
case IDM_CLOSE:
CloseWindowFromTray(_hwndForMenu);
break;
case IDM_ABOUT:
DialogBox(_hInstance, MAKEINTRESOURCE(IDD_ABOUT), _hwndHook, (DLGPROC)AboutDlgProc);
break;
case IDM_EXIT:
SendMessage(_hwndHook, WM_DESTROY, 0, 0);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_RESTORE:
RestoreWindowFromTray(_hwndForMenu);
break;
case WM_ADDTRAY:
MinimizeWindowToTray((HWND)lParam);
case IDM_CLOSE:
CloseWindowFromTray(_hwndForMenu);
break;
case WM_REMTRAY:
RestoreWindowFromTray((HWND)lParam);
case IDM_ABOUT:
DialogBox(_hInstance, MAKEINTRESOURCE(IDD_ABOUT), _hwndHook, (DLGPROC)AboutDlgProc);
break;
case WM_REFRTRAY:
RefreshWindowInTray((HWND)lParam);
case IDM_EXIT:
SendMessage(_hwndHook, WM_DESTROY, 0, 0);
break;
case WM_TRAYCMD:
switch ((UINT)lParam) {
case NIN_SELECT:
RestoreWindowFromTray(_hwndItems[wParam]);
break;
case WM_CONTEXTMENU:
_hwndForMenu = _hwndItems[wParam];
ExecuteMenu();
break;
case WM_MOUSEMOVE:
RefreshWindowInTray(_hwndItems[wParam]);
break;
}
}
break;
case WM_ADDTRAY:
MinimizeWindowToTray((HWND)lParam);
break;
case WM_REMTRAY:
RestoreWindowFromTray((HWND)lParam);
break;
case WM_REFRTRAY:
RefreshWindowInTray((HWND)lParam);
break;
case WM_TRAYCMD:
switch ((UINT)lParam) {
case NIN_SELECT:
RestoreWindowFromTray(_hwndItems[wParam]);
break;
case WM_DESTROY:
MessageBox(NULL, L"HookWndProc() WM_DESTROY", L"WhatsappTray", MB_OK | MB_ICONINFORMATION);
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
RestoreWindowFromTray(_hwndItems[i]);
}
}
UnRegisterHook();
FreeLibrary(_hLib);
PostQuitMessage(0);
case WM_CONTEXTMENU:
_hwndForMenu = _hwndItems[wParam];
ExecuteMenu();
break;
case WM_MOUSEMOVE:
RefreshWindowInTray(_hwndItems[wParam]);
break;
}
break;
case WM_DESTROY:
MessageBox(NULL, L"HookWndProc() WM_DESTROY", L"WhatsappTray", MB_OK | MB_ICONINFORMATION);
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
RestoreWindowFromTray(_hwndItems[i]);
}
}
UnRegisterHook();
FreeLibrary(_hLib);
PostQuitMessage(0);
break;
}

return DefWindowProc(hwnd, msg, wParam, lParam);
Expand All @@ -253,6 +253,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine
WNDCLASS wc;
MSG msg;

bool closeToTray = strstr(szCmdLine, "--closeToTray");

_hInstance = hInstance;
_hwndHook = FindWindow(NAME, NAME);
if (_hwndHook) {
Expand All @@ -268,19 +270,37 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine
MessageBox(NULL, L"Error loading Hook.dll.", L"WhatsappTray", MB_OK | MB_ICONERROR);
return 0;
}
if (!RegisterHook(_hLib)) {

// Damit nicht alle Prozesse gehookt werde, verwende ich jetzt die ThreadID des WhatsApp-Clients.
HWND hwndWhatsapp = FindWindow(NULL, WHATSAPP_CLIENT_NAME);
if (hwndWhatsapp == NULL)
{
MessageBox(NULL, L"WhatsApp-Window not found.", L"WhatsappTray", MB_OK | MB_ICONERROR);
return 0;
}

DWORD threadId = GetWindowThreadProcessId(hwndWhatsapp, NULL);
if (threadId == NULL)
{
MessageBox(NULL, L"ThreadID of WhatsApp-Window not found.", L"WhatsappTray", MB_OK | MB_ICONERROR);
return 0;
}

if (RegisterHook(_hLib, threadId, closeToTray) == false)
{
MessageBox(NULL, L"Error setting hook procedure.", L"WhatsappTray", MB_OK | MB_ICONERROR);
return 0;
}
wc.style = 0;
wc.lpfnWndProc = HookWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;

wc.style = 0;
wc.lpfnWndProc = HookWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = NAME;
if (!RegisterClass(&wc)) {
MessageBox(NULL, L"Error creating window class", L"WhatsappTray", MB_OK | MB_ICONERROR);
Expand All @@ -293,7 +313,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine
for (int i = 0; i < MAXTRAYITEMS; i++) {
_hwndItems[i] = NULL;
}

while (IsWindow(_hwndHook) && GetMessage(&msg, _hwndHook, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
Expand Down
2 changes: 1 addition & 1 deletion WhatsappTray/WhatsappTray.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

#define DLLIMPORT __declspec(dllexport)

BOOL DLLIMPORT RegisterHook(HMODULE);
BOOL DLLIMPORT RegisterHook(HMODULE hLib, DWORD threadId, bool closeToTray);
void DLLIMPORT UnRegisterHook();
Loading

0 comments on commit 652e014

Please sign in to comment.