阅读背景:

win32 api 滚动条 处理 WM_VSCROLL消息

来源:互联网 

scroll.c

/* -------------------------------------------------------------------
Author:邱于涵
--------------------------------------------------------------------*/

#include <windows.h>
#include<strsafe.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("MyWindows");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

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

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName,
		TEXT("涵涵工作室"),
		WS_OVERLAPPEDWINDOW|WS_VSCROLL | WS_HSCROLL,  //加上 WS_VSCROLL|WS_HSCROLL就可以显示 滚动条了
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL);

	ShowWindow(hwnd, iCmdShow);
	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 rect;
	TCHAR strBuffer[128];
	//(重点)设置static 的好处就是 此变量 在一次消息相应中初始化了 下次 还可以使用(其他消息也可以使用)
	static size_t iTarget, cxClient, cyClinet;
	switch (message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd, &rect);
		DrawText(hdc, TEXT("大家好!"), -1, &rect,
			DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hwnd, &ps);
		break;
	case WM_SIZE:
		cxClient = LOWORD(lParam);
		cyClinet = HIWORD(lParam);
		hdc = GetDC(hwnd);
		//LOWORD 低十六位 HIWORD高十六位,WORD就是 字型数据 (16位)
		StringCchPrintf(strBuffer, 128, TEXT("当前客户区分辨率:%d*%d"), LOWORD(lParam), HIWORD(lParam));
		//这里要注意先后顺序 先使用StringCchPrintf 写入后,StringCchLength才有效
		StringCchLength(strBuffer, 128, &iTarget);
		TextOut(hdc, 0, 0, strBuffer, iTarget);
		ReleaseDC(hwnd, hdc);
		break;
	
	case WM_VSCROLL:
		hdc = GetDC(hwnd);
		//设置向上和向右对齐
		SetTextAlign(hdc, TA_TOP | TA_RIGHT);
		switch (LOWORD(wParam))
		{
		case SB_LINEDOWN:
			TextOut(hdc, cxClient-10, 0, TEXT("向下滚动一行"),6);
			break;
		case SB_LINEUP:
			TextOut(hdc, cxClient - 10, 0, TEXT("向上滚动一行"), 6);
			break;
		case SB_PAGEUP:
			TextOut(hdc, cxClient - 10, 0, TEXT("向上滚动一页"), 6);
			break;
		case SB_PAGEDOWN:
			TextOut(hdc, cxClient - 10, 0, TEXT("向下滚动一页"), 6);
			break;
		case SB_THUMBTRACK:
			TextOut(hdc, cxClient - 10, 0, TEXT("别抓住我不放"), 6);
			break;
		default:
			break;
		}
		ReleaseDC(hwnd, hdc);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}/* ------------------------------



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: