Win32按钮设计思路

本文档展示了如何使用C++和Windows API创建一个图形界面应用程序,包含背景图片设置、子窗口创建、鼠标事件处理,以及根据鼠标状态改变窗口显示效果。通过`WndProc`和`ChildWndProc`两个窗口过程函数处理窗口消息,实现鼠标悬停和按下时的视觉反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <windows.h>
#include <stdio.h>

#pragma comment(lib,"Msimg32.lib")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HBITMAP hBkBmp;
HBRUSH hBkBrush;

#ifdef _DEBUG
int main()
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR szCmdLine, int iCmdShow)
#endif
{
	static TCHAR szAppName[] = TEXT("HelloWin");
	HWND         hwnd;
	MSG          msg;
	WNDCLASS     wndclass;

#ifdef _DEBUG
	hInst = GetModuleHandleA(NULL);
#else
	hInst = hInstance;
#endif

	hBkBmp = (HBITMAP)LoadImageA(NULL,"background.bmp",IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
	hBkBrush = CreatePatternBrush(hBkBmp);


	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInst;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = hBkBrush;// (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	wndclass.hbrBackground = NULL;// (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszClassName = "ChildWnd";
	wndclass.lpfnWndProc = ChildWndProc;
	RegisterClass(&wndclass);

	hwnd = CreateWindow(szAppName,                  // window class name
		TEXT("The Hello Program"), // window caption
		WS_OVERLAPPEDWINDOW,        // window style
		CW_USEDEFAULT,              // initial x position
		CW_USEDEFAULT,              // initial y position
		CW_USEDEFAULT,              // initial x size
		CW_USEDEFAULT,              // initial y size
		NULL,                       // parent window handle
		NULL,                       // window menu handle
		hInst,                  // program instance handle
		NULL);                     // creation parameters

	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

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

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc;
	PAINTSTRUCT ps;
	//RECT rc;

	switch (message)
	{
	case WM_CREATE:
		CreateWindow("ChildWnd",                  // window class name
			NULL, // window caption
			WS_CHILD|WS_VISIBLE,        // window style
			10,              // initial x position
			10,              // initial y position
			300,              // initial x size
			200,              // initial y size
			hwnd,                       // parent window handle
			NULL,                       // window menu handle
			hInst,                  // program instance handle
			NULL);                     // creation parameters
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		//GetClientRect(hwnd, &rc);
		//FillRect(hdc, &rc, hBkBrush);
		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

bool IsMouseInside(HWND hwnd)
{
	POINT pt;
	RECT rc;
	GetCursorPos(&pt);
	GetWindowRect(hwnd, &rc);
	if (pt.x >= rc.left && pt.x <= rc.right &&
		pt.y >= rc.top && pt.y <= rc.bottom)
		return true;
	return false;
}

RECT GetChildRectInParent(HWND hwndParent,HWND hChild)
{
	RECT rec;
	GetClientRect(hChild, &rec);
	MapWindowPoints(hChild, hwndParent, (POINT*)&rec, 2);
	return rec;
}


void DrawHover(HWND hwnd)
{
	HWND hParentWnd = GetParent(hwnd);
	RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
	RECT rcParent;
	GetClientRect(hParentWnd, &rcParent);
	HDC hdc, hdcMem;
	HBITMAP hBmp;
	RECT rc;
	GetClientRect(hwnd, &rc);
	hdc = GetDC(hwnd);
	hdcMem = CreateCompatibleDC(hdc);
	hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
	SelectObject(hdcMem, hBmp);
	FillRect(hdcMem, &rcParent, hBkBrush);

	//HDC hdcParent = GetDC(hParentWnd);
	BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
		SRCCOPY);
	//ReleaseDC(hParentWnd, hdcParent);

	SetBkMode(hdc, TRANSPARENT);
	SetTextColor(hdc, RGB(255,0,0));
	char* str = "鼠标悬停";
	TextOutA(hdc, 0, 0, str, strlen(str));
	//TransparentBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, rc.right, rc.bottom,
	//	RGB(255, 255, 255));
	ReleaseDC(hwnd,hdc);
	DeleteObject(hdcMem);
	DeleteObject(hBmp);
}

void DrawPressed(HWND hwnd)
{
	HWND hParentWnd = GetParent(hwnd);
	RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
	RECT rcParent;
	GetClientRect(hParentWnd, &rcParent);
	HDC hdc, hdcMem;
	HBITMAP hBmp;
	RECT rc;
	GetClientRect(hwnd, &rc);
	hdc = GetDC(hwnd);
	hdcMem = CreateCompatibleDC(hdc);
	hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
	SelectObject(hdcMem, hBmp);
	FillRect(hdcMem, &rcParent, hBkBrush);

	BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
		SRCCOPY);

	SetBkMode(hdc, TRANSPARENT);
	SetTextColor(hdc, RGB(255, 0, 0));
	char* str = "鼠标按下";
	TextOutA(hdc, 0, 0, str, strlen(str));
	ReleaseDC(hwnd, hdc);
	DeleteObject(hdcMem);
	DeleteObject(hBmp);
}

void OnPaint(HWND hwnd, HDC hdc)
{
	HWND hParentWnd = GetParent(hwnd);
	RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
	RECT rcParent;
	GetClientRect(hParentWnd, &rcParent);
	HDC hdcMem;
	HBITMAP hBmp;
	RECT rc;
	GetClientRect(hwnd, &rc);
	//hdc = GetDC(hwnd);
	hdcMem = CreateCompatibleDC(hdc);
	hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
	SelectObject(hdcMem, hBmp);
	FillRect(hdcMem, &rcParent, hBkBrush);

	BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
		SRCCOPY);

	SetBkMode(hdc, TRANSPARENT);
	SetTextColor(hdc, RGB(255, 0, 0));
	char* str = "默认样式";
	TextOutA(hdc, 0, 0, str, strlen(str));
	DeleteObject(hdcMem);
	DeleteObject(hBmp);
}

LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc;
	PAINTSTRUCT ps;

	TRACKMOUSEEVENT tme;

	//避免因频繁触发WM_MOUSEMOVE而导致频繁绘图
	static bool bEnter;
	static bool bLeaveWithPressed;//按着离开

	HDC hdcMem;
	static HBITMAP hBmp;
	RECT rc;

	switch (message)
	{
	case WM_CREATE:
		bEnter = false;
		bLeaveWithPressed = false;
		return 0;

	//case WM_SIZE:
	//	return 0;

	case WM_LBUTTONDOWN:
		SetCapture(hwnd);
		bLeaveWithPressed = false;
		//画按下的状态
		DrawPressed(hwnd);
		return 0;

	case WM_LBUTTONUP:
		if (GetCapture() == hwnd)
		{
			if (IsMouseInside(hwnd))
			{
				//执行事件响应
				//画鼠标悬停
				DrawHover(hwnd);
			}
			ReleaseCapture();
		}
		return 0;

	case WM_MOUSEMOVE:
		if (GetCapture() == hwnd)
		{
			if (IsMouseInside(hwnd))
			{
				if (!bLeaveWithPressed)
				{
					bLeaveWithPressed = true;
					//绘制按下的状态
					DrawPressed(hwnd);
#ifdef _DEBUG
					printf("带着焦点在窗口内移动\n");
#endif
				}
			}
			else
			{
				if (bLeaveWithPressed)
				{
					bLeaveWithPressed = false;
					//恢复默认样式
					InvalidateRect(hwnd, NULL, FALSE);
					UpdateWindow(hwnd);
#ifdef _DEBUG
					printf("带着焦点在窗口外移动\n");
#endif
				}
			}
		}
		else
		{
			if (!bEnter)
			{
				bEnter = true;
				tme.cbSize = sizeof(tme);
				tme.dwFlags = TME_LEAVE;
				tme.hwndTrack = hwnd;
				TrackMouseEvent(&tme);
				DrawHover(hwnd);
#ifdef _DEBUG
				printf("不带焦点在窗口内移动\n");
#endif
			}
		}
		return 0;

	case WM_MOUSELEAVE:
		bEnter = false;
		InvalidateRect(hwnd, NULL, FALSE);
		UpdateWindow(hwnd);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		OnPaint(hwnd, hdc);
		EndPaint(hwnd, &ps);
		return 0;

	case WM_ERASEBKGND:
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值