Win32 sdk 定制网格布局界面开发

xingyun86 2020-8-15 971

Win32 sdk 定制网格布局界面开发

typedef enum ChildrenControls {
	IDCC_MINIMUM = WM_USER,
	IDCC_BUTTON_TEST,
	IDCC_BUTTON_TEST2,
	IDCC_BUTTON_TEST3,
	IDCC_MAXIMUM,
};
class cc_item {
public:
	typedef enum ScaleType {
		ST_FIXED=0,
		ST_X_SCALE,
		ST_Y_SCALE,
		ST_XY_SCALE,
	};
public:
	cc_item(ScaleType sType, int w, int h, double xScale, double yScale, WNDPROC fnWndProc):
		sType(sType),w(w),h(h),xScale(xScale),yScale(yScale),fnWndProc(fnWndProc)
	{
	}
public:
	ScaleType sType;
	int w;
	int h;
	int xExclude;//当前行的所有固定项宽度
	int yExclude;//当前列的所有固定项高度
	int xN;//有效行数
	int yN;//有效列数
	double xScale;
	double yScale;
	WNDPROC fnWndProc;
};
std::vector<std::vector<uint16_t>> cc_array;
std::unordered_map<uint16_t, cc_item> cc_umap;
HWND CreatePpsdlg(HWND hParent = NULL, LPVOID lpWndProc = NULL, BOOL bModal = TRUE, BOOL bCenter = TRUE)
{
	HWND hWnd = NULL;
	WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
	bModal &= (hParent != NULL);
	if (bModal) { EnableWindow(hParent, FALSE); }
	GetClassInfoEx(GetModuleHandle(NULL), WC_DIALOG, &wcex);
	wcex.lpszClassName = TEXT(__func__);
	wcex.lpfnWndProc = ([](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)->LRESULT
		{
			WNDPROC fnWndProc = NULL;
			switch (uMsg)
			{
			case WM_CREATE:
			{
				HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(128));
				SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
				SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
				SendMessage(hWnd, WM_SETICON, ICON_SMALL2, (LPARAM)hIcon);
				if ((lParam != NULL) && (fnWndProc = *(WNDPROC*)lParam) != NULL)
				{
					fnWndProc(hWnd, uMsg, wParam, lParam);
					SetProp(hWnd, TEXT(__func__), (HANDLE)(fnWndProc));
				}
			}
			break;
			case WM_DESTROY:
			{
				PostMessage(hWnd, WM_QUIT, (WPARAM)0, (LPARAM)0);
			}
			break;
			case WM_PAINT:
			{
				HDC hDC = nullptr;
				PAINTSTRUCT ps = { 0 };
				hDC = BeginPaint(hWnd, &ps);
				if (hDC != nullptr)
				{
					RECT rc = { 0 };
					GetClientRect(hWnd, &rc);
					FillRect(hDC, &rc, (HBRUSH)LTGRAY_BRUSH);
					EndPaint(hWnd, &ps);
				}
			}
			default:
			{
				if ((fnWndProc = (WNDPROC)GetProp(hWnd, TEXT(__func__))) != NULL )
				{
					fnWndProc(hWnd, uMsg, wParam, lParam);
				}
			}
			break;
			}
			return DefWindowProc(hWnd, uMsg, wParam, lParam);
		});
	RegisterClassEx(&wcex);
	/////////////////////////////////////////////////
	if ((hWnd = CreateWindowEx(
		WS_EX_DLGMODALFRAME,
		wcex.lpszClassName,
		wcex.lpszClassName,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		hParent,
		NULL,
		wcex.hInstance,
		lpWndProc)
		) != NULL) {
		if (bCenter == TRUE)
		{
			if (hParent != NULL)
			{
				CenterWindowInParent(hWnd, hParent);
			}
			else
			{
				CenterWindowInScreen(hWnd);
			}
		}
		ShowWindow(hWnd, SW_SHOW);
		UpdateWindow(hWnd);
		while (1)
		{
			MSG msg = { 0 };
			if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				if (msg.message == WM_QUIT)
				{
					break;
				}
				else
				{
					if (bModal && (msg.hwnd != hWnd))
					{
						continue;
					}
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
			}
			else
			{
				// 完成某些工作的其他行程式 
			}
		}
	}
	if (bModal) { EnableWindow(hParent, TRUE); SetForegroundWindow(hParent); }
	return 0;
}
LRESULT CALLBACK HandleButtonTestWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CreatePpsdlg(hWnd, NULL, TRUE);
	return 0;
}
#define ROW_NUM 3
#define COL_NUM 3
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CREATE:
	{
		for (auto row = 0; row < ROW_NUM; row++)
		{
			cc_array.push_back({});
			for (auto col = 0; col < COL_NUM; col++)
			{
				cc_array[row].push_back(0);
			}
		}
		int nControlId = IDCC_BUTTON_TEST;
		cc_umap.emplace(nControlId, cc_item(cc_item::ST_XY_SCALE, 60, 40, 0.5, 0.5, HandleButtonTestWndProc));
		CreateWindowEx(0,
			WC_BUTTON,
			TEXT("test"),
			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
			0,
			0,
			cc_umap.at(nControlId).w,
			cc_umap.at(nControlId).h,
			hWnd,
			(HMENU)nControlId,
			NULL,
			NULL);
		cc_array[0][0] = nControlId;
		nControlId = IDCC_BUTTON_TEST2;
		cc_umap.emplace(nControlId, cc_item(cc_item::ST_XY_SCALE, 60, 40, 0.5, 0.5, HandleButtonTestWndProc));
		CreateWindowEx(0,
			WC_BUTTON,
			TEXT("test"),
			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
			0,
			0,
			cc_umap.at(nControlId).w,
			cc_umap.at(nControlId).h,
			hWnd,
			(HMENU)nControlId,
			NULL,
			NULL);
		cc_array[1][0] = nControlId;
		nControlId = IDCC_BUTTON_TEST3;
		cc_umap.emplace(nControlId, cc_item(cc_item::ST_XY_SCALE, 60, 40, 0.5, 0.5, HandleButtonTestWndProc));
		CreateWindowEx(0,
			WC_BUTTON,
			TEXT("test"),
			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
			0,
			0,
			cc_umap.at(nControlId).w,
			cc_umap.at(nControlId).h,
			hWnd,
			(HMENU)nControlId,
			NULL,
			NULL);
		cc_array[1][1] = nControlId;
		{
			//行运算
			auto rows = cc_array;
			auto cols = rows.at(0);
			int xN = 0;
			int yN = 0;
			for (auto row = 0; row < rows.size(); row++)
			{
				bool bFound = false;
				int xExclude = 0;
				for (auto col = 0; col < cols.size(); col++)
				{
					auto item = rows.at(row).at(col);
					if (cc_umap.find(item) != cc_umap.end())
					{
						bFound = true;
						if (cc_umap.at(item).sType == cc_item::ST_FIXED)
						{
							xExclude += cc_umap.at(item).w;
						}
					}
				}
				if (bFound > 0)
				{
					yN++;
				}
				//修正
				for (auto col = 0; col < cols.size(); col++)
				{
					auto item = rows.at(row).at(col);
					if (cc_umap.find(item) != cc_umap.end())
					{
						if (cc_umap.at(item).sType != cc_item::ST_FIXED)
						{
							cc_umap.at(item).xExclude = xExclude;
						}
					}
				}
			}
			//列运算
			for (auto col = 0; col < cols.size(); col++)
			{
				bool bFound = false;
				int yExclude = 0;
				for (auto row = 0; row < rows.size(); row++)
				{
					auto item = rows.at(row).at(col);				
					if (cc_umap.find(item) != cc_umap.end())
					{
						bFound = true;
						if (cc_umap.at(item).sType == cc_item::ST_FIXED)
						{
							yExclude += cc_umap.at(item).h;
						}
					}
				}
				if (bFound > 0)
				{
					xN ++;
				}
				//修正
				for (auto row = 0; row < rows.size(); row++)
				{
					auto item = rows.at(row).at(col);
					if (cc_umap.find(item) != cc_umap.end())
					{
						bFound = true;
						if (cc_umap.at(item).sType != cc_item::ST_FIXED)
						{
							cc_umap.at(item).yExclude = yExclude;
						}
					}
				}
			}
			//优化数值
			for (auto col = 0; col < cols.size(); col++)
			{
				for (auto row = 0; row < rows.size(); row++)
				{
					auto item = rows.at(row).at(col);
					if (cc_umap.find(item) != cc_umap.end())
					{
						cc_umap.at(item).xN = xN;
						cc_umap.at(item).yN = yN;
					}
				}
			}
		}
	}
		return 0;
	case WM_COMMAND:
	{
		if (cc_umap.find(LOWORD(wParam)) != cc_umap.end()) {
			cc_umap.at(LOWORD(wParam)).fnWndProc(hWnd, uMsg, wParam, lParam);
		}
	}
	break;
	case WM_SIZE:
	{
		RECT rcWnd = { 0 };
		GetClientRect(hWnd, &rcWnd);
		int left = 0;
		int top = 0;
		auto rows = cc_array;
		for (auto row = 0; row < rows.size(); row++)
		{
			auto cols = rows.at(row);
			uint16_t uItem = 0;
			left = 0;
			for (auto col = 0; col < cols.size(); col++)
			{
				auto item = cols.at(col);
				//存在控件
				if (item > 0)
				{
					cc_item* p_cc_item = &cc_umap.at(item);
					if (p_cc_item->sType == cc_item::ST_FIXED)
					{
						MoveWindow(GetDlgItem(hWnd, item), left, top, p_cc_item->w, p_cc_item->h, TRUE);
						left += p_cc_item->w;
						uItem = p_cc_item->h;
					}
					else if (p_cc_item->sType == cc_item::ST_X_SCALE)
					{
						p_cc_item->w = (rcWnd.right - rcWnd.left - p_cc_item->xExclude) * p_cc_item->xScale;
						MoveWindow(GetDlgItem(hWnd, item), left, top, p_cc_item->w, p_cc_item->h, TRUE);
						left += p_cc_item->w;
						uItem = p_cc_item->h;
					}
					else if (p_cc_item->sType == cc_item::ST_Y_SCALE)
					{
						p_cc_item->h = (rcWnd.bottom - rcWnd.top - p_cc_item->yExclude) * p_cc_item->yScale;
						MoveWindow(GetDlgItem(hWnd, item), left, top, p_cc_item->w, p_cc_item->h, TRUE);
						left += p_cc_item->w;
						uItem = p_cc_item->h;
					}
					else if (p_cc_item->sType == cc_item::ST_XY_SCALE)
					{
						p_cc_item->w = (rcWnd.right - rcWnd.left - p_cc_item->xExclude) * p_cc_item->xScale;
						p_cc_item->h = (rcWnd.bottom - rcWnd.top - p_cc_item->yExclude) * p_cc_item->yScale;
						MoveWindow(GetDlgItem(hWnd, item), left, top, p_cc_item->w, p_cc_item->h, TRUE);
						left += p_cc_item->w;
						uItem = p_cc_item->h;
					}
				}
			}
			top += uItem;
		}
		/*int row = 0;
		int col = 0;
		int xLeft = 0;
		int yLeft = 0;
		int xTop = 0;
		int yTop = 0;
		row = 0;
		col = 0;
		if (cc_array[row][col] > 0)
		{//存在控件
			cc_item* p_cc_item = &cc_umap.at(cc_array[row][col]);
			if (p_cc_item->sType == cc_item::ST_FIXED)
			{
				MoveWindow(GetDlgItem(hWnd, cc_array[row][col]), xLeft, xTop, p_cc_item->w, p_cc_item->h, TRUE);
				xLeft += p_cc_item->w;
				yTop += p_cc_item->h;
			}
			else
			{
				//查找两外两行是否存在控件以确定高度
			}
		}
		row = 0;
		col = 1;
		if (cc_array[row][col] > 0)
		{//存在控件
			cc_item* p_cc_item = &cc_umap.at(cc_array[row][col]);
			if (p_cc_item->sType == cc_item::ST_FIXED)
			{
				MoveWindow(GetDlgItem(hWnd, cc_array[row][col]), xLeft, xTop, p_cc_item->w, p_cc_item->h, TRUE);
				xLeft += p_cc_item->w;
				xTop += p_cc_item->h;
			}
			else
			{
				//查找两外两行是否存在控件以确定高度
			}
		}
		row = 1;
		col = 0;
		if (cc_array[row][col] > 0)
		{//存在控件
			cc_item* p_cc_item = &cc_umap.at(cc_array[row][col]);
			if (p_cc_item->sType == cc_item::ST_FIXED)
			{
				MoveWindow(GetDlgItem(hWnd, cc_array[row][col]), yLeft, yTop, p_cc_item->w, p_cc_item->h, TRUE);
				yLeft += p_cc_item->w;
			}
			else
			{
				//查找两外两行是否存在控件以确定高度
			}
		}
		row = 1;
		col = 1;
		if (cc_array[row][col] > 0)
		{//存在控件
			cc_item* p_cc_item = &cc_umap.at(cc_array[row][col]);
			if (p_cc_item->sType == cc_item::ST_FIXED)
			{
				MoveWindow(GetDlgItem(hWnd, cc_array[row][col]), yLeft, yTop, p_cc_item->w, p_cc_item->h, TRUE);
				yLeft += p_cc_item->w;
			}
			else
			{
				//查找两外两行是否存在控件以确定高度
			}
		}*/
	}
	break;
	case WM_PAINT:
	{
		HDC hDC = nullptr;
		PAINTSTRUCT ps = { 0 };
		hDC = BeginPaint(hWnd, &ps);
		if (hDC != nullptr)
		{
			RECT rc = { 0 };
			GetClientRect(hWnd, &rc);
			FillRect(hDC, &rc, (HBRUSH)LTGRAY_BRUSH);
			TextOut(hDC, 0, 0, TEXT("Hello world!"), lstrlen(TEXT("Hello world!")));
			TextOut(hDC, 32, 32, TEXT("Win32 Api!"), lstrlen(TEXT("Win32 Api!")));
			EndPaint(hWnd, &ps);
		}
	}
		break;
	}
	return 0;
}
__inline static
DWORD WINAPI ThreadStartRoutine(LPVOID lpThreadParameter)
{
    UNREFERENCED_PARAMETER(lpThreadParameter);
	//PpsNewUI(GetModuleHandle(NULL));
	CreatePpsdlg(NULL, WndProc);
	return 0;
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回