如何让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);

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值