I am new to MFC and trying to solve this MFC line drawing problem but I keep get issue with access. first of all, this is problem.
- By using Left Mouse Button drag, draw a line.(meaning just like drawing Line on App Paint)
- All the line should be stored in array.
- All the line should not be erased when client window size change.
What I am trying to do is, create a Line Class.
CLine.cpp
#include "pch.h"
#include "CLine.h"
CLine::CLine()
{
m_start = NULL;
m_end = NULL;
}
void CLine::draw(CDC* pDC)
{
pDC->MoveTo(m_start);
pDC->LineTo(m_end);
}
in ChildView.h I have created,
int LineCount;
CLine* CLine[100];
ChildView.cpp
void CChildView::OnPaint()
{
CPaintDC dc(this);
if (LMouseDown)
{
for (int i = 0; i <= LineCount; i++)
{
CLine[i]->draw(&dc);
}
}
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
CLine[LineCount]->m_start = point;
CLine[LineCount]->m_end = point;
LMouseDown = true;
CWnd::OnLButtonDown(nFlags, point);
}
void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
if (LMouseDown)
{
CLine[LineCount]->m_end = point;
}
CWnd::OnMouseMove(nFlags, point);
}
void CChildView::OnLButtonUp(UINT nFlags, CPoint point)
{
if (LMouseDown)
{
CLine[LineCount]->m_end = point;
LMouseDown = false;
LineCount++;
}
CWnd::OnLButtonUp(nFlags, point);
}
When I run this code, as soon as I click some where on window, I get error Exception thrown: write access violation
.
What do I have to do, in order to use this array properly to save my Line information?