#include <windows.h>
#include <strsafe.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
WNDCLASS wndclass;
MSG msg;
static WCHAR szClassName[] = TEXT("ClassName");
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;
wndclass.lpfnWndProc = WndProc;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)COLOR_MENU;
wndclass.cbWndExtra = 0;
wndclass.cbClsExtra = 0;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("请在window nt 上使用本程序"), TEXT("提示"), MB_OK);
return -1;
}
HWND hwnd = CreateWindow(szClassName,
TEXT(""),
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static int iOldyScrollPos = 0;
static int cxChar, cyChar;
static int cxClient, cyClient;
TEXTMETRICW tm;
PAINTSTRUCT ps;
RECT rect;
SCROLLINFO si;
TCHAR szWindowText[128];
int i = 0;
int len = 0;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
/* 计算出字体的间距 */
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
return 0;
case WM_SIZE:
/* 获取客户端分辨率 */
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
/* 下面这两句的功能是开启 SetScrollInfo 和 GetScrollInfo 的功能 */
memset(&si, 0, sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = 256 - 1;
si.nPos = 0;
si.nPage = cyClient / cyChar; //这个就是设置滑块大小的参数
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
return 0;
case WM_VSCROLL:
/* 下面这两句的功能是开启 SetScrollInfo 和 GetScrollInfo 的功能 */
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
iOldyScrollPos = si.nPos;
switch (LOWORD(wParam))
{
case SB_LINEUP:
si.nPos -= 1;
break;
case SB_LINEDOWN:
si.nPos += 1;
break;
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBPOSITION:
si.nPos = si.nTrackPos;
break;
}
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
GetScrollInfo(hwnd, SB_VERT, &si);
if (iOldyScrollPos != si.nPos)
{
ScrollWindow(hwnd, 0, cyChar * (iOldyScrollPos - si.nPos), NULL, NULL);
UpdateWindow(hwnd);
iOldyScrollPos = si.nPos;
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
/* 这下面便是显示进程的地方 */
for (i = 0; i < 256; i++)
{
StringCchPrintf(szWindowText, 128, TEXT("%d: %s"), i + 1, TEXT("I love you"));
StringCchLength(szWindowText, 128, &len);
TextOut(hdc, 0, (i - iOldyScrollPos) * cyChar, szWindowText, len);
}
EndPaint(hwnd, &ps);
return 0;
case WM_CLOSE:
if (IDYES == MessageBox(NULL, TEXT("是否退出本程序"), TEXT("提示:"), MB_YESNO))
{
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}