gpt4 book ai didi

c++ - MoveToEx 和 LineTo 不使用存储在顶点 (push_back) win32 c++ 中的 MAKEPOINTS 绘制

转载 作者:行者123 更新时间:2023-11-28 04:05:17 25 4
gpt4 key购买 nike

除非我设置,例如,LineTo(hdc,50,50) 它将从点 (50,50) 绘制,但无论我做什么,我都无法使用 MAKEPOINTS 使其工作并将其存储在 Vertex 类中(类是正确的)

请帮忙

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;

编辑:我正在添加 WM_PAINT 和 Vertex 类以及 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;

顶点标题

#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;
};

顶点标题

#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());
}

出于某种我不知道的原因它不想使用“x”和“y”但是我只有这个程序有这个问题,它的计算就像一个魅力。

谢谢你的帮助

最佳答案

在您的代码中,该行在同一点开始和结束。所以你没有画线。如果您的 Vertex 类函数如下:

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

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

根据鼠标点击绘制连续的线段,可以这样编码:

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;

如果需要保存鼠标点击的位置,需要自己添加。和 here提供了一个用鼠标绘制的示例,但变量应该是 static 变量。

关于c++ - MoveToEx 和 LineTo 不使用存储在顶点 (push_back) win32 c++ 中的 MAKEPOINTS 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58829729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com