#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);
}