Click here to Skip to main content
15,895,011 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Determine if VC++ 2005 SP1 runtime is installed? [modified] Pin
mid=57418-Sep-07 16:37
mid=57418-Sep-07 16:37 
QuestionLine drawing in MFC Pin
DGit8-Sep-07 12:30
DGit8-Sep-07 12:30 
AnswerRe: Line drawing in MFC Pin
Christian Graus8-Sep-07 12:32
protectorChristian Graus8-Sep-07 12:32 
GeneralRe: Line drawing in MFC Pin
DGit8-Sep-07 12:49
DGit8-Sep-07 12:49 
GeneralRe: Line drawing in MFC Pin
Christian Graus8-Sep-07 16:12
protectorChristian Graus8-Sep-07 16:12 
GeneralRe: Line drawing in MFC Pin
bob169728-Sep-07 17:12
bob169728-Sep-07 17:12 
GeneralRe: Line drawing in MFC Pin
DGit10-Sep-07 6:42
DGit10-Sep-07 6:42 
AnswerRe: Line drawing in MFC [modified] Pin
bob169728-Sep-07 13:01
bob169728-Sep-07 13:01 
Rubberbanding the line is just part of what you need. Once you finish tracking the line and release the mouse button, your OnPaint or OnDraw will need to keep redrawing the graphics primitives (lines, arcs, ellipses, rects, etc...) whenever the area you've drawn to is invalidated.

Ivor Horton has a great introduction to this in his Visual C++ books. He goes through what is needed to store the coordinates of graphics primitives in the document in collection classes etc...

But, with that disclaimer aside, here's a quick and dirty demo on how to rubberband a line (remember, this only cover the rubberbanding and not redrawing your window when you drag another window over it etc...)...

Start a new Visual C++ Doc/View project, nothing fancy using a single document and deriving from CView for simplicity.

Use the ClassWizard to add these handlers to your view class and ensure the code looks something like this...

// in your views .h file
private:
void DrawRubberBand(CPoint pointCurrent);
CPoint m_pointLast;
CPoint m_pointStart;
BOOL m_bTracking;

// in your views .cpp file
void CRubberBandView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

if (nFlags & MK_LBUTTON) {
DrawRubberBand(point);
m_bTracking=TRUE;
m_pointLast=point;
}

CView::OnMouseMove(nFlags, point);
}

void CRubberBandView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
m_pointStart=point;
m_pointLast=point;

CView::OnLButtonDown(nFlags, point);
}

void CRubberBandView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

ReleaseCapture();
m_bTracking=FALSE;
DrawRubberBand(point); // Erase the last line

CView::OnLButtonUp(nFlags, point);
}

void CRubberBandView::DrawRubberBand(CPoint pointCurrent)
{
CClientDC dc(this);

dc.SetROP2(R2_NOT);
dc.SelectStockObject(BLACK_PEN);

// Do we need to erase last line
if (m_bTracking) {

dc.MoveTo(m_pointStart);
dc.LineTo(m_pointLast);
}

// Draw the current line
dc.MoveTo(m_pointStart);
dc.LineTo(pointCurrent);
}



-- modified at 23:14 Saturday 8th September, 2007
GeneralRe: Line drawing in MFC Pin
DGit8-Sep-07 13:26
DGit8-Sep-07 13:26 
GeneralRe: Line drawing in MFC Pin
bob169728-Sep-07 13:31
bob169728-Sep-07 13:31 
GeneralRe: Line drawing in MFC Pin
DGit9-Sep-07 8:59
DGit9-Sep-07 8:59 
QuestionGet User Picture Pin
Perspx8-Sep-07 12:12
Perspx8-Sep-07 12:12 
AnswerRe: Get User Picture Pin
Mark Salsbery8-Sep-07 12:28
Mark Salsbery8-Sep-07 12:28 
Questionstrtok problem Pin
gizmokaka8-Sep-07 10:41
gizmokaka8-Sep-07 10:41 
AnswerRe: strtok problem Pin
Michael Dunn8-Sep-07 11:16
sitebuilderMichael Dunn8-Sep-07 11:16 
AnswerRe: strtok problem Pin
Robert Surtees8-Sep-07 11:38
Robert Surtees8-Sep-07 11:38 
QuestionMessage Loop in DLL Pin
vaibhav khattri8-Sep-07 10:26
vaibhav khattri8-Sep-07 10:26 
AnswerRe: Message Loop in DLL Pin
Mark Salsbery8-Sep-07 11:17
Mark Salsbery8-Sep-07 11:17 
QuestionVisual C++.net 2003 missing project property pages Pin
markdesmarais20078-Sep-07 9:33
markdesmarais20078-Sep-07 9:33 
QuestionLPSTR_TEXTCALLBACK Pin
paper678-Sep-07 7:03
paper678-Sep-07 7:03 
AnswerRe: LPSTR_TEXTCALLBACK Pin
Mark Salsbery8-Sep-07 8:32
Mark Salsbery8-Sep-07 8:32 
AnswerRe: LPSTR_TEXTCALLBACK Pin
Michael Dunn8-Sep-07 8:40
sitebuilderMichael Dunn8-Sep-07 8:40 
QuestionSOLVED: Problem with events MFC [modified] Pin
progDes8-Sep-07 6:32
progDes8-Sep-07 6:32 
AnswerRe: Problem with events MFC Pin
Mark Salsbery8-Sep-07 7:31
Mark Salsbery8-Sep-07 7:31 
GeneralRe: Problem with events MFC Pin
progDes8-Sep-07 7:38
progDes8-Sep-07 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.