Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2006, 5:25 PM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Toggle Hider (ToggleHide in C)

I just wrote this because I said I would here.

CTRL + ALT + h = Hide/unhide the active window.
CTRL + ALT + q = Kill ToggleHide, if there is a window hidden it will be shown.

The code is not commented, because it's fairly straight-forward.
It's released under the GPL, because I don't see anyone use this in any personal project. So just share if you modify/extend.
/*
ToggleHide 1.0 - Toggle hides the active or hidden window.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <stdio.h>
#include <windows.h>

char * className = "HotkeyHide";
char * captionName = "HotkeyHide Application";

int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
BOOL InitApplication( HINSTANCE );
BOOL InitInstance( HINSTANCE, int );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	MSG msg;

	if (!InitApplication(hInstance))
		return FALSE;

	if (!InitInstance(hInstance, nCmdShow))
		return FALSE;

	while((GetMessage(&msg, NULL, 0, 0)) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}

BOOL InitApplication( HINSTANCE hInstance )
{

	WNDCLASS WndClass;
	WndClass.style = 0;
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.lpfnWndProc = WndProc;
	WndClass.hInstance = hInstance;
	WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	WndClass.lpszMenuName = NULL;
	WndClass.lpszClassName = className;

	return RegisterClass(&WndClass);
}

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
    HWND hwnd = CreateWindow(className, captionName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 230, 50, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

    if(!hwnd)
		return FALSE;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	static ATOM hkToggleHide, hkKillSelf;
	static HWND hActiveWindow;
	static BOOL one = FALSE;
	switch(message)
	{
		case WM_CREATE:
			if(!(hkToggleHide = GlobalAddAtom("ToggleHide_hkToggleHide")))
				return 1;
			if (!(RegisterHotKey(hwnd, hkToggleHide, MOD_CONTROL | MOD_ALT, VkKeyScan('h'))))
				return 1;

			if(!(hkKillSelf = GlobalAddAtom("ToggleHide_hkKillSelf")))
				return 1;
			if (!(RegisterHotKey(hwnd, hkKillSelf, MOD_CONTROL | MOD_ALT, VkKeyScan('q'))))
				return 1;

			CreateWindow("button", "Kill App", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, 0, 0, 100, 20, hwnd, (HMENU) 1, (HINSTANCE)GetWindowLong (hwnd,GWL_HINSTANCE), NULL);
			return 0;

		case WM_COMMAND:
			if(HIWORD(wParam) == BN_CLICKED)
			{
				if(LOWORD(wParam) == 1)
				{
					SendMessage(hwnd, WM_HOTKEY, hkKillSelf, lParam);
				}
			}
			return 0;

		case WM_HOTKEY:
			if(wParam == hkToggleHide)
			{
				if(one)
					ShowWindow(hActiveWindow, SW_SHOW);
				else if((hActiveWindow = GetForegroundWindow()) != NULL)
					ShowWindow(hActiveWindow, SW_HIDE);
				one =! one;
			}
			else if(wParam == hkKillSelf)
			{
				if(hActiveWindow != NULL)
					ShowWindow(hActiveWindow, SW_SHOW);
				PostMessage(hwnd, WM_CLOSE, 0, 0);
			}
			return 0;

		case WM_CLOSE:
			DestroyWindow(hwnd);
			return 0;

		case WM_DESTROY:
			GlobalDeleteAtom(hkToggleHide);
			UnregisterHotKey(0, hkToggleHide);
			PostQuitMessage(0);
			return 0;

		default:
            return DefWindowProc(hwnd, message, wParam, lParam);
	}
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 8th, 2006, 1:00 AM   #2
brownhead
Programmer
 
Join Date: Mar 2006
Location: California
Posts: 37
Rep Power: 0 brownhead is on a distinguished road
Send a message via AIM to brownhead Send a message via MSN to brownhead
Very Nice , havn't tested because I'm unfamiliar with the programming language (I'm unfamiliar with anything non-vb oriented , and please don't make an issue about that..), not as functional as Cloak.. but thats because its not a full program (if you did make a full program I'm sure it would beat Cloak into the ground ), I don't have the option to hide the active window though... and that seems like a VERY good idea, I'm gonna put that in the next version. Great job again
brownhead is offline   Reply With Quote
Old May 8th, 2006, 4:58 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by brownhead
not as functional as Cloak
What other features does Cloak have?
Quote:
Originally Posted by brownhead
I don't have the option to hide the active window though..
You don't? Which window does it hide then?

Thanks for the feedback.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 9th, 2006, 12:05 AM   #4
brownhead
Programmer
 
Join Date: Mar 2006
Location: California
Posts: 37
Rep Power: 0 brownhead is on a distinguished road
Send a message via AIM to brownhead Send a message via MSN to brownhead
Cloak can set the translucency of a window (not hard, just another API... Cloak is a simple project, theres no denying that, it really wasn't that hard to make), the title (SetWindowText API), and a bit more advanced hotkeys..
You target windows by selecting them, not by pushing a button and whatever window you have focused on will dissappear.
G'day to ya
brownhead is offline   Reply With Quote
Old May 9th, 2006, 6:21 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Minor correction + some defines to reduce size to 2.5 KB and only one instance of the program at a time.
/*
ToggleHide 1.0 - Toggle hides the active or hidden window.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/*#define MAKE_SMALL*/
#ifdef MAKE_SMALL
#pragma comment(linker,"/ENTRY:WinMain")
#pragma comment(linker,"/MERGE:.rdata=.data")
#pragma comment(linker,"/MERGE:.text=.data")
#pragma comment(lib,"msvcrt.lib")
#define WIN32_LEAN_AND_MEAN
#endif

