// TestWin.cpp : 定义应用程序的入口点。
//
#define  _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <WinUser.h>
//#include <stdio.h>
#include "TestWin.h"
//#include "fileapi.h"
//#include <string>
//#include <Richedit.h>
#define ID 123
void WriteFile(const char* str) {
	//FILE* file = fopen("d:\\d.txt", "a+");

	//fprintf(file, str);
	//fprintf(file, "\n");
	//fclose(file);
}
HINSTANCE thisInstance;
HWND btnHwnd = NULL;//按钮控件
HWND staticHwnd = NULL;//文本控件
HWND editHwnd = NULL;//编辑框控件
HWND listBoxHwnd = NULL;//列表框控件
HWND comboBoxHwnd = NULL;//组合框控件
HWND scrollbarHwnd = NULL;//滚动条控件
HWND rickEditHwnd = NULL;//富文本编辑器
HWND menuHwnd = NULL;//菜单控件
WNDPROC oldProc;
LRESULT CALLBACK NewWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message)
	{
	case WM_LBUTTONDOWN: {
		MessageBox(NULL, L"我拦截了消息", L"提示", MB_OK);
		break;
	}
					   return CallWindowProc(oldProc, hwnd, message, wParam, lParam);
	}
}
//创建按钮控件
void CreateBtn(HWND parent) {
	btnHwnd = CreateWindow(L"button",
		L"按钮控件",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP,
		0, 0,
		200, 200,
		parent,
		(HMENU)111,
		thisInstance,
		NULL
	);
	HANDLE hBmp = LoadImage(NULL, L"d:\\a.bmp", IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE);
	SendMessage(btnHwnd, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);//设置按钮的背景图像
}
//创建文本控件
void CreateStatic(HWND parent) {
	staticHwnd = CreateWindow(L"static",
		L"我是文本",
		WS_CHILD | WS_VISIBLE | SS_SIMPLE | SS_NOTIFY,
		200, 400,
		200, 200,
		parent,
		(HMENU)222,
		thisInstance,
		NULL);
	SetWindowText(staticHwnd, L"我是曹瑞鹏");//设置控件文本
	HANDLE hBmp = LoadImage(NULL, L"d:\\a.bmp", IMAGE_BITMAP, 200, 200, LR_LOADFROMFILE);
	SendMessage(staticHwnd, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);//设置按钮的背景图像
}
//创建编辑框
void CreateEdit(HWND parent) {
	editHwnd = CreateWindow(L"edit",//窗口类名称
		L"测试编辑框测试编辑框测试编辑框",//窗口标题
		WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_NOHIDESEL,
		0, 0,//相对父窗体的左边
		200, 200,//窗体的宽高
		parent,//父窗体句柄
		(HMENU)123,//控件的唯一ID
		thisInstance, //实例句柄
		NULL);
	//SendMessage(editHwnd, EM_SETSEL, (WPARAM)2, (LPARAM)6);//选中文本
	//TCHAR buff[64];
	//GetWindowText(NULL, buff, 64);
	oldProc = (WNDPROC)SetWindowLong(editHwnd, (-4), (LONG)NewWndProc);

}
//创建列表框控件
void CreateListBox(HWND parent) {
	listBoxHwnd = CreateWindow(L"listbox",
		L"列表控件",
		WS_CHILD | WS_VISIBLE | LBS_NOTIFY,
		30, 100,
		200, 100,
		parent,
		(HMENU)124,
		thisInstance,
		NULL);
	SendMessage(listBoxHwnd, LB_ADDSTRING, 0, (LPARAM)L"音乐");
	SendMessage(listBoxHwnd, LB_ADDSTRING, 0, (LPARAM)L"足球");
	SendMessage(listBoxHwnd, LB_INSERTSTRING, 1, (LPARAM)L"编程");
}
//创建组合框
void CreateComboBox(HWND parent) {
	comboBoxHwnd = CreateWindow(L"combobox",
		L"组合框控件",
		WS_CHILD | WS_VISIBLE | CBS_SIMPLE,
		30, 100,
		200, 100,
		parent,
		(HMENU)125,
		thisInstance,
		NULL);
	SendMessage(comboBoxHwnd, CB_ADDSTRING, 0, (LPARAM)L"音乐");
	SendMessage(comboBoxHwnd, CB_ADDSTRING, 0, (LPARAM)L"足球");
	SendMessage(comboBoxHwnd, CB_INSERTSTRING, 1, (LPARAM)L"编程");

	SendMessage(comboBoxHwnd, CB_SETCURSEL, 2, 0);//选中某一项
}
//创建滚动条控件
void CreateScrollBar(HWND parent) {
	scrollbarHwnd = CreateWindow(L"scrollbar",
		L"滚动条",
		WS_CHILD | WS_VISIBLE | SBS_TOPALIGN,
		30, 100,
		200, 100,
		parent,
		(HMENU)126,
		thisInstance,
		NULL);
	SendMessage(scrollbarHwnd, SBM_SETRANGE, 0, 100);//设置滚动条范围
	SendMessage(scrollbarHwnd, SBM_SETPOS, 40, 1);//设置滚动条当前位置
}
////创建富文本编辑框
//void CreateRichEdit(HWND parent) {
//	LoadLibrary(L"Msftedit.dll");//加载富文本动态库
//	rickEditHwnd = CreateWindow(MSFTEDIT_CLASS,
//		L"这是一个富文本编辑框",
//		WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL,
//		50, 30,
//		200, 200,
//		parent,
//		(HMENU)127,
//		thisInstance,
//		NULL);
//}
void PaintText(HWND hwnd) {
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hwnd, &ps);
	TextOut(hdc, 0, 0, L"你好啊", lstrlen(L"你好啊"));
	EndPaint(hwnd, &ps);
}
void PaintText2(HWND hwnd) {
	HDC hdc = GetDC(hwnd);
	TextOut(hdc, 0, 0, L"你好啊", lstrlen(L"你好啊"));
	ReleaseDC(hwnd, hdc);
}
void PaintText3(HWND hwnd) {
	HDC hdc = GetWindowDC(hwnd);
	RECT r = { 0,0,400,10 };
	HBRUSH brh = CreateSolidBrush(RGB(255, 0, 0));
	FillRect(hdc, &r, brh);
	DeleteObject(brh);
	ReleaseDC(hwnd, hdc);
}
//绘图API
void PaintApi(HWND hwnd) {
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hwnd, &ps);

	HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));

	MoveToEx(hdc, 0, 0, NULL);
	LineTo(hdc, 100, 100);

	HBRUSH hBsh = CreateSolidBrush(RGB(255, 0, 0));
	SelectObject(hdc, hBsh);

	RECT r = { 100,200,300,300 };
	Rectangle(hdc, r.left, r.top, r.right, r.bottom);

	Ellipse(hdc, 50, 50, 200, 300);
	Pie(hdc, 0, 0, 100, 100, 30, 30, 50, 100);

	DeleteObject(hPen);
	DeleteObject(hBsh);
	EndPaint(hwnd, &ps);
}
//绘图API2
void PaintApi2(HWND hwnd) {
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hwnd, &ps);
	SetBkColor(hdc, RGB(255, 0, 0));
	SetTextAlign(hdc, TA_LEFT);
	TextOut(hdc, 100, 100, L"你好啊", lstrlen(L"你好啊"));
	RECT r = { 100,200,300,300 };
	Rectangle(hdc, 100, 200, 300, 300);
	DrawText(hdc, L"你好啊你好看", lstrlen(L"你好啊你好看"), &r, DT_SINGLELINE | DT_VCENTER);
	EndPaint(hwnd, &ps);
}
//双缓冲技术
void PaintApi3(HWND hwnd) {
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hwnd, &ps);
	HDC hMemDC = CreateCompatibleDC(hdc);//创建内存兼容DC 1*1
	HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, 100, 100);//创建兼容位图
	SelectObject(hMemDC, hMemBmp);//将位图选入内存dc

	TextOut(hMemDC, 0, 0, L"你好啊", lstrlen(L"你好啊"));
	Rectangle(hMemDC, 50, 50, 80, 80);

	//BitBlt(hdc, 0, 0, 400, 400, hMemDC, 0, 0, SRCCOPY);
	StretchBlt(hdc, 0, 0, 400, 400, hMemDC, 0, 0, 100, 100, SRCCOPY);
	DeleteObject(hMemBmp);
	DeleteDC(hMemDC);
	EndPaint(hwnd, &ps);
}
//位图绘制
void PaintApi4(HWND hwnd) {
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint(hwnd, &ps);
	HDC hMemDC = CreateCompatibleDC(hdc);//创建和窗口hdc兼容的内存dc
	HBITMAP hBmp = (HBITMAP)LoadImage(NULL, L"d:\\a.bmp", IMAGE_BITMAP, 800, 800, LR_LOADFROMFILE);
	SelectObject(hMemDC, hBmp);
	TextOut(hMemDC, 100, 100, L"图片水印", lstrlen(L"图片水印"));
	SetStretchBltMode(hdc, COLORONCOLOR);

	BitBlt(hdc, 0, 0, 400, 400, hMemDC, 0, 0, SRCCOPY);
	//StretchBlt(hdc, 0, 0, 400, 400, hMemDC, 0, 0, 800, 800, SRCCOPY);
	DeleteObject(hBmp);
	DeleteDC(hMemDC);
	EndPaint(hwnd, &ps);
}
//创建菜单
void CreateMenuTest(HWND hwnd) {
	HMENU hTopMenu = CreateMenu();
	HMENU hPopMenu = CreatePopupMenu();
	AppendMenu(hPopMenu, MF_STRING, 200, L"新建");
	AppendMenu(hPopMenu, MF_SEPARATOR, 0, 0);
	AppendMenu(hPopMenu, MF_STRING | MF_CHECKED, 201, L"打开");

	AppendMenu(hTopMenu, MF_POPUP, (UINT)hPopMenu, L"文件");

	menuHwnd = CreateWindow(L"CAORUI",
		L"窗口",
		WS_OVERLAPPEDWINDOW,
		100, 100, 400, 400,
		NULL,
		hTopMenu,
		thisInstance,
		NULL
	);
}
LRESULT CALLBACK MyWndDef(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message)
	{
	case WM_CLOSE:
	{
		WriteFile("WM_CLOSE");
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	case WM_NCDESTROY://销毁非客户区
	{
		WriteFile("WM_NCDESTROY");

		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	case WM_DESTROY://销毁客户区
	{
		WriteFile("WM_DESTROY");
		PostQuitMessage(0);
		break;
	}

	case WM_NCCREATE:
	{
		SetWindowLongPtr(hwnd, (-21), (LONG_PTR)(0x345));
		WriteFile("WM_NCCREATE");
		CREATESTRUCT* p = (CREATESTRUCT*)lParam;
		return TRUE;
	}

	case WM_CREATE:
	{
		WriteFile("WM_CREATE");
		CREATESTRUCT* p = (CREATESTRUCT*)lParam;
		CreateBtn(hwnd);//创建按钮控件
		//CreateStatic(hwnd);//创建文本控件
		//CreateEdit(hwnd);//创建编辑框控件
		//CreateListBox(hwnd);//创建列表控件
		//CreateComboBox(hwnd);//创建组合框控件
		//CreateScrollBar(hwnd);//创建滚动条控件
		//CreateRichEdit(hwnd);//创建富文本编辑框

		return 0;
	}

	case WM_QUIT:
	{
		WriteFile("WM_QUIT");
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	case WM_PAINT:
	{
		//PaintApi4(hwnd);
		PAINTSTRUCT ps;
		BeginPaint(hwnd, &ps);
		EndPaint(hwnd, &ps);
		//CreateMenuTest(hwnd);
		return 0;
	}
	case WM_ERASEBKGND:
	{
		//WriteFile("WM_ERASEBKGND");
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	case WM_MOUSEMOVE://s 
	{
		//WriteFile("WM_MOUSEMOVE");
		return 0;
	}
	case WM_LBUTTONDOWN: {
		WriteFile("WM_LBUTTONDOWN");
		return 0;
	}
	case WM_LBUTTONDBLCLK: //左键单击
	{
		WriteFile("WM_LBUTTONDBLCLK");
		return 0;
	}
	case WM_KEYUP:
	{
		WriteFile("WM_KEYUP");
		return 0;
	}
	case WM_CHAR:
	{
		//std::string str = "WM_CHAR";
		//WriteFile(str.c_str());
		return 0;
	}
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case 111: {
			if (HIWORD(wParam) == BN_CLICKED) {
				WriteFile("BN_CLICKED");
			}
			if (HIWORD(wParam) == BN_DOUBLECLICKED)
			{
				WriteFile("BN_DOUBLECLICKED");
			}
			break;
		}
		case 222:
		{
			if (HIWORD(wParam) == STN_CLICKED) {
				WriteFile("STN_CLICKED");
			}
			break;
		}
		case 123:
		{
			if (HIWORD(wParam) == EN_CHANGE)
			{
				TCHAR buff[64];
				GetWindowText((HWND)lParam, buff, 64);
				int a = 1;
			}
			break;
		}
		case 124:
		{
			if (HIWORD(wParam) == LBN_SELCHANGE)
			{
				int index = SendMessage((HWND)lParam, LB_GETCURSEL, 1, 0);
				TCHAR buff[64] = { 0 };
				SendMessage((HWND)lParam, LB_GETTEXT, index, (LPARAM)buff);
				MessageBox(NULL, buff, L"提示", MB_OK);
			}
			break;
		}
		case 125:
		{
			if (HIWORD(wParam) == CBN_SELCHANGE)
			{
				int index = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
				TCHAR buff[64]{ 0 };
				SendMessage((HWND)lParam, CB_GETLBTEXT, index, (LPARAM)buff);
				MessageBox(NULL, buff, L"提示", MB_OK);
			}
			break;
		}
		default:
			break;
		}
	}
	case WM_CTLCOLORSTATIC: //静态控件或者只读和可编辑的编辑框在 wparam:上下文DC的句柄 lparam:子窗口(控件)的句柄
	{
		HDC hdc = (HDC)wParam;
		HWND hCtrl = (HWND)lParam;
		UINT ctrlID = GetWindowLong(hCtrl, GWL_ID);
		SetTextColor(hdc, RGB(0, 255, 0));
		SetBkColor(hdc, RGB(255, 0, 0));
		return (LRESULT)GetStockObject(BLACK_BRUSH);
	};
	case WM_CTLCOLOREDIT: //装饰编辑框控件
	{
		HDC hdc = (HDC)wParam;
		HWND hCtrl = (HWND)lParam;
		UINT ctrlID = GetWindowLong(hCtrl, GWL_ID);
		if (ctrlID == 123)
		{
			SetTextColor(hdc, RGB(255, 0, 0));
			SetBkMode(hdc, TRANSPARENT);
		}
		HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 0));
		return (LRESULT)hbr;
	}
	case WM_CTLCOLORLISTBOX:
	{
		HDC hdc = (HDC)wParam;
		HWND hCtrl = (HWND)lParam;
		UINT ctrlID = GetWindowLong(hCtrl, GWL_ID);
		SetTextColor(hdc, RGB(0, 255, 0));
		SetBkColor(hdc, RGB(255, 0, 0));
		return (LRESULT)GetStockObject(LTGRAY_BRUSH);
	}
	case WM_CTLCOLORSCROLLBAR:
	{
		HDC hdc = (HDC)wParam;
		HWND hCtrl = (HWND)lParam;
		UINT ctrlID = GetWindowLong(hCtrl, GWL_ID);
		SetBkColor(hdc, RGB(255, 0, 0));
		HBRUSH bsh = CreateSolidBrush(RGB(255, 0, 0));
		return (LRESULT)bsh;
	}
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPWSTR    lpCmdLine,
	_In_ int       nCmdShow)
{

	thisInstance = hInstance;
	//定义窗口类
	WNDCLASS wndClass; WNDCLASSEX a;
	//窗口类样式
	wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	//窗口过程函数
	wndClass.lpfnWndProc = MyWndDef;
	//窗口类无扩展
	wndClass.cbClsExtra = 1;
	//窗口实例无扩展
	wndClass.cbWndExtra = 2;
	//窗口所属实例句柄
	wndClass.hInstance = hInstance;
	//窗口最小化图标句柄
	wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	//窗口光标样式句柄
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	//窗口背景句柄 
	wndClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
	//窗口类Menu名称
	wndClass.lpszMenuName = NULL;
	//窗口类的唯一名称
	wndClass.lpszClassName = L"hello";

	if (!RegisterClass(&wndClass)) {
		MessageBox(NULL, L"窗口类注册失败!", L"提示", MB_OK);
		return FALSE;
	}

	HWND hwnd = CreateWindow(L"hello",//窗口类名称
		L"第一个窗口程序",//窗口左上角的标题
		WS_OVERLAPPEDWINDOW,//窗口风格
		CW_USEDEFAULT,//窗口左上角坐标
		CW_USEDEFAULT,
		CW_USEDEFAULT,//窗口宽高
		CW_USEDEFAULT,
		NULL,//父窗口句柄 
		NULL,//窗口菜单句柄
		hInstance,//窗口所属实例句柄
		NULL
	);

	//SetWindowPos(hwnd, NULL, 1, 1, 900, 500, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
	//显示窗口
	ShowWindow(hwnd, nCmdShow);
	//绘制用户区域
	UpdateWindow(hwnd);


	MSG msg;
	while (GetMessage(&msg, nullptr, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}