0

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.

  1. By using Left Mouse Button drag, draw a line.(meaning just like drawing Line on App Paint)
  2. All the line should be stored in array.
  3. 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?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Use the debugger to see where the problem is. My guess: you did not initialize the CLine array with CLine objects, and so your code dereferences garbage pointers. – j6t May 13 '20 at 21:13
  • 1
    Welcome to Stack Overflow! You have left out a lot of important code from your question. However, the `CDC::MoveTo()` and `CDC::LineTo()` functions take a single `POINT` as the argument, not an array of `POINT`, or even a pointer to a `POINT`. You maybe are thinking of the [`CDC::PolyLine()`](https://learn.microsoft.com/en-us/cpp/mfc/reference/cdc-class?view=vs-2019#polyline) function. – Adrian Mole May 13 '20 at 21:16
  • @j6t debugger says "CLine[LineCount]->m_start = point;" this line, i get "Exception thrown: write access violation." I think, i cannot overwrite variable m_start in CLine Class. I don't know if I understand concept of initializing array correctly, but I tried using "CLine* CLine[100] = {};" but I still get the Same error at the same point... – mapleclover7 May 13 '20 at 21:32
  • @AdrianMole Thanks, However, I am trying to draw multiple indivual lines, not consecutive connecting lines. – mapleclover7 May 13 '20 at 21:34
  • @j6t also I tried inside OnLButtonDown i added CLine[LineCount] = new CLine(); but I get this error, "expected a type specifier" red underline under "CLine();" – mapleclover7 May 13 '20 at 21:39
  • You should probably consider a different implementation here. Drawing these lines should happen in response to your view's WM_PAINT handler, also known as your view's OnDraw method. This sounds somewhat similar to the old MFC Scribble sample, which might give you some additional pointers. https://learn.microsoft.com/en-us/cpp/porting/porting-guide-mfc-scribble?view=vs-2019 – Ed Dore May 14 '20 at 03:30
  • The debugger tells you where exactly the write access violation.happens. Learn to use the debugger, it will save you countless hours. The debugger integrated in Visual Studio is really good and easy to use. – Jabberwocky May 14 '20 at 06:38
  • `CLine* CLine[100];` is an array of pointers to `Cline`, each of which points nowhere. You probably rather want an array of `CLine`: `CLine CLine[100]`. And replace all `CLine[xxx]->` with `CLine[xxx].` – Jabberwocky May 14 '20 at 06:40
  • 1
    And don't use a type named `CLine` as well as a variable named `CLine` as well. Use different names. That would fix your error about "expected a type specifier". – j6t May 14 '20 at 07:23
  • 1
    This question is unrelated to MFC. You're simply trying to access objects that do not exist. How to instantiate an object is explained in *any* introductory material on C++. Pick up a book or more from [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/1889329). – IInspectable May 14 '20 at 09:39

0 Answers0