zoukankan      html  css  js  c++  java
  • 如何让CRectTracker的m_rect不超出一定的范围,比如screen或者某个document的范围

    最近在尝试做一个QQ截图那样的工具,其中一个功能就是要做一个选择框,自然用到了CRectTracker

    但是有一个很关键的东西就是,拖拽CRectTracker的时候,不能让CRectTracker“移出”屏幕,否则截图出来就有黑色的块

    怎么办?搜了一下,也没搜到什么有用的资料(可能是我搜索技能太low)

    去MSDN看着CRectTracker的文档,想一想应该是Override其中某个method就可以的。

    后来琢磨了一下,搞定了,直接Override CRectTracker::OnChangedRect即可,代码直接贴下面:

    CVSCRectTracker.h

     1 #pragma once
     2 
     3 #include <afxext.h>
     4 
     5 // Canvas CRectTracker
     6 class CVSCRectTracker :
     7     public CRectTracker
     8 {
     9 public:
    10     CVSCRectTracker(LPCRECT lpSrcRect, UINT nStyle);
    11     /******************************************************
    12         In order to restrict the tracker's rectangle within
    13         the range of the screen.
    14 
    15         Override this method, when the rectangle's
    16         device coordinates are beyond the range of
    17         the device (the screen), refuse to make the change.
    18     ******************************************************/
    19     virtual void OnChangedRect(const CRect& rectOld);
    20 };

    CVSCRectTracker.cpp

     1 #include "CVSCRectTracker.h"
     2 
     3 CVSCRectTracker::CVSCRectTracker(LPCRECT lpSrcRect, UINT nStyle) : CRectTracker(lpSrcRect, nStyle)
     4 {
     5 }
     6 
     7 void CVSCRectTracker::OnChangedRect(const CRect& rectOld)
     8 {
     9     // get screen metrics
    10     LONG cxScreen(::GetSystemMetrics(SM_CXSCREEN)), cyScreen(::GetSystemMetrics(SM_CYSCREEN));
    11     // If coordinates are out of the screen, reset the rectangle to its last position
    12     if (m_rect.left <= 0 || m_rect.right >= cxScreen)
    13         m_rect.left = m_rectLast.left, m_rect.right = m_rectLast.right;
    14     if (m_rect.top <= 0 || m_rect.bottom >= cyScreen)
    15         m_rect.top = m_rectLast.top, m_rect.bottom = m_rectLast.bottom;
    16     CRectTracker::OnChangedRect(m_rect);
    17 }

    Canvas.cpp

    CRectTracker *tracker = new CVSCRectTracker(&rect, CRectTracker::resizeOutside | CRectTracker::dottedLine);

    然后你该怎么用怎么用就可以了

  • 相关阅读:
    树状数组
    LCA最近公共祖先
    ordered_set
    马拉车算法
    数论
    图论
    其他
    线段树
    序列自动机
    优先队列
  • 原文地址:https://www.cnblogs.com/qrlozte/p/4743944.html
Copyright © 2011-2022 走看看