⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageviewerview.cpp

📁 图像显示软件源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    if (Dlg.DoModal() == IDOK)
    {
        while(GetQueueStatus(QS_PAINT))
        {
            // allow main window to redraw to fully remove the dialog
            // before starting a potentially lengthy delete operation
            theApp.PumpMessage();
        }

        switch (Dlg.Selection)
        {
        case 0: // Current image
            pDoc->DeleteImageData(CurrentImage);
            break;

        case 1: // All except current image
            if (0 != CurrentImage)
            {
                pDoc->DeleteImageData(0, CurrentImage - 1);
            }

            pDoc->DeleteImageData(CurrentImage + 1, pDoc->GetImageDataCount());
            break;

        case 2: // All images
            pDoc->DeleteImageData(0, pDoc->GetImageDataCount());
            break;

        case 3: // range of images
            {

                UINT From = min(Dlg.From, Dlg.To);
                UINT To = max(Dlg.From, Dlg.To);
                pDoc->DeleteImageData(From - 1, To - 1);
            }
            break;

        default: // invalid value
            ASSERT(0);
        }
    }

    if (!locked)
    {
        // remove the temporary lock
        AfxGetMainWnd()->SendMessage(WM_COMMAND, ID_VIEW_DISPLAY_LOCK, 0);
    }
}

void CImageViewerView::OnUpdateImageDelete(CCmdUI *pCmdUI)
{
    pCmdUI->Enable(GetDocument()->GetImageDataCount() > 0);
}

void CImageViewerView::OnSetFocus(CWnd* pOldWnd)
{
    CScrollView::OnSetFocus(pOldWnd);

    AfxGetMainWnd()->SendMessage(WMU_SETPROPERTIES,
                                 reinterpret_cast<WPARAM>(GetParent()),
                                 reinterpret_cast<LPARAM>(GetDocument()->GetImageData(CurrentImage)));
}

bool CImageViewerView::MoveCursor(int x, int y)
{
    if (0 == x && 0 == y)
        return false;

    CRect VisibleRect = GetVisibleRect();
    CPoint CursorPos;
    GetCursorPos(&CursorPos);
    if (!VisibleRect.PtInRect(CursorPos))
        return false;

    if (ZoomRatios[CurrentZoomRatio] > 100)
    {
        // convert client coords to pixel coords, advance pixel, convert pixel coords back to client coords
        ScreenToClient(&CursorPos);
        CursorPos += GetDeviceScrollPosition();
        double xpos = floor((CursorPos.x - ZoomedBitmapRect.left) * 100.0 / ZoomRatios[CurrentZoomRatio]) + x;
        CursorPos.x = long(ZoomedBitmapRect.left + ceil(xpos * ZoomRatios[CurrentZoomRatio] / 100.0));
        double ypos = floor((CursorPos.y - ZoomedBitmapRect.top) * 100.0 / ZoomRatios[CurrentZoomRatio]) + y;
        CursorPos.y = long(ZoomedBitmapRect.top + ceil(ypos * ZoomRatios[CurrentZoomRatio] / 100.0));
        CursorPos -= GetDeviceScrollPosition();
        ClientToScreen(&CursorPos);
    }
    else
    {
        CursorPos += CPoint(x, y);
    }

    int bottomrightoffset = (ViewGrid && ZoomRatios[CurrentZoomRatio] > 199) ? 2 : 1;

    if (CursorPos.x > VisibleRect.right - bottomrightoffset)
        CursorPos.x = VisibleRect.right - bottomrightoffset;
    if (CursorPos.x < VisibleRect.left)
        CursorPos.x = VisibleRect.left;

    if (CursorPos.y > VisibleRect.bottom - bottomrightoffset)
        CursorPos.y = VisibleRect.bottom - bottomrightoffset;
    if (CursorPos.y < VisibleRect.top)
        CursorPos.y = VisibleRect.top;

    return CenterCursorOnPixel(CursorPos);
}

