邱于涵的博客

软件开发|网站开发|C++|PHP|JavaEE|数据库

win32 (SDK) 垂直滚动条

scroll.c

/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/

#include <windows.h>
#include<strsafe.h>

#define NUMLINE 100

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;
	int i = 0;
	size_t size;
	static TEXTMETRIC TextMetric;
	TCHAR strBuffer[128];
	//(重点)设置static 的好处就是 此变量 在一次消息相应中初始化了 下次 还可以使用(其他消息也可以使用)
	                                         //滚动条位置    文字位置
	static size_t iTarget, cxClient, cyClinet, iVScrollPos = 0,iVTextPos=0, charWidth, charHeight;
	switch (message)
	{
	case WM_CREATE:
		hdc = GetDC(hwnd);
		GetTextMetrics(hdc, &TextMetric);
		charWidth = TextMetric.tmAveCharWidth;
		charHeight = TextMetric.tmHeight + TextMetric.tmExternalLeading;
		ReleaseDC(hwnd, hdc);
		//NUMLINE -1是因为从0开始  最后一个参数指定滚动条是否重绘
		//有多少行就 这是多少值
		SetScrollRange(hwnd, SB_VERT, 0, NUMLINE - 1, FALSE);
		//设置 TRUE 滚动条立即重绘 这样才能看到新位置
		SetScrollPos(hwnd, SB_VERT, 0, TRUE);
		break;
	case WM_PAINT:
		hdc = GetDC(hwnd);
		for (i; i < NUMLINE; i++)
		{
			StringCchPrintf(strBuffer, 128, TEXT("%d,%s"), i,TEXT("你好"));
			StringCchLength(strBuffer, 128, &size);
			// tmAveCharWidth 平均宽度 tmHeight+tnExternalLeading就是高度
			TextOut(hdc, charWidth, (iVTextPos*charHeight) + i*charHeight, strBuffer, size);
		}
		ReleaseDC(hwnd, hdc);
		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:
			if (iVScrollPos != (NUMLINE-1)) iVTextPos -= 1;
			TextOut(hdc, cxClient-10, 0, TEXT("向下滚动一行"),6);
			
			break;
		case SB_LINEUP:
			if (iVScrollPos != 0) iVTextPos += 1;
			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:
			iVTextPos = -HIWORD(wParam);
			
			TextOut(hdc, cxClient - 10, 0, TEXT("别抓住我不放"), 6);
			break;
		default:
			break;
		}
		iVScrollPos = -iVTextPos;
		ReleaseDC(hwnd, hdc);
		SetScrollPos(hwnd, SB_VERT, iVScrollPos, TRUE);
		InvalidateRect(NULL, NULL, FALSE);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012997311/article/details/53265354
所属专栏: windows程序设计(SDK)
想对作者说点什么? 我来说一句

Win32 SDK,Win32 API,Windows SDK

2017年01月18日 5.85MB 下载

没有更多推荐了,返回首页

不良信息举报

win32 (SDK) 垂直滚动条

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