MoveToEx和LineTo不使用存储在顶点(push_back)win32 c++中的MAKEPOINTS进行绘制
MoveToEx and LineTo doesnt draw using MAKEPOINTS stored in vertices(push_back) win32 c++
除非我设置了LineTo(hdc,50,50(,否则它将从点(50,50
请帮助
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头
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"answers"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;
如果你需要保存鼠标点击的位置,你需要自己添加。这里提供了一个使用鼠标绘制的示例,但变量应该是static
变量。
试试类似的东西:
vector<Vertex> connectLines;
void addClickPoint(HWND hWnd, int X, int Y)
{
connectLines.push_back(Vertex(X, Y));
InvalidateRect(hWnd, NULL, TRUE);
}
...
case WM_LBUTTONDOWN:
{
POINTS clickPoint = MAKEPOINTS(lParam);
addClickPoint(hWnd, clickPoint.x, clickPoint.y);
/* alternatively:
addClickPoint(hWnd, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
*/
}
break;
// optionally:
case WM_MOUSEMOVE:
{
if (wParam & MK_LBUTTON)
{
POINTS clickPoint = MAKEPOINTS(lParam);
addClickPoint(hWnd, clickPoint.x, clickPoint.y);
/* alternatively:
addClickPoint(hWnd, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
*/
}
}
break;
//
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
...
if (connectLines.size() > 1)
{
MoveToEx(hdc, connectLines[0].GetX(), connectLines[0].GetY(), NULL);
for(size_t i = 1; i < connectLines.size(); ++i)
LineTo(hdc, connectLines[i].GetX(), connectLines[i].GetY());
}
...
EndPaint(hWnd, &ps);
}
break;
相关文章:
- Win32编译器选项和内存分配
- C++win32 API创建多个类似视口的窗口
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- 检测win32服务创建和删除的最佳方法
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- 将 win32 hbitmap 转换为 winrt softwarebitmap
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- Alt+Enter 在 Win32 应用中,管理大小调整和分辨率
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 从预处理器获取 Windows 版本(C++ Win32)
- Issues with Win32 ReadProcessMemory API
- Win32 发送输入鼠标移动滞后并冻结
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 将 Win32/WinAPI 应用程序移植到 wxWidgets
- Win32 API 控制台光标在 WriteConsole 后不移动
- C++ Win32 Threads
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库