Click here to Skip to main content
15,881,092 members
Articles / Multimedia / GDI+

Screen Painter

Rate me:
Please Sign up or sign in to vote.
4.55/5 (13 votes)
11 Jul 2004CPOL3 min read 85.2K   2.6K   50  
Application for painting on the screen.
/*****************************************************************************************
 *									  ScreenPainter_DLL.cpp                                   *		
 *****************************************************************************************
 *	AUTHOR      : Bahrudin Hrnjica                                                               *
 *	PROJECT     : ScreenPainter_DLL 																 *
 *	DESCRIPTION :  ScreenPainter_DLL.cpp : Defines the entry point for the DLL application.   *
 *					Hook system Keyboard Event,WH_KEYBOARD,Application will Un/Install   *
 *					this Hook DLL.If user click on a key,Post a user-defined msg to apps *
 *  -----------------------------------------------------------------------------------  *
 *	Revision History :                                                                   *
 *  08/05/2003  Bahrudin.    Initial Version.                                                *
 *	                                                                                     *
 *  -----------------------------------------------------------------------------------  *
 *								Copyright (c) 2003                       *
 ****************************************************************************************/

//PCH Header
#include "stdafx.h"

//Handle of windows which will recieve the Key Nook
HWND hWndReceiver = NULL;

//Global variables used for handling the keyboard hook
static UINT UWM_KEYDOWN; //Message ID
HHOOK hookKeyBoard;		 //Hook ID
HINSTANCE hInst;		 //Instance od the dll

//Function declaration
static LRESULT CALLBACK KeyboardHook(UINT nCode, WPARAM wParam, LPARAM lParam);
__declspec(dllexport) BOOL InstallKBHook(HWND hWnd);
__declspec(dllexport) BOOL UnInstallKBHook(HWND hWnd);


//Dll main entry function
BOOL APIENTRY DllMain( HINSTANCE  hInstance,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
	switch(ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		UWM_KEYDOWN = ::RegisterWindowMessage("UWM_KEYDOWN_MSG-0A63-49c3-9ACB-CF0068027A66");
		hInst = hInstance;
		return TRUE;
	case DLL_PROCESS_DETACH:
		return TRUE;
	}
    return TRUE;
}

// if user click on Keyboard, PostMsg to Apps
 
/*****************************************************************************************
 *	FUNCTION    : KeyboardHook(UINT nCode, WPARAM wParam, LPARAM lParam)                 *
 *---------------------------------------------------------------------------------------*
 *	DESCRIPTION : If the user clicks on Keyboard, Post message to the Application        *
 *	                                                                                     *
 *	ARGUMENT    : nCode  : Message code, depends on message                              *
 *	              wParam  : Additional Parameters                                        *
 *	              lParam  : Additional Parameter                                         *
 *	                                                                                     *
 *	RETURN      : Result of Hooking the message                                          *
 ****************************************************************************************/
static LRESULT CALLBACK KeyboardHook(UINT nCode, WPARAM wParam, LPARAM lParam)
{
	

	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == VK_ESCAPE))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);

	/*if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == VK_RETURN))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);*/

	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'C' ||wParam == 'c' ))
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
		
	
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'W' ||wParam == 'w' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'X' ||wParam == 'x' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'q' ||wParam == 'Q' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);

	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'r' ||wParam == 'R' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'g' ||wParam == 'G' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'b' ||wParam == 'B' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);
	if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == 'y' ||wParam == 'Y' ))	
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);

	if( (nCode >=0) && (nCode == HC_ACTION))
		::PostMessage(hWndReceiver,UWM_KEYDOWN,wParam,0);

	return CallNextHookEx(hookKeyBoard,nCode,wParam,lParam);
}
 
/*****************************************************************************************
 *	FUNCTION    : InstallKBHook(HWND hWnd)                                               *
 *---------------------------------------------------------------------------------------*
 *	DESCRIPTION : Main application will call this method to Hook keyboard, must "EXPORT  *
 *	                                                                                     *
 *	                                                                                     *
 *	ARGUMENT    : hWnd  : Handler of Windows which has to recieve the hook               *
 *	                                                                                     *
 *	RETURN      : True if Successful                                                     *
 ****************************************************************************************/
__declspec(dllexport) BOOL InstallKBHook(HWND hWnd)
{
	if( hWndReceiver != NULL) //If the reciever recieve the hook
		return FALSE; //reHook

	hookKeyBoard = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHook,	hInst,0);
	//If hookKeyboard instaled corectly
	if(hookKeyBoard != NULL)
	{ 
		hWndReceiver  = hWnd;
		return TRUE;
	}
	return FALSE;
} 




/*****************************************************************************************
 *	FUNCTION    : __declspec(dllexport) BOOL UnInstallKBHook(HWND hWnd)                  *
 *---------------------------------------------------------------------------------------*
 *	DESCRIPTION : Main application will call this method to UnHook keyboard              *
 *	                                                                                     *
 *	ARGUMENT    : hWnd  : Handler of Windows which has to recieve the hook               *
 *	RETURN      : True if successfull                                                    *
 ****************************************************************************************/
__declspec(dllexport) BOOL UnInstallKBHook(HWND hWnd)
{
	if((hWnd == NULL) || (hWnd != hWndReceiver))
		return FALSE;
    BOOL bUnHook = UnhookWindowsHookEx(hookKeyBoard);
	if(bUnHook)
		 hWndReceiver = NULL;

    return bUnHook;
} 

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)
Bosnia and Herzegovina Bosnia and Herzegovina
Bahrudin Hrnjica holds a Ph.D. degree in Technical Science/Engineering from University in Bihać.
Besides teaching at University, he is in the software industry for more than two decades, focusing on development technologies e.g. .NET, Visual Studio, Desktop/Web/Cloud solutions.

He works on the development and application of different ML algorithms. In the development of ML-oriented solutions and modeling, he has more than 10 years of experience. His field of interest is also the development of predictive models with the ML.NET and Keras, but also actively develop two ML-based .NET open source projects: GPdotNET-genetic programming tool and ANNdotNET - deep learning tool on .NET platform. He works in multidisciplinary teams with the mission of optimizing and selecting the ML algorithms to build ML models.

He is the author of several books, and many online articles, writes a blog at http://bhrnjica.net, regularly holds lectures at local and regional conferences, User groups and Code Camp gatherings, and is also the founder of the Bihac Developer Meetup Group. Microsoft recognizes his work and awarded him with the prestigious Microsoft MVP title for the first time in 2011, which he still holds today.

Comments and Discussions