// ch1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;
HPEN hPen1,hPen2;
HBRUSH hBru1,hBru2;
HBRUSH hBru_p1,hBru_p2;
HDC hdc;
HDC mdc,mdc_tmp,mdc_buffer,mdc_clean;
HBITMAP hbmp,hbmp_tmp,cars_1,cars_2,hbmp_clean;
int sBru =HS_VERTICAL;
//int sPen=PS_SOLID;
int sPen=PS_DASH;
int x_p,y_p; //全域變數
	int x,y;
	bool m_lturndown;
	bool m_rturndown;
								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_CH1, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CH1);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
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			= LoadIcon(hInstance, (LPCTSTR)IDI_CH1);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_CH1;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
   MoveWindow(hWnd,10,10,600,450,true);
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   hPen1 = CreatePen(sPen,1,RGB(255,255,255));//白色
   hPen2 = CreatePen(sPen,1,RGB(0,255,0));//黑色
   hBru1=CreateHatchBrush(sBru,RGB(255,255,255));
   hBru2=CreateHatchBrush(sBru,RGB(0,255,0));
   hBru_p1=CreateSolidBrush(RGB(255,255,255));
   hBru_p2=CreateSolidBrush(RGB(255,0,0));
   hbmp = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE); 
   hbmp_tmp = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);
   cars_1 = (HBITMAP)LoadImage(NULL,"body1.bmp",IMAGE_BITMAP,40,60,LR_LOADFROMFILE);
   cars_2 = (HBITMAP)LoadImage(NULL,"body2.bmp",IMAGE_BITMAP,40,60,LR_LOADFROMFILE);//遮罩圖
   hbmp_clean = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);

   hdc = GetDC(hWnd);
   mdc = CreateCompatibleDC(hdc);
   mdc_tmp = CreateCompatibleDC(hdc);
   mdc_clean = CreateCompatibleDC(hdc);
   mdc_buffer=CreateCompatibleDC(hdc);
   SelectObject(mdc_tmp,hbmp_tmp);
   SelectObject(mdc_clean,hbmp_clean);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//

//****自訂繪圖函式*********************************
// 透空貼圖
void MyPaint(HDC hdc)
{
	if (x_p==0 && y_p==0)
			{
				x_p=x;
				y_p=y;
			}
		    //SelectObject(mdc,hPen1);
		    //SelectObject(mdc,hBru_p1);
        	//Ellipse(mdc,x_p-50,y_p-25,x_p+50,y_p+25);                 //畫橢圓形

			//SelectObject(mdc,hPen2);
		    //SelectObject(mdc,hBru_p2);
        	//SelectObject(mdc,hBru_p2);
		if(m_rturndown==true)BitBlt(mdc,0,0,600,450,mdc_clean,0,0,SRCCOPY);
		if(m_rturndown==true)BitBlt(mdc_tmp,0,0,600,450,mdc_clean,0,0,SRCCOPY);

    SelectObject(mdc_buffer,cars_2);
	BitBlt(mdc,x-30,y-25,60,50,mdc_buffer,0,0,SRCAND);
    SelectObject(mdc_buffer,cars_1);
	BitBlt(mdc,x-30,y-25,60,50,mdc_buffer,0,0,SRCPAINT);                //畫橢圓形
            BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY);
		if(m_lturndown==true)BitBlt(mdc_tmp,0,0,600,450,mdc,0,0,SRCCOPY);
			x_p=x;
			y_p=y;
            BitBlt(mdc,0,0,600,450,mdc_tmp,0,0,SRCCOPY);

	
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;

	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
            RECT rt;
			GetClientRect(hWnd, &rt);
			//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
    		EndPaint(hWnd, &ps);
			break;
		case WM_MOUSEMOVE:
			hdc =GetDC(hWnd);
            SelectObject(mdc,hbmp);
            m_lturndown=false;
            m_rturndown=false;
            RECT rt1;
			GetClientRect(hWnd, &rt1);
	        //DrawText(mdc, szHello, strlen(szHello), &rt1, DT_CENTER);

			y=lParam >>16;
			x=lParam & 0x0000FFFF;
			MyPaint(hdc);
			ReleaseDC(hWnd,hdc);
            break;
	    case WM_LBUTTONDOWN:
			hdc =GetDC(hWnd);
            SelectObject(mdc,hbmp);
            m_lturndown=true;
            //RECT rt1;
			//GetClientRect(hWnd, &rt1);
	        //DrawText(mdc, szHello, strlen(szHello), &rt1, DT_CENTER);

			y=lParam >>16;
			x=lParam & 0x0000FFFF;
			MyPaint(hdc);
			ReleaseDC(hWnd,hdc);
            break;
	    case WM_RBUTTONDOWN:
			hdc =GetDC(hWnd);
            SelectObject(mdc,hbmp);
            m_rturndown=true;
            //RECT rt1;
			//GetClientRect(hWnd, &rt1);
	        //DrawText(mdc, szHello, strlen(szHello), &rt1, DT_CENTER);

			y=lParam >>16;
			x=lParam & 0x0000FFFF;
			MyPaint(hdc);
			ReleaseDC(hWnd,hdc);
            break;
		case WM_DESTROY:
			PostQuitMessage(0);
			DeleteObject(hPen1);
			DeleteObject(hPen2);
            DeleteObject(hBru1);		//刪除畫刷
            DeleteObject(hBru2);		//刪除畫刷
            DeleteDC(mdc);
            DeleteObject(hbmp);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}
