View Single Post
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