//==============================================================================
//
// KeyMouse.cpp 
//
// Template for OS Windows application with key/mouse messages processing
//
// Developer: Henry Guennadi Levkine
//
// Version 0.04
//
//==============================================================================

#include <windows.h>

//-----------------------------------------------------------------------------
// message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  HDC hDc;

  RECT rect;

  static int nXCoord;
  static int nYCoord;

  switch (message) 
  {
	  //-----------------------------------
    case WM_KEYDOWN:
      if(wParam == VK_LEFT)       Beep(100,100);
      else if(wParam == VK_RIGHT) Beep(500,100);

      break;

	  //-----------------------------------
    case WM_CHAR:
      if(wParam == 'A') Beep(100,100);
      if(wParam == 'a') Beep(500,100); 

      break;

	  //-----------------------------------
    case WM_LBUTTONDOWN:
      nXCoord = LOWORD(lParam);
      nYCoord = HIWORD(lParam);

      GetClientRect(hWnd, &rect);
      InvalidateRect(hWnd, &rect, FALSE);
      UpdateWindow(hWnd);
      
      break;

	  //-----------------------------------
    case WM_PAINT:
      hDc = BeginPaint(hWnd, &ps);
      SetPixel(hDc, nXCoord, nYCoord,RGB(255,0,0));
      SetPixel(hDc, nXCoord+1, nYCoord,RGB(255,0,0));
      SetPixel(hDc, nXCoord, nYCoord+1,RGB(255,0,0));
      SetPixel(hDc, nXCoord+1, nYCoord+1,RGB(255,0,0));
      EndPaint(hWnd, &ps);
      break;

    //-----------------------------------
    case WM_DESTROY:
      PostQuitMessage(0);
      break;

    //-----------------------------------
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

//-----------------------------------------------------------------------------
// class registration
//-----------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
  WNDCLASSEX wcex;

  wcex.cbSize = sizeof(WNDCLASSEX); 

  wcex.style        = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc  = (WNDPROC)WndProc;
  wcex.cbClsExtra   = 0;
  wcex.cbWndExtra   = 0;
  wcex.hInstance    = hInstance;
  wcex.hIcon        = NULL;
  wcex.hCursor      = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszMenuName = NULL;
  wcex.lpszClassName= "KMClass";
  wcex.hIconSm      = NULL;

  return RegisterClassEx(&wcex); 
}

//-----------------------------------------------------------------------------
// create and show application window
//-----------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND hWnd;

  hWnd = CreateWindow("KMClass", 
                      "DemoApplication", 
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
                      NULL, 
                      NULL, 
                      hInstance, 
                      NULL);
  if(!hWnd)
  {
    return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  MSG msg;

  MyRegisterClass(hInstance);

  if(!InitInstance(hInstance, nCmdShow))
  {
    return FALSE;
  }

  // Main message loop:
  while(GetMessage(&msg, NULL, 0, 0)) 
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return msg.wParam;
}







