windows学习——文本输出

#include<Windows.h>
#include<iostream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
int a = 0;
LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE PrevhInstance,
	PSTR szCmdLine,
	int iCmdShow)
{
	//mycreatefile();
	TCHAR classname[] = TEXT("scorll_windows");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

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

	if (!RegisterClass(&wndclass)) {
		MessageBox(NULL, TEXT("register is error!"), TEXT("error"), MB_ICONERROR);
		return 0;
	}
	hwnd = CreateWindow(classname, TEXT("scorll_windows1"), WS_OVERLAPPEDWINDOW | WS_VSCROLL,
		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);
	}
	MessageBox(NULL, TEXT("ASDASDASD"), TEXT("ASDASDASD"), MB_OK);
	return msg.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int cxChar, cyChar, cCaps;
	int cxClient, cyClient;
	int iBeginPaint, iEndPaint;
	PAINTSTRUCT ps;
	HDC hdc;
	TEXTMETRIC tm;
	SCROLLINFO si;
	int iVertpos;
	switch (message) {
	case WM_CREATE:
		hdc = GetDC(hwnd);
		GetTextMetrics(hdc, &tm);
		cxChar = tm.tmAveCharWidth;
		cCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)*cxChar / 2;
		cyChar = tm.tmHeight + tm.tmExternalLeading;
		ReleaseDC(hwnd, hdc);
		return 0;
	case WM_SIZE:
		cxClient = LOWORD(lParam);
		cyClient = HIWORD(lParam);
		si.cbSize = sizeof(si);
		si.fMask = SIF_RANGE | SIF_PAGE;
		si.nMin = 0;
		si.nMax = 100;
		si.nPage = cyClient / cyChar;
		SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
		return 0;
	case WM_VSCROLL:
		si.cbSize = sizeof(si);
		si.fMask = SIF_ALL;
		GetScrollInfo(hwnd, SB_VERT, &si);
		iVertpos = si.nPos;
		switch (LOWORD(wParam)) {
		case SB_TOP:
			si.nPos = si.nMin;
			break;
		case SB_BOTTOM:
			si.nPos = si.nMax;
			break;
		case SB_PAGEDOWN:
			si.nPos += si.nPage; break;
		case SB_PAGEUP:
			si.nPos -= si.nPage; break;
		case SB_THUMBPOSITION:
			si.nPos = si.nTrackPos; break;
		case SB_LINEDOWN:
			si.nPos += 1; break;
		case SB_LINEUP:
			si.nPos -= 1; break;
		}
		SetScrollInfo(hwnd, SB_VERT, &si, true);
		GetScrollInfo(hwnd, SB_VERT, &si);//试过了如果没有这行代码也一样;
		if (si.nPos != iVertpos) {
			ScrollWindow(hwnd, 0, cyChar*(iVertpos - si.nPos), NULL, NULL);
		}
		return 0;
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		si.cbSize = sizeof(si);
		si.fMask = SIF_POS;
		GetScrollInfo(hwnd, SB_VERT, &si);
		iVertpos = si.nPos;
		iBeginPaint = max(0, iVertpos + ps.rcPaint.top / cyChar);
		iEndPaint = min(100, iVertpos + ps.rcPaint.bottom / cyChar);
		for (int i = iBeginPaint; i <= iEndPaint; ++i) {
			int y;
			y = cyChar*(i - iBeginPaint);
			TCHAR str[100];
			TextOut(hdc, 0, y, str,wsprintf(str,TEXT("sdasdads %d"),i));
		}
		EndPaint(hwnd, &ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值