鱼C论坛

 找回密码
 立即注册
查看: 2563|回复: 3

滑动块 的问题

[复制链接]
发表于 2017-10-15 16:52:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大佬帮忙 帮我讲讲好么 谢谢了



#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;
        MSG  msg;
        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[128];

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

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-10-15 17:00:27 | 显示全部楼层
yPos                 是指的当前 滑动块 的位置 也就是首行 的索引值
ps.rcPaint.top    是指的无效区域 的到顶部的距离
yChar                字体的大小

iBeginPaint:

ps.rcPaint.top/yChar     得的到距离首行的行数
yPos+ps.rcPaint.top/yChar   获得需要重绘 的索引值

iEndPaint:
同上

(i-yPos)*yChar:
这个是什么意思

i- yPos  是当前 首行 到需要重绘的偏移的 * yChar  是当期 需要偏移的距离吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 17:03:19 | 显示全部楼层
ScrollWindow(NULL, 0 , yChar*(yPos - si.nPos),  NULL, NULL);

yChar*(yPos - si.nPos)  这是需要 滚动的距离么?

负数    下滚
正数    上滚

大佬 帮讲讲
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-27 16:25:45 | 显示全部楼层

(i-yPos)*yChar
首先,yChar 表示的是每行的高度
好像同一个问题问了两遍?
i 表示的是文本的第 i 行
yPos此时表示垂直滚动条的位置
因为是往上滚动时应该为负数,此时行数变为:  i +(- yPos)
往下滚动时应该为正数,此时行数变为: i - (+ yPos)
很明显,两种情况都可以写作 i - yPos
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表