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

HINSTANCE InstanceHandle;
HWND      MainWindow;

int manx, many;

void Move(int mousex, int mousey)
{
  HDC dc = GetDC(MainWindow);
  if(abs(manx - mousex) > abs(many - mousey))
  {
     double y = many;

     double dy = (((double )many - mousey)/((double )manx - mousex));
     int x;
     if(manx < mousex)
     {
        for(x = manx; x < mousex; x++)
        {
           SetPixel(dc, x, (int )y, RGB(0, 0, 255));
           y += dy;
        }
     }
     else
     {
        for(x = manx; x > mousex; x--)
        {
           SetPixel(dc, x, (int )y, RGB(0, 0, 255));
           y -= dy;
        }
     }
  }
  else
  {
     double x = manx;
     double dx = (((double )many - mousey)/((double )manx - mousex));
     int y;
     if(many < mousey)
     {
        for(y = many; y < mousey; y++)
        {
           SetPixel(dc, (int )x, y, RGB(0, 0, 255));
           x += dx;
        }
     }
     else
     {
        for(y = many; y > mousey; y--)
        {
           SetPixel(dc, (int )x, y, RGB(0, 0, 255));
           x -= dx;
        }
     }
  }
  ReleaseDC(MainWindow, dc);
}

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
    case WM_DESTROY:
       PostQuitMessage(0);
       break;
    case WM_LBUTTONDOWN:
       manx = LOWORD(lParam);
       many = HIWORD(lParam);
       break;
    case WM_LBUTTONUP:
       Move(LOWORD(lParam), HIWORD(lParam));
       break;
    default:
       return DefWindowProc(hwnd,msg,wParam,lParam);
  }
  return 0;
}

HWND CreateMainWindow()
{
   WNDCLASS wc;
   memset(&wc, 0, sizeof(WNDCLASS));
   wc.style = CS_HREDRAW | CS_VREDRAW  | CS_DBLCLKS ;
   wc.lpfnWndProc = (WNDPROC )MainWndProc;
   wc.hInstance = InstanceHandle;
   wc.hbrBackground = (HBRUSH )(COLOR_WINDOW + 1);
   wc.lpszClassName = "SimpleWinWndClass";
   wc.lpszMenuName = 0;
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   if(!RegisterClass(&wc))
      return 0;

   return CreateWindow("SimpleWinWndClass",
                       "Simple-Window",
                       WS_MINIMIZEBOX | WS_VISIBLE |
                       WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX |
                       WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
                       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
                       0,
                       0,
                       InstanceHandle,
                       0);
}

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

   if((MainWindow = CreateMainWindow()) == (HWND )0)
   {
      MessageBox(0, "Failed to create MainWindow!", "Warning", MB_OK);
      return 0;
   }
   ShowWindow(MainWindow, SW_SHOW);
   MSG Msg;
   while(GetMessage(&Msg, 0, 0, 0))
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
   return Msg.wParam;
}