Click here to Skip to main content
15,887,280 members
Articles / Programming Languages / C++

Middle Mouse Button (or Wheel) to Doubleclick (VC6)

Rate me:
Please Sign up or sign in to vote.
4.67/5 (17 votes)
26 Jul 2010CPOL2 min read 83.8K   2K   34  
This is a small but handy tool I'm using every day. It converts a middle mouse button click in to a left mouse button double click.
// MouseHook.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#define _COMPILING_44E531B1_14D3_11d5_A025_006067718D04
#include "MouseHook.h"

#pragma data_seg(".JET")
HWND hWndServer = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.JET,rws")

HINSTANCE hInst;
UINT UWM_MBTNCLICK;
HHOOK hook;
static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam);

BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                       DWORD  Reason, 
                       LPVOID Reserved
					 )
{
 switch(Reason)
    { /* reason */
     case DLL_PROCESS_ATTACH:
	hInst = hInstance;
	UWM_MBTNCLICK = RegisterWindowMessage(UWM_MBTNCLICK_MSG);
	return TRUE;
     case DLL_PROCESS_DETACH:
	if(hWndServer != NULL)
	   clearMyHook(hWndServer);
	return TRUE;
    } /* reason */
    return TRUE;
}


/****************************************************************************
*                                 setMyHook
* Inputs:
*       HWND hWnd: Window to notify
* Result: BOOL
*       TRUE if successful
*	FALSE if error
* Effect: 
*       Sets the hook
****************************************************************************/

__declspec(dllexport) BOOL setMyHook(HWND hWnd) {
	if(hWndServer != NULL)
		return FALSE; // already hooked!
    
	hook = SetWindowsHookEx(WH_MOUSE_LL,
			    (HOOKPROC)msghook,
			    hInst,
			    0);
    if(hook != NULL) { /* success */
		hWndServer = hWnd;
		return TRUE;
	} /* success */
    return FALSE; // failed to set hook
} // setMyHook

/****************************************************************************
*                                 clearMyHook
* Inputs:
*       HWND hWnd: Window hook
* Result: BOOL
*       TRUE if successful
*	FALSE if error
* Effect: 
*       Removes the hook that has been set
****************************************************************************/

__declspec(dllexport) BOOL clearMyHook(HWND hWnd) {
	if(hWnd != hWndServer || hWnd == NULL)
		return FALSE;
    
	BOOL unhooked = UnhookWindowsHookEx(hook);
    
	if(unhooked)
		hWndServer = NULL;

    return unhooked;
} // clearMyHook

/****************************************************************************
*                                   msghook
* Inputs:
*       int nCode: Code value
*	WPARAM wParam:
*	LPARAM lParam:
* Result: LRESULT
*       Either 0 or the result of CallNextHookEx
* Effect: 
*       Hook processing function. If the message is a mouse-move message,
*	posts the coordinates to the parent window
****************************************************************************/

static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam) {
	if(nCode < 0) { /* pass it on */
		CallNextHookEx(hook, nCode, wParam, lParam);
		return 0;
	} /* pass it on */

	if(wParam == WM_MBUTTONUP ){
		PostMessage(hWndServer, UWM_MBTNCLICK, 0, 0);
	}

    return CallNextHookEx(hook, nCode, wParam, lParam);
} // msghook

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
programmer and software junkie since 1991 zurich switzerland

Comments and Discussions