bool CImageViewerView::CenterCursorOnPixel(CPoint CursorPos)
{
    CPoint ImagePoint(CursorPos);
    ScreenToClient(&ImagePoint);
    ImagePoint += GetDeviceScrollPosition();
    if (ZoomRatios[CurrentZoomRatio] >= 200)
    {
        // center the mouse cursor on the image pixel
        double pixels = ZoomRatios[CurrentZoomRatio] / 100.0;
        long xpos = long((ImagePoint.x - ZoomedBitmapRect.left) / pixels);
        ImagePoint.x = ZoomedBitmapRect.left + long(xpos * pixels) + long(pixels / 2);
        long ypos = long((ImagePoint.y - ZoomedBitmapRect.top) / pixels);
        ImagePoint.y = ZoomedBitmapRect.top + long(ypos * pixels) + long(pixels / 2);
        ImagePoint -= GetDeviceScrollPosition();
        ClientToScreen(&ImagePoint);
        CursorPos = ImagePoint;
    }

    if (GetVisibleRect().PtInRect(CursorPos))
    {
        SetCursorPos(CursorPos.x, CursorPos.y);
        return true;
    }
    return false;
}

void CImageViewerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    if (IsCTRLpressed())
    {
        if (IsSHIFTpressed() || (nChar != VK_HOME && nChar != VK_END))
        {
            return CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
        }
    }

    UINT Message = IsSHIFTpressed() ? WM_HSCROLL : WM_VSCROLL;
    CPoint CursorPos;
    GetCursorPos(&CursorPos);

    switch (nChar)
    {
    case VK_UP:
        if (IsSCROLLLOCKToggled() ||
            IsSHIFTpressed() ||
            GetScrollPos(SB_VERT) <= 0)
        {
            MoveCursor(0, -1);
        }
        else
        {
            SendMessage(WM_VSCROLL, SB_LINEUP, 0);
            CenterCursorOnPixel(CursorPos);
        }
        break;

    case VK_DOWN:
        if (IsSCROLLLOCKToggled() ||
            IsSHIFTpressed() ||
            GetScrollPos(SB_VERT) >= GetScrollLimit(SB_VERT) ||
            !(GetStyle() & WS_VSCROLL))
        {
            MoveCursor(0, 1);
        }
        else
        {
            SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
            CenterCursorOnPixel(CursorPos);
        }
        break;

    case VK_LEFT:
        if (IsSCROLLLOCKToggled() ||
            IsSHIFTpressed() ||
            GetScrollPos(SB_HORZ) <= 0)
        {
            MoveCursor(-1, 0);
        }
        else
        {
            SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
            CenterCursorOnPixel(CursorPos);
        }
        break;

    case VK_RIGHT:
        if (IsSCROLLLOCKToggled() ||
            IsSHIFTpressed() ||
            GetScrollPos(SB_HORZ) >= GetScrollLimit(SB_HORZ) ||
            !(GetStyle() & WS_HSCROLL))
        {
            MoveCursor(1, 0);
        }
        else
        {
            SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
            CenterCursorOnPixel(CursorPos);
        }
        break;

    case VK_NEXT:
        SendMessage(Message, SB_PAGEDOWN, 0);
            CenterCursorOnPixel(CursorPos);
        break;

    case VK_PRIOR:
        SendMessage(Message, SB_PAGEUP, 0);
        CenterCursorOnPixel(CursorPos);
        break;

    case VK_HOME:
        if (IsCTRLpressed())
        {
            SendMessage(WM_HSCROLL, SB_LEFT, 0);
            SendMessage(WM_VSCROLL, SB_TOP, 0);
        }
        else
        {
            SendMessage(Message, SB_TOP, 0);
        }
        CenterCursorOnPixel(CursorPos);
        break;

    case VK_END:
        if (IsCTRLpressed())
        {
            SendMessage(WM_HSCROLL, SB_RIGHT, 0);
            SendMessage(WM_VSCROLL, SB_BOTTOM, 0);
        }
        else
        {
            SendMessage(Message, SB_BOTTOM, 0);
        }
        CenterCursorOnPixel(CursorPos);
        break;

    case VK_ESCAPE:
        {
            CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
            if (NULL != pMainFrame && pMainFrame->PropertiesDialog.IsWindowVisible())
            {
                pMainFrame->PostMessage(WM_COMMAND, MAKEWPARAM(ID_IMAGE_PROPERTIES, 1), NULL);
            }
        }
        break;

    case VK_TAB:
        {
            CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
            if (NULL != pMainFrame && pMainFrame->PropertiesDialog.IsWindowVisible())
            {
                pMainFrame->PropertiesDialog.SetFocus();
            }
        }
        break;

    default:
        CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
    }
}

void CImageViewerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
    CDeletedImageData *pData = dynamic_cast<CDeletedImageData *>(pHint);
    if (pData)
    {
        if (pData->CountLeft == 0)
        {
            GetParent()->PostMessage(WM_CLOSE);
            return;
        }

        size_t Temp = CurrentImage;
        if (CurrentImage >= pData->StartRange && CurrentImage <= pData->EndRange)
        {
            // current image in deleted range, set to first image after deleted range
            Temp = pData->StartRange;
        }
        else if (CurrentImage > pData->EndRange)
        {
            // deleted range below this image, keep on same image
            Temp -= pData->EndRange - pData->StartRange + 1;
        }

        if (Temp >= pData->CountLeft)
        {
            // went past the last image, stay on the last image
            Temp = pData->CountLeft - 1;
        }

//        TRACE(_T("CurrentImage %d, Deleted %d to %d, CountLeft %d, Temp %d\n"),
//              CurrentImage, pData->StartRange, pData->EndRange, pData->CountLeft, Temp);

        SetCurrentImage(Temp, true);
    }
    else if (lHint == 0xfeb1)
    {
        CImageViewerDoc *pDoc = GetDocument();
        SetCurrentImage(pDoc->GetImageDataCount() - 1, CurrentImage == 0xffffffff);
    }
    else
    {
        CScrollView::OnUpdate(pSender, lHint, pHint);
    }
}

BOOL CImageViewerView::OnMouseWheel(UINT nFlags, short zDelta, CPoint /*pt*/)
{
    // Wheel = vertical scroll
    // Shift + wheel = horizontal scroll
    // Control + wheel = zoom
    static int Cumulative = 0;
    static UINT LastFlag = nFlags;

    if (nFlags != LastFlag)
    {
        Cumulative = 0;
        LastFlag = nFlags;
    }

    Cumulative += zDelta;

    if (nFlags & MK_CONTROL)
    {
        if (abs(Cumulative) >= WHEEL_DELTA)
        {
            if (Cumulative < 0)
            {
                OnZoomZoomIn();
            }
            else
            {
                OnZoomZoomOut();
            }
            Cumulative = 0;
        }
    }
    else
    {
        UINT LinesPerDelta = 3;
        SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &LinesPerDelta, 0);
        int LineDelta = WHEEL_DELTA / LinesPerDelta;
        if (abs(Cumulative) >= LineDelta)
        {
            UINT Message = nFlags & MK_SHIFT ? WM_HSCROLL : WM_VSCROLL;
            WPARAM Direction = Cumulative < 0 ? SB_LINERIGHT : SB_LINELEFT;
            for (int x = 0; x < abs(Cumulative / LineDelta); x++)
            {
                PostMessage(Message, Direction, 0);
            }
            Cumulative = 0;
        }
    }

    return TRUE;
}

void CImageViewerView::OnMouseMove(UINT nFlags, CPoint point)
{
    CRect ClientRect;
    GetClientRect(ClientRect);

    if (LButtonDown &&
        (point.x < AUTOSCROLLBORDER ||
         point.y < AUTOSCROLLBORDER ||
         ClientRect.right - point.x < AUTOSCROLLBORDER ||
         ClientRect.bottom - point.y < AUTOSCROLLBORDER
        )
       )
    {
        SetTimer(AUTOSCROLLTIMERID, AUTOSCROLLTIMERSPEED, NULL);
        AutoScrollTimer = true;
    }
    else if (AutoScrollTimer)
    {
        AutoScrollTimer = false;
    }

    CScrollView::OnMouseMove(nFlags, point);
}

void CImageViewerView::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == AUTOSCROLLTIMERID)
    {
        if (!AutoScrollTimer)
        {
            KillTimer(AUTOSCROLLTIMERID);
            return;
        }

        CPoint pt;
        GetCursorPos(&pt);
        ScreenToClient(&pt);

        CRect ClientRect;
        GetClientRect(&ClientRect);

        if (!ClientRect.PtInRect(pt))
        {
            // should never get here, cursor has been clipped in OnLButtonDown
            KillTimer(AUTOSCROLLTIMERID);
            AutoScrollTimer = false;
        }
        else
        {
            int pixels = ZoomRatios[CurrentZoomRatio] / 100;
            if (pixels < 1)
                pixels = 1;

            CPoint ScrollPos(GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));
            CPoint NewPos = ScrollPos;

            if (GetStyle() & WS_HSCROLL)
            {
                if (pt.x < AUTOSCROLLBORDER)
                    NewPos.x -= pixels;

                if (ClientRect.right - pt.x < AUTOSCROLLBORDER)
                    NewPos.x += pixels;

                int xMax = GetScrollLimit(SB_HORZ);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -