Recently in the attempt to make a tool like QQ, one of the functions is to make a selection box, the natural use of CRectTracker
But one of the key things is that when you drag a crecttracker, you can't let CRectTracker "move out" the screen, or there's a black block.
What to do? Searched, and did not find any useful information (may be my search skills too low)
Go to MSDN and look at CRectTracker's documentation, and think of the one method that should be override.
Later pondering a bit, fix, directly override Crecttracker::onchangedrect can, code directly paste below:
CVSCRectTracker.h
1 #pragmaOnce2 3#include <afxext.h>4 5 //Canvas CRectTracker6 classCvscrecttracker:7 PublicCRectTracker8 {9 Public:Ten Cvscrecttracker (Lpcrect lpsrcrect, UINT nstyle); One /****************************************************** A In order to restrict the tracker ' s rectangle within - The range of the screen . - the Override This method when the rectangle ' s - device coordinates is beyond the range of - the device (the screen), refuse to make the change. - ******************************************************/ + Virtual voidOnchangedrect (Constcrect&rectold); -};
CVSCRectTracker.cpp
1#include"CVSCRectTracker.h"2 3 Cvscrecttracker::cvscrecttracker (Lpcrect lpsrcrect, UINT nstyle): CRectTracker (Lpsrcrect, NStyle)4 {5 }6 7 voidCvscrecttracker::onchangedrect (Constcrect&rectold)8 {9 //Get screen metricsTen LONG Cxscreen (:: GetSystemMetrics (Sm_cxscreen)), Cyscreen (:: GetSystemMetrics (Sm_cyscreen)); One //If coordinates is out of the screens, reset the rectangle to their last position A if(M_rect.left <=0|| M_rect.right >=Cxscreen) -M_rect.left = m_rectlast.left, M_rect.right =M_rectlast.right; - if(M_rect.top <=0|| M_rect.bottom >=Cyscreen) theM_rect.top = m_rectlast.top, M_rect.bottom =M_rectlast.bottom; - Crecttracker::onchangedrect (m_rect); -}
Canvas.cpp
New Cvscrecttracker (&rect, Crecttracker::resizeoutside | CRectTracker::d ottedline);
And how you can use it.
How to make CRectTracker's m_rect beyond a certain range, such as screen or the scope of a document