Search code examples
c++winapidrawing

MoveToEx and LineTo doesnt draw using MAKEPOINTS stored in vertices(push_back) win32 c++


unless I set, for example, LineTo(hdc,50,50) it will draw from point(50,50) but no matter what I do I can't make it work using MAKEPOINTS and store it in Vertex class(class is correct)

please help

case WM_LBUTTONDOWN:
        {
            HDC hdc = GetDC(hWnd);
            POINTS clickPoint = MAKEPOINTS(lParam); 
            connectLines.push_back(Vertex(clickPoint.x, clickPoint.y));                 
            MoveToEx(hdc, clickPoint.x, clickPoint.y, NULL);            
            LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
            ReleaseDC(hWnd, hdc);
        }
        break;

edit: I'm adding WM_PAINT and Vertex class and Vertex header

case WM_PAINT:
        {
            PAINTSTRUCT ps;         
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...

            for (Vertex points : connectLines) {
                HPEN pen = CreatePen(PS_SOLID, 7, RGB(255, 0, 0));
                HGDIOBJ oldPen = SelectObject(hdc, pen);
                SelectObject(hdc, oldPen);
                DeleteObject(pen);
            }
            EndPaint(hWnd, &ps);
        }
        break;

Vertex header

#pragma once
class Vertex
{
public:
    Vertex();
    Vertex(int x, int y);
    Vertex(const Vertex& other);

    // Accessors
    int GetX() const;
    void SetX(const int x);
    int GetY() const;
    void SetY(const int y);

    // Assigment operator
    Vertex& operator= (const Vertex& rhs);

    bool operator== (const Vertex& rhs) const;

    const Vertex operator+ (const Vertex& rhs) const;

private:
    int _x;
    int _y;
};

Vertex header

#include "Vertex.h"

Vertex::Vertex()
{
    _x = 0.0f;
    _y = 0.0f;
}

Vertex::Vertex(int x, int y)
{
    _x = x;
    _y = y;
}

Vertex::Vertex(const Vertex& other)
{
    _x = other.GetX();
    _y = other.GetY();
}

int Vertex::GetX() const
{
    return _x;
}

void Vertex::SetX(const int x)
{
    _x = x;
}

int Vertex::GetY() const
{
    return _y;
}

void Vertex::SetY(const int y)
{
    _y = y;
}

Vertex& Vertex::operator=(const Vertex& rhs)
{
    // Only do the assignment if we are not assigning
    // to ourselves
    if (this != &rhs)
    {
        _x = rhs.GetX();
        _x = rhs.GetY();
    }
    return *this;
}

// The const at the end of the declaration for '==' indicates that this operation does not change
// any of the member variables in this class

bool Vertex::operator==(const Vertex& rhs) const
{
    return (_x == rhs.GetX() && _y == rhs.GetY());
}

// You can see three different uses of 'const' here:
//
// The first const indicates that the method changes the return value, but it is not moved in memory
// The second const indicates that the parameter is passed by reference, but it is not modified
// The third const indicates that the operator does not change any of the memory variables in the class

const Vertex Vertex::operator+(const Vertex& rhs) const
{
    return Vertex(_x + rhs.GetX(), _y + rhs.GetY());
}

For some reason which is unknown to me It doesn't want to take "x" and "y" however I have this problem only with this program with calculations it works like a charm.

Thank you for help


Solution

  • In your code, the line start and end at the same point. So you fail to draw line. if your Vertex class function like this:

    void Vertex::SetX(const int x)
    {
        _x = x;
    }
    
    void Vertex::SetY(const int y)
    {
        _y = y;
    }
    
    

    Draw a continous lines segment based on the click the mouse, you can code like this:

    static POINT ptPrevious = { 0,0 };
    static bool flag = false;
    Vertex temp;
    ...
    case WM_LBUTTONDOWN:
            HDC hdc = GetDC(hWnd);
            POINTS clickPoint = MAKEPOINTS(lParam);
            if (flag == false) {
                ptPrevious.x = clickPoint.x;
                ptPrevious.y = clickPoint.y;
                flag = true;
            }
            //store the point in connectLines
            temp.SetX(clickPoint.x);
            temp.SetY(clickPoint.y);
            connectLines.push_back(temp);
    
            MoveToEx(hdc, ptPrevious.x, ptPrevious.y, NULL);
            LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
    
            //record previous point
            ptPrevious.x = clickPoint.x;
            ptPrevious.y = clickPoint.y;
    
            ReleaseDC(hWnd, hdc);
            break;
    

    If you need to save the location of the mouse click, you need to add it yourself. And here provides a sample Drawing with the Mouse, but the variables should be static variable.