Win32无边框窗口

窗口是透明的,支持拖动或调整大小,请用Direct3D12和DirectComposition进行绘制。

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CREATE:
		//OnInit(hWnd);
		break;
	case WM_PAINT:
		//OnRender();
		break;
	case WM_NCCALCSIZE:
		if (wParam)
		{
			if (IsZoomed(hWnd))//最大化时修正客户区
			{
				NCCALCSIZE_PARAMS* lp = (LPNCCALCSIZE_PARAMS)lParam;
				lp->rgrc[0].left += 8;
				lp->rgrc[0].top += 8;
				lp->rgrc[0].right -= 8;
				lp->rgrc[0].bottom -= 8;
			}
		}
		return 0;//去除非客户区
	case WM_NCHITTEST:
		if (!IsZoomed(hWnd))//调整窗口大小
		{
			int x = 8;//边框宽度
			RECT rc; GetClientRect(hWnd, &rc);//客户区矩形
			POINT pt; GetCursorPos(&pt); ScreenToClient(hWnd, &pt);//鼠标位置
			if (pt.x < rc.left + x)
			{
				if (pt.y < rc.top + x)return HTTOPLEFT;
				if (pt.y >= rc.bottom - x)return HTBOTTOMLEFT;
				return HTLEFT;
			}
			if (pt.x >= rc.right - x)
			{
				if (pt.y < rc.top + x)return HTTOPRIGHT;
				if (pt.y >= rc.bottom - x)return HTBOTTOMRIGHT;
				return HTRIGHT;
			}
			if (pt.y < rc.top + x)return HTTOP;
			if (pt.y >= rc.bottom - x)return HTBOTTOM;
		}
		return HTCAPTION;//拖动窗口
		break;
	case WM_DESTROY:
		//OnDestroy();
		PostQuitMessage(0);
		break;
	}
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
	WNDCLASS wc = { 0 };
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = L"无边框窗口";
	RegisterClassW(&wc);

	HWND hWnd = CreateWindowExW(WS_EX_NOREDIRECTIONBITMAP, wc.lpszClassName, nullptr, WS_OVERLAPPEDWINDOW, 183, 84, 1000, 600, nullptr, nullptr, hInstance, nullptr);
	ShowWindow(hWnd, nShowCmd);
	UpdateWindow(hWnd);

	MSG msg;
	while (GetMessageW(&msg, nullptr, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessageW(&msg);
	}
	return 0;
}


版权声明:本文为Direct3D12原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。