【发布时间】:2013-07-04 07:01:38
【问题描述】:
我需要使用 MFC 制作交互式图表
它有点像一个均衡器控件,用户应该能够点击均衡器上的一个点,拖动它来改变它的 y 轴值
我也刚开始学习MFC
到目前为止,我已经在OnPaint() 函数中使用CPaintDC 在对话框中绘制图形。现在图表非常简单,矩形边框,填充白色,图表上有 4 个点。我使用OnMouseMove() 函数来了解光标是否在图形区域内,并使用OnLButtonDown() 函数来了解用户点击的位置。如果用户单击了一个位置,这意味着我想更改该位置图形点的 y 轴值,我使用Invalidate() 重新绘制图形并在OnLButtonDown() 内调用OnPaint()。但是,每次图表必须更新时,我都会看到闪烁。现在这不是问题,但我需要扩展此图,使其至少有 64 个可变点,并且能够通过拖动而不是单击我想要它去的位置来更改一个点的 y 轴值。闪烁问题会随着我增加点数和图形外观的复杂性而增加吗?稍后,该图表将需要具有轴、网格线、标签等。闪烁是我应该关心的吗?有什么办法可以预防吗?
----更新----
这就是我根据我对 CodeDreamer 建议的理解更新我的 OnPaint() 函数的方式
void Cgraph_on_dlgboxDlg::OnPaint()
{
CPaintDC dc_blt(this);
CDC dc;
CBitmap bmpDC;
CRect rcClient;
GetClientRect(rcClient);
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
{
CDialogEx::OnPaint();
}
dc.CreateCompatibleDC(&dc);
bmpDC.CreateCompatibleBitmap(&dc, theGraph.width,theGraph.height );
dc.SelectObject(&bmpDC);
CPen pen;
COLORREF pencolour = RGB(0, 0, 0);
COLORREF brushcolour = RGB(0, 0, 255);
COLORREF graphColour = RGB(0, 0, 150);
// Draw boarder
pen.CreatePen(PS_SOLID, 3, pencolour);
// CBrush brush(HS_CROSS, brushcolour);
dc.SetBkMode(TRANSPARENT);
dc.SetMapMode(MM_TEXT);
dc.SetViewportOrg(theGraph.x1, theGraph.y1);
dc.SelectObject(&pen);
// Draw graph boundary
CPoint point1(0,0);
point1.x = 0;
point1.y = 0;
CPoint point2(0,0);
point2.x = point1.x + theGraph.width;
point2.y = point1.y + theGraph.height;
dc.Rectangle(CRect(point1, point2));
pen.DeleteObject();
// Draw Horizontal at 0
pen.CreatePen(PS_SOLID, 1, pencolour);
dc.SelectObject(&pen);
dc.MoveTo(0, theGraph.height - ORG_DIST_FROM_BOTTOM);
dc.LineTo(theGraph.width, theGraph.height - ORG_DIST_FROM_BOTTOM);
pen.DeleteObject();
dc.SetViewportOrg(theGraph.x1, theGraph.y1 + theGraph.height - ORG_DIST_FROM_BOTTOM); // dc.SetViewportOrg() always works relative to the clinet origin
// Draw graph line
pen.CreatePen(PS_SOLID, 2, graphColour);
dc.SelectObject(&pen);
for(int i = 0; i<NUM_OF_SECTIONS_IN_GRAPH; i++){
dc.MoveTo(graphSamplePoints[i].x, graphSamplePoints[i].y);
dc.LineTo(graphSamplePoints[i+1].x, graphSamplePoints[i+1].y);
}
// draw circles at graph sample points
for(int i = 0; i<NUM_OF_POINTS_IN_GRAPH; i++){
CIRCLE(dc, graphSamplePoints[i].x, graphSamplePoints[i].y, GRP_SMP_RAD);
}
// dc_blt.BitBlt(0,0,rcClient.Width(), rcClient.Height(), &dc, 0, 0, SRCCOPY);
dc_blt.BitBlt(theGraph.x1,theGraph.y1,theGraph.width, theGraph.height, &dc, 0, 0, SRCCOPY);
}
我需要多次更改视口的来源,我的猜测是这可能是错误的原因之一。欢迎提出任何建议。
这就是我的输出在没有双缓冲的情况下的样子
这就是我尝试双缓冲时的样子
【问题讨论】:
标签: visual-studio-2010 visual-studio visual-c++ mfc visual-c++-2010