#include <stdio.h>
#include <windows.h>

char * className = "HotkeyHide";
char * captionName = "HotkeyHide Application";

int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
HANDLE OneInstance( LPCTSTR lpName );
BOOL InitApplication( HINSTANCE );
BOOL InitInstance( HINSTANCE, int );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	MSG msg;
	HANDLE hMutex = OneInstance("ToggleHide");

	if(!hMutex)
		return FALSE;

	if (!InitApplication(hInstance))
		return FALSE;

	if (!InitInstance(hInstance, nCmdShow))
		return FALSE;

	while((GetMessage(&msg, NULL, 0, 0)) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}

HANDLE OneInstance( LPCTSTR lpName )
{
	HANDLE hMutex = CreateMutex(NULL, TRUE, lpName);

	switch(GetLastError())
	{
		case ERROR_SUCCESS:
			break;

		case ERROR_ALREADY_EXISTS:
			hMutex = NULL;
			break;

		default:
			hMutex = NULL;
			break;
	}
	return hMutex;
}

BOOL InitApplication( HINSTANCE hInstance )
{

	WNDCLASS WndClass;
	WndClass.style = 0;
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.lpfnWndProc = WndProc;
	WndClass.hInstance = hInstance;
	WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	WndClass.lpszMenuName = NULL;
	WndClass.lpszClassName = className;

	return RegisterClass(&WndClass);
}

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
{
    HWND hwnd = CreateWindow(className, captionName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 250, 55, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

    if(!hwnd)
		return FALSE;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	static ATOM hkToggleHide, hkKillSelf;
	static HWND hActiveWindow;
	static BOOL one = FALSE;
	switch(message)
	{
		case WM_CREATE:
			if(!(hkToggleHide = GlobalAddAtom("ToggleHide_hkToggleHide")))
				return 1;
			if (!(RegisterHotKey(hwnd, hkToggleHide, MOD_CONTROL | MOD_ALT, VkKeyScan('h'))))
				return 1;

			if(!(hkKillSelf = GlobalAddAtom("ToggleHide_hkKillSelf")))
				return 1;
			if (!(RegisterHotKey(hwnd, hkKillSelf, MOD_CONTROL | MOD_ALT, VkKeyScan('q'))))
				return 1;

			CreateWindow("button", "Kill App", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, 0, 0, 100, 20, hwnd, (HMENU) 1, (HINSTANCE)GetWindowLong (hwnd,GWL_HINSTANCE), NULL);
			return 0;

		case WM_COMMAND:
			if(HIWORD(wParam) == BN_CLICKED)
			{
				if(LOWORD(wParam) == 1)
				{
					SendMessage(hwnd, WM_HOTKEY, hkKillSelf, lParam);
				}
			}
			return 0;

		case WM_HOTKEY:
			if(wParam == hkToggleHide)
			{
				if(one)
				{
					ShowWindow(hActiveWindow, SW_SHOW);
					one =! one;
				}
				else if((hActiveWindow = GetForegroundWindow()) != NULL)
				{
					ShowWindow(hActiveWindow, SW_HIDE);
					one =! one;
				}
			}
			else if(wParam == hkKillSelf)
			{
				if(hActiveWindow != NULL)
					ShowWindow(hActiveWindow, SW_SHOW);
				PostMessage(hwnd, WM_CLOSE, 0, 0);
			}
			return 0;

		case WM_CLOSE:
			DestroyWindow(hwnd);
			return 0;

		case WM_DESTROY:
			GlobalDeleteAtom(hkToggleHide);
			UnregisterHotKey(0, hkToggleHide);
			PostQuitMessage(0);
			return 0;

		default:
            return DefWindowProc(hwnd, message, wParam, lParam);
	}
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC