/* The original version of this program can be found at http://damb.dk */
#include <windows.h>

#define IDC_BUTTON1  1200
#define IDC_STATIC_1 1201

HINSTANCE InstanceHandle;
int NumPush = 0;
bool IsTransparent;

LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
   {
   case WM_CREATE:
      CreateWindow("STATIC",
                   "0", // Initial Text
                   WS_CHILD | WS_VISIBLE | SS_LEFT,  // Style
                   5, 5, 100, 20, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_STATIC_1), // ID
                   InstanceHandle,  // The application
                   0);

      CreateWindow("BUTTON",
                   "Push Me",                        // Button Text
                   WS_CHILD | WS_VISIBLE,  // Style
                   5, 30, 100, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(IDC_BUTTON1), // ID
                   InstanceHandle,  // The application
                   0);

      SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_ASYNCWINDOWPOS);
      break;
   case WM_ACTIVATEAPP:
   {
      const LONG Style = WS_EX_LAYERED | WS_EX_TRANSPARENT;
      if(wParam)
      {  // We are activating, so remove the styles
         LONG OldState = GetWindowLong(hwndDlg, GWL_EXSTYLE);
         LONG NewState = OldState & ~Style;
         SetWindowLong(hwndDlg, GWL_EXSTYLE, NewState);
      }
      else
      {  // We are deactivating, so use the style, if set
         if(IsTransparent)
         {
            LONG OldState = GetWindowLong(hwndDlg, GWL_EXSTYLE);
            LONG NewState = OldState | Style;
            SetWindowLong(hwndDlg, GWL_EXSTYLE, NewState);
            SetLayeredWindowAttributes(hwndDlg, 0, 160, LWA_ALPHA);
         }
      }
      break;
   }
   case WM_COMMAND:
      if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDC_BUTTON1)
      {
         if(IsTransparent)
         {
            IsTransparent = false;
            SetDlgItemText(hwndDlg, IDC_STATIC_1, "Off");
            SetWindowPos(hwndDlg, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
         }
         else
         {
            IsTransparent = true;
            SetDlgItemText(hwndDlg, IDC_STATIC_1, "On");
            SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
         }
      }
      break;
   }
   return DefWindowProc(hwndDlg, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, INT nCmdShow)
{
   InstanceHandle = hInstance;

   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
   wc.lpfnWndProc = DialogProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
   wc.lpszClassName = "WhateverClass";
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   if(!RegisterClass(&wc))
      return FALSE;

   HWND WindowHandle = CreateWindow("WhateverClass",
                                    "Whatever", // Caption text
                                    WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
                                    WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
                                    100, 100, 150, 100,  // Position
                                    NULL,
                                    NULL,
                                    InstanceHandle,
                                    0);
   MSG Msg;
   while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
   {
      if(!IsDialogMessage(WindowHandle, &Msg))
      {
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   }
   return 0;
}