我编写了一个简单的MFC应用程序来在对话框中绘制多边形,complete code being here。
代码打开一个对话框,默认标准灰色背景,定义
std::vector<CPoint> m_LeftPolygon;
然后执行以下操作:
void DrawPitons(std::vector<CPoint> points, COLORREF rgbColor, HDC hDC)
{
static unsigned short XDiff[7] = { 5,4,4,3,2,1,0 },
YDiff[7] = { 0,1,2,3,4,4,5 };
for (unsigned pnt = 0; pnt < points.size(); pnt++)
{
SetPixel(hDC, points[pnt].x, points[pnt].y, rgbColor);
for (unsigned short i = 0; i < 7; i++)
{
SetPixel(hDC, points[pnt].x + XDiff[i], points[pnt].y + YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x + XDiff[i], points[pnt].y - YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x - XDiff[i], points[pnt].y + YDiff[i], rgbColor);
SetPixel(hDC, points[pnt].x - XDiff[i], points[pnt].y - YDiff[i], rgbColor);
}
}
}
void CROIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // device context for painting
HDC hDC = dc.GetSafeHdc();
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)),
oldPen = (HPEN)SelectObject(hDC, hPen);
SetROP2(hDC, R2_COPYPEN);
// Draw the polygon
if (m_LeftPolygon.size() > 1)
{
BOOL stat = MoveToEx(hDC, (int)m_LeftPolygon[0].x, (int)m_LeftPolygon[0].y, NULL);
for (size_t index = 1; index < m_LeftPolygon.size(); index++)
stat = LineTo(hDC, (int)m_LeftPolygon[index].x, (int)m_LeftPolygon[index].y);
stat = LineTo(hDC, (int)m_LeftPolygon[0].x, (int)m_LeftPolygon[0].y);
}
// Draw the control points
DrawPitons(m_LeftPolygon, RGB(255, 0, 0), hDC);
SelectObject(hDC, oldPen);
DeleteObject(hPen);
}
}
void CROIDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
m_LeftPolygon.push_back(point);
SendMessage(WM_PAINT);
}
出于某种原因,没有画出任何东西,但我希望在灰色背景上用红色绘制岩钉和连接线。
我错过了什么?