滑动块 的问题
大佬帮忙 帮我讲讲好么 谢谢了#include <windows.h>
#include <strsafe.h>
#include "symets.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstcance, PSTR lpCmdLine, int iCmdShow)
{
HWND hwnd;
MSGmsg;
WNDCLASS wndclass;
//窗口类名
WCHAR szClassName[] = TEXT("FristWindow");
//初始化窗口类
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szClassName;
wndclass.lpszMenuName = NULL;
//注册窗口类
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("本程序只能在 WINDOWS NT 框架上运行!!!"), TEXT("提示信息:"), MB_OK|MB_ICONERROR);
return 0;
}
//创建窗口
hwnd = CreateWindow(szClassName, TEXT("FristWindow"), 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);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
SCROLLINFO si;
static int xChar, yChar, xUpper;
static int xClient, yClient, yPos;
int iBeginPaint, iEndPaint;
WCHAR szBuffer;
switch (msg)
{
case WM_CREATE:
//获得字体大小
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
xChar = tm.tmAveCharWidth;
yChar = tm.tmHeight+tm.tmExternalLeading;
xUpper = (int)(tm.tmPitchAndFamily&1?3:2)*xChar/2;
ReleaseDC(hwnd, hdc);
break;
case WM_SIZE:
//获取客户区大小
xClient = LOWORD(lParam);
yClient = HIWORD(lParam);
//设置垂直滚动条信息
si.cbSize = sizeof(si);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nPage = yClient / yChar;
si.nMax = NUMLINES - 1;
si.nMin = 0;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
yPos = si.nPos;
iBeginPaint = max(0, yPos + ps.rcPaint.top / yChar);
iEndPaint = min(NUMLINES, yPos + ps.rcPaint.bottom / yChar);
for (int i = iBeginPaint; i < iEndPaint; i++)
{
TextOut(hdc, 50*xUpper,(i-yPos)*yChar,sysmetrics.szLabel,lstrlen(sysmetrics.szLabel));
}
EndPaint(hwnd, &ps);
break;
case WM_VSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
yPos = 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 (yPos != si.nPos)
{
ScrollWindow(hwnd, 0, yChar*(yPos - si.nPos), NULL, NULL);
UpdateWindow(hwnd);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
yPos 是指的当前 滑动块 的位置 也就是首行 的索引值
ps.rcPaint.top 是指的无效区域 的到顶部的距离
yChar 字体的大小
iBeginPaint:
ps.rcPaint.top/yChar 得的到距离首行的行数
yPos+ps.rcPaint.top/yChar 获得需要重绘 的索引值
iEndPaint:
同上
(i-yPos)*yChar:
这个是什么意思
i- yPos是当前 首行 到需要重绘的偏移的 * yChar是当期 需要偏移的距离吗? ScrollWindow(NULL, 0 , yChar*(yPos - si.nPos),NULL, NULL);
yChar*(yPos - si.nPos)这是需要 滚动的距离么?
负数 下滚
正数 上滚
大佬 帮讲讲
(i-yPos)*yChar
首先,yChar 表示的是每行的高度
好像同一个问题问了两遍?
i 表示的是文本的第 i 行
yPos此时表示垂直滚动条的位置
因为是往上滚动时应该为负数,此时行数变为:i +(- yPos)
往下滚动时应该为正数,此时行数变为: i - (+ yPos)
很明显,两种情况都可以写作 i - yPos
页:
[1]