//---------------------------------------------------------------------//
//                                                                     //
//  code1.cpp                                                          //
//  bkenwright@xbdev.net                                               //
//  www.xbdev.net                                                      //
//                                                                     //
//---------------------------------------------------------------------//


#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "Winmm.lib")


#include <windows.h>
#include <stdio.h>                                // used for sprintf(..)

// Give our window a name, and of course name its class.
const char *CLASSNAME = "Tut Win32 - xbdev.net", *WINNAME = "Fonts are easy";

LRESULT CALLBACK WndProc(HWND hWnd, unsigned int iMessage, WPARAM wParam, LPARAM lParam);


// This function is our program entry point, but this time its got a lot
// of Windows initilisation code :-/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
	HWND hWnd;
	MSG Message;
    WNDCLASS WndClass;
    ZeroMemory(&WndClass, sizeof(WndClass));
    
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    // Casting, e.g. (HBRUSH), is for the compilers benefit, so that
	// it doesn't get unhappy, and to tell it that where passing a paramter
	// but we want it passes as a HBRUSH or what ever we've cast it to.
	WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hIcon = LoadIcon(hInstance, NULL);
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = WndProc;
	WndClass.lpszClassName = CLASSNAME;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	if(!RegisterClass(&WndClass))
		return 0;
        
	hWnd = CreateWindow(CLASSNAME, WINNAME, WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
						NULL, NULL, hInstance, NULL);

	ShowWindow(hWnd, nCmdShow);
    
	// This is our windows messaging loop!
	while(GetMessage(&Message, hWnd, 0, 0))
	{
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}
	return Message.wParam;
}


/* Details for TextOut(..) from the ms docs.
	BOOL TextOut(
		HDC hdc,           // handle to DC
		int nXStart,       // x-coordinate of starting position
		int nYStart,       // y-coordinate of starting position
		LPCTSTR lpString,  // character string
		int cbString       // number of characters
	);
*/

// This is our Call Back procedure, where you would put your code...this gets
// called repeatedly from our windows messaging loop.
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	switch(iMessage)
	{
	case WM_MOUSEMOVE:
		{
			// NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
			HDC hDC = GetDC(hWnd);
			int xp = LOWORD(lParam);
			int yp = HIWORD(lParam);
			// Use the default Font to display our mouse position on screen
			char buf[200];           // Temp text buffer
			sprintf(buf, "xpos:%d, ypos:%d", xp, yp);


			//       +--- HDC, which is our screen drawing canvas
			//       |   +---+-- x and y position..where 0,0 is the top left
			//       |   |   |   +--- Our string of char's
			//       |   |   |   |       +------The number of characters to display
			//      \|/ \|/ \|/ \|/     \|/
			//       |   |   |   |       |
			TextOut(hDC, 5,  5, buf, strlen(buf));
			ReleaseDC(hWnd, hDC);
		}
		break;
	case WM_CLOSE:
		if(MessageBox(hWnd, "Leave? Quit the our mini window?", "Message", MB_YESNO) == IDYES)
			PostQuitMessage(0);
		
		break;
	default:
		return DefWindowProc(hWnd, iMessage, wParam, lParam);
	}
	return 0;
}



