首页 > 编程语言 > 详细

关于MFC中滚动条的参悟

时间:2019-06-17 18:34:33      阅读:129      评论:0      收藏:0      [点我收藏+]

  int iHeight = GetSystemMetrics(SM_CYSCREEN);//获取屏幕高度的分辨率

    CRect rect1;

    GetClientRect(rect1);//获取要客户区位置信息(呈现数据矩形框)

 

    SCROLLINFO scrollInfo;         

    GetScrollInfo(SB_VERT,   &scrollInfo,   SIF_ALL); //获取进度条当前信息,因为要考虑滚动条滚动到一半,然后对界面放大,滚动条仍然要处在正确的,此处获取的是放大或缩小前的滚动条信息。

 

    SCROLLINFO info;

    info.cbSize = sizeof(SCROLLINFO);    

    info.fMask = SIF_ALL;    

    info.nMin = 0;    

    info.nMax = 2*iHeight-rect1.Height(); //图形的总高度为2* iHeight,我们所要移动的高度是2*iHeight-rect1.Height(),因为客户区界面上已经显示了一部分。我们的最大值就设置为2*iHeight-rect1.Height()。

    info.nPage = 1;//每单位移动一个像素点    

    info.nPos = (scrollInfo.nPos*(2*iHeight-rect1.Height()))/scrollInfo.nMax; 

//计算放大或缩小后的滚动位置,scrollInfo.nPos(原始位置)除以scrollInfo.nMax(原始最大位置),得到原始位置中滚动条所占的比例,乘以(2*iHeight-rect1.Height())(当前最大位置)得到在新界面中滚动条所在的位置。

    SetScrollInfo(SB_VERT,&info,SIF_ALL);//设置滚动条信息

    ScrollWindow(0,  -1*info.nPos);//滚动客户区界面,滚动条向下走,第二个值要取负数。

 

按比例放大或缩小:

在.h文件中定义POINT oldPiont;

在OnInitDialog函数中计算当前客户区的宽度和高度。

 CRect rect;
 GetClientRect(&rect);
 oldPiont.x = rect.right - rect.left;
 oldPiont.y = rect.bottom - rect.top;

在界面尺寸发生变化时在OnSize(UINT nType, int cx, int cy)函数中调用如下代码:

 

    float fsp[2];
        POINT newPoint;//获取当前对话框大小
        CRect newRect;//获取当前对话框的坐标
        GetClientRect(&newRect);
        newPoint.x = newRect.right - newRect.left;
        newPoint.y = newRect.bottom - newRect.top;
        fsp[0] = (float)newPoint.x / oldPiont.x;
        fsp[1] = (float)newPoint.y / oldPiont.y;

        int woc;
        CRect rect;
        CPoint oldTLPoint, newTLPoint;//左上角
        CPoint oldBRPoint, newBRPoint;//右下角
        //列出所有的子空间
        HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD);
        while (hwndChild) {
            woc = ::GetDlgCtrlID(hwndChild);//取得ID


            GetDlgItem(woc)->GetWindowRect(rect);

            ScreenToClient(rect);

            oldTLPoint = rect.TopLeft();
            newTLPoint.x = long(oldTLPoint.x*fsp[0]);
            newTLPoint.y = long(oldTLPoint.y*fsp[1]);
            oldBRPoint = rect.BottomRight();
            newBRPoint.x = long(oldBRPoint.x*fsp[0]);
            newBRPoint.y = long(oldBRPoint.y*fsp[1]);

            rect.SetRect(newTLPoint, newBRPoint);

            GetDlgItem(woc)->MoveWindow(rect, TRUE);
            hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
        }
        oldPiont = newPoint;


 

 

关于MFC中滚动条的参悟

原文:https://www.cnblogs.com/mylsoft/p/11040794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!