WIN32学习——鼠标消息

1 基本鼠标消息
    WM_LBUTTONDOWN - 鼠标左键按下
    WM_LBUTTONUP - 鼠标左键抬起
    WM_RBUTTONDOWN - 鼠标右键按下
    WM_RBUTTONUP - 鼠标右键抬起
    WM_MOUSEMOVE - 鼠标移动消息
2 双击消息
    WM_LBUTTONDBCLK - 鼠标左键双击
    WM_RBUTTONDBCLK - 鼠标右键双击
3 滚轮消息

    WM_MOUSEWHEEL - 鼠标滚轮消息

以上消息附加参数(WPARAM和LPARAM)说明:

1 以上消息中的LPARAM参数均表示当前鼠标的位置,其中,LOWORD表示X坐标,HIWORD表示Y坐标

2 以上消息除了WM_MOUSEWHEEL消息之外,其他消息的WPARAM均表示其他按键的状态,如ALT、SHIFT等

WM_MOUSEWHEEL消息的WPARAM的LOWORD表示其他按键的状态,如ALT、SHIFT等。HIWORD表示滚轮的偏移量,这个值是120的倍数,通过正负值表示滚动方向(正:向前滚动,负向后滚动)


附加鼠标消息学习代码:


#include
HINSTANCE g_hInstance;
HANDLE hOutputHandle;
int n_xPos = 100, n_yPos = 100;
void OnPaint(HWND hWnd)
{
	PAINTSTRUCT ps = { 0 };
	HDC hDC = BeginPaint(hWnd, &ps);
	TextOut(hDC, n_xPos, n_yPos, __TEXT("hello"), lstrlen(__TEXT("hello")));
	EndPaint(hWnd, &ps);

	TCHAR szText[256] = { 0 };
	lstrcpy(szText, __TEXT("WM_PAINT\n"));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
}

void OnLButtonDown(WPARAM wParam,LPARAM lParam)
{
	TCHAR szText[256] = { 0 };
	wsprintf(szText, __TEXT("WM_LBUTTONDOWN:%08X,(%d,%d)\n"), wParam, LOWORD(lParam), HIWORD(lParam));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
}

void OnLButtonUp(WPARAM wParam, LPARAM lParam)
{
	TCHAR szText[256] = { 0 };
	wsprintf(szText, __TEXT("WM_LBUTTONUP:%08X,(%d,%d)\n"), wParam, LOWORD(lParam), HIWORD(lParam));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
}

void OnLButtonDbclk(WPARAM wParam,LPARAM lParam)
{
	TCHAR szText[256] = { 0 };
	wsprintf(szText, __TEXT("WM_LBUTTONDBLCLK:%08X,(%d,%d)\n"), wParam, LOWORD(lParam), HIWORD(lParam));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
}

void OnMouseMove(HWND hWnd,WPARAM wParam, LPARAM lParam)
{
	TCHAR szText[256] = { 0 };
	wsprintf(szText, __TEXT("WM_MOUSEMOVE:%08X,(%d,%d)\n"), wParam, LOWORD(lParam), HIWORD(lParam));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
	n_xPos = LOWORD(lParam);
	n_yPos = HIWORD(lParam);
	InvalidateRect(hWnd, NULL, TRUE);
}

void OnMouseWheel(WPARAM wParam, LPARAM lParam)
{
	TCHAR szText[256] = { 0 };
	SHORT num = HIWORD(wParam);
	wsprintf(szText, __TEXT("WM_MOUSEWHEEL:%08X,%d,(%d,%d)\n"), LOWORD(wParam), num, LOWORD(lParam), HIWORD(lParam));
	WriteConsole(hOutputHandle, szText, lstrlen(szText), NULL, NULL);
}

LRESULT CALLBACK MyProc(HWND   hWnd, UINT   uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		//鼠标消息
	case WM_MOUSEWHEEL:
		OnMouseWheel(wParam,lParam);
		break;
	case WM_MOUSEMOVE:
		OnMouseMove(hWnd,wParam,lParam);
		break;
	case WM_LBUTTONDBLCLK:
		OnLButtonDbclk(wParam,lParam);
		break;
	case WM_LBUTTONDOWN:
		OnLButtonDown(wParam,lParam);
		break;
	case WM_LBUTTONUP:
		OnLButtonUp(wParam, lParam);
		break;
	case WM_PAINT:
		OnPaint(hWnd);
		break;
	case WM_SYSCOMMAND:
		if (wParam == SC_CLOSE)
		{
			if (IDOK == MessageBox(hWnd, __TEXT("确定要关闭窗口?"), __TEXT("提示"), MB_OKCANCEL))
			{
				PostQuitMessage(0);
			}
			else
			{
				return 0;
			}
		}
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

ATOM Register(LPCWSTR className)
{
	WNDCLASSEX wc = { 0 };
	wc.cbClsExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.hCursor = NULL;
	wc.hIcon = NULL;
	wc.hIconSm = NULL;
	wc.hInstance = g_hInstance;
	wc.lpfnWndProc = MyProc;
	wc.lpszClassName = className;
	wc.lpszMenuName = NULL;
	//要窗口实现双击功能,需要在注册窗口类中添加 CS_DBLCLKS 这种风格
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	return RegisterClassEx(&wc);
}

HWND Create(LPCWSTR className, LPCWSTR windowName)
{
	return CreateWindowEx(0, className, windowName, WS_OVERLAPPEDWINDOW, 100, 100, 600, 600, NULL, NULL, g_hInstance, NULL);
}

void Show(HWND hWnd)
{
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
}

void Message()
{
	MSG ms = { 0 };
	while (GetMessage(&ms, NULL, 0, 0))
	{
		TranslateMessage(&ms);
		DispatchMessage(&ms);
	}
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	AllocConsole();
	g_hInstance = hInstance;
	hOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	Register(__TEXT("Main"));
	HWND hWnd = Create(__TEXT("Main"), __TEXT("This is test"));
	Show(hWnd);
	Message();
	return 0;
}




你可能感兴趣的:(WIN32学习)