实际的大小按比例绘制网格线,当窗口的大小放大时,网格线的数目就会自动增多,窗口缩
小时,网格线数目就会减小。 说到底,就是视图窗口的每个网格面积大小保持一定的范围
之内,这里每个网格面积指一个网格所占屏幕的面积大小保持一定。
没有什么思路 ,大家说说看。主要问题是怎么知道窗口的大小,怎么根据窗口的大小画网格线
18 个解决方案
#1
你可以放一个Picture控件,让它不可见
重载OnSize()让Picture控件随着窗口的变化而改变大小,
绘制的坐标轴及其网格根据Picture的lefttop,rightbottom来进行绘制,只要你设置好比例就可以了
重载OnSize()让Picture控件随着窗口的变化而改变大小,
绘制的坐标轴及其网格根据Picture的lefttop,rightbottom来进行绘制,只要你设置好比例就可以了
#2
如果是视图程序的话,就直接根据视图的lefttop,rightbottom来进行绘制,同样设置好比例就可以了
#3
实际上就是根据屏幕的实际的 像素画 多少个像素画一个网格线
怎么获得屏幕的像素
怎么获得屏幕的像素
#4
//获取屏幕的宽度和高度
int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
#5
使用平行投影
将一个单位对应到窗口系统上的一个像素上。
也就是说,对于你的投影体,当窗口大小改变的时候,设置它的宽为窗口框,高为窗口高。
那么不管窗口怎么变,一个像素就一定等于一个单位
将一个单位对应到窗口系统上的一个像素上。
也就是说,对于你的投影体,当窗口大小改变的时候,设置它的宽为窗口框,高为窗口高。
那么不管窗口怎么变,一个像素就一定等于一个单位
#6
不好意思,没看清楚。 我以为是说OpenGL或者D3D
如果你是在CView派生类里面, 那么很简单。
每次RESIZE后,计算出View的中心点,做十字交叉线。
然后向横、纵两个方向扩展经纬线。 设置间隔一定就可以了。 只要循环出窗口范围就停止。
如果你是在CView派生类里面, 那么很简单。
每次RESIZE后,计算出View的中心点,做十字交叉线。
然后向横、纵两个方向扩展经纬线。 设置间隔一定就可以了。 只要循环出窗口范围就停止。
#7
根据你视图显示东西的比例来设置 你网格线的间距 你就在 你视图上 上部和左部 预留20 宽度的区域画线 就用MOVETO LINETO 在这些地方画刻度,如果要长短刻度 那就循环中每过8个线或10个线 画条较长的线 同时在这些较长线上TEXTOUT坐标值,而线与线的间距 就看你视图显示的比例来定 而 这样用了边上的区域 那你要显示的画面的视图区域的起点 可以重20,20 位置开始 这样也可以实现坐标
#8
原点定在哪?
#9
不懂,帮顶
#10
坐标轴的原点在左下角
#11
在onPaint里,
用GetWindowRect/GetClientRect之类获取绘制区大小,定个比例数字,然后用dc->moveto/lineto之类划线就可以了
用GetWindowRect/GetClientRect之类获取绘制区大小,定个比例数字,然后用dc->moveto/lineto之类划线就可以了
#12
给你点源码参考参考,不过要随屏幕变化就要自己修改了
void CMy111View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC *pDc = GetDC();
DrawResponseGraph(pDc);
// Do not call CFormView::OnPaint() for painting messages
}
void CMy111View::DrawResponseGraph(CDC *pDc)
{
int i;
CBitmap curveBitmap;
// float cutTime;
// float cutValue;
int nTextHeight;
int nTextWidth;
CString AxisText;
CString AxisText1;
CString cursorText;
CString cutText;
CString responseText;
TEXTMETRIC Text;
CPen pen;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
curveBitmap.CreateCompatibleBitmap(pDc,m_graphWidth,m_graphHeight);
MemDC.SelectObject(&curveBitmap);
MemDC.FillSolidRect(0,0,m_graphWidth,m_graphHeight,pDc->GetPixel(5,5));
pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptZeroPoint.x,m_ptEndPoint.y);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y);
//画坐标轴和刻度
MemDC.GetTextMetrics(&Text);
nTextWidth = Text.tmAveCharWidth;
nTextHeight = Text.tmHeight;
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(150,150,150));
MemDC.SelectObject(&pen);
AxisText.Format("%dT/(s)",m_sampStart*SAMPLINGTIME/1000);
MemDC.TextOut(m_ptZeroPoint.x+-nTextWidth-15,m_ptZeroPoint.y+2,AxisText);
AxisText1.Format("V/(mm/s)");
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth-50,m_ptGraphStart.y-m_graphHeight-20,AxisText1);
ASSERT(RATIO > 0);
for (i = 50; i <= 800; i+=50)
{
MemDC.MoveTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y);
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptEndPoint.y);
AxisText.Format("%3d",m_sampStart*SAMPLINGTIME/1000+(i*RATIO*SAMPLINGTIME)/1000);
MemDC.TextOut(m_ptZeroPoint.x+i-nTextWidth-5,m_ptZeroPoint.y+2,AxisText);
}
for (i = 10; i <= 50; i+=10)
{
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-i*2);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y-i*2);
AxisText.Format("%3d",i*10);
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth*3-4,m_ptZeroPoint.y-i*2-5,AxisText);
}
if (m_bPaintCoordinate)
{
pen.DeleteObject();
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-m_coordinateData[0]*2);
for (i = 1; i < GRAPHSAMPLINGNUM; i++)
{
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y-m_coordinateData[i]*2);
}
}
//画鼠标坐标
pen.DeleteObject();
pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
if (m_ptCursor.x > m_ptZeroPoint.x && m_ptCursor.x < m_ptEndPoint.x
&& m_ptCursor.y < m_ptZeroPoint.y && m_ptCursor.y > m_ptEndPoint.y)
{
cursorText.Format("(%2.2f,%d)",(float)m_sampStart*SAMPLINGTIME/1000+(float)(m_ptCursor.x - m_ptZeroPoint.x)*RATIO*SAMPLINGTIME/1000,(m_ptZeroPoint.y - m_ptCursor.y)/2*10);
if (m_ptCursor.x > m_ptGraphStart.x+m_graphWidth - 100)
{
MemDC.TextOut(m_ptCursor.x-70,m_ptCursor.y+10,cursorText);
}
else
{
MemDC.TextOut(m_ptCursor.x+20,m_ptCursor.y+10,cursorText);
}
//画参考线
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptCursor.x-10,m_ptCursor.y);
MemDC.LineTo(m_ptCursor.x+10,m_ptCursor.y);
MemDC.MoveTo(m_ptCursor.x,m_ptCursor.y-10);
MemDC.LineTo(m_ptCursor.x,m_ptCursor.y+10);
}
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
pDc->BitBlt(m_ptGraphStart.x,m_ptGraphStart.y,m_graphWidth,m_graphHeight,&MemDC,0,0,SRCCOPY);
}
void CMy111View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC *pDc = GetDC();
DrawResponseGraph(pDc);
// Do not call CFormView::OnPaint() for painting messages
}
void CMy111View::DrawResponseGraph(CDC *pDc)
{
int i;
CBitmap curveBitmap;
// float cutTime;
// float cutValue;
int nTextHeight;
int nTextWidth;
CString AxisText;
CString AxisText1;
CString cursorText;
CString cutText;
CString responseText;
TEXTMETRIC Text;
CPen pen;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
curveBitmap.CreateCompatibleBitmap(pDc,m_graphWidth,m_graphHeight);
MemDC.SelectObject(&curveBitmap);
MemDC.FillSolidRect(0,0,m_graphWidth,m_graphHeight,pDc->GetPixel(5,5));
pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptZeroPoint.x,m_ptEndPoint.y);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y);
//画坐标轴和刻度
MemDC.GetTextMetrics(&Text);
nTextWidth = Text.tmAveCharWidth;
nTextHeight = Text.tmHeight;
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(150,150,150));
MemDC.SelectObject(&pen);
AxisText.Format("%dT/(s)",m_sampStart*SAMPLINGTIME/1000);
MemDC.TextOut(m_ptZeroPoint.x+-nTextWidth-15,m_ptZeroPoint.y+2,AxisText);
AxisText1.Format("V/(mm/s)");
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth-50,m_ptGraphStart.y-m_graphHeight-20,AxisText1);
ASSERT(RATIO > 0);
for (i = 50; i <= 800; i+=50)
{
MemDC.MoveTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y);
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptEndPoint.y);
AxisText.Format("%3d",m_sampStart*SAMPLINGTIME/1000+(i*RATIO*SAMPLINGTIME)/1000);
MemDC.TextOut(m_ptZeroPoint.x+i-nTextWidth-5,m_ptZeroPoint.y+2,AxisText);
}
for (i = 10; i <= 50; i+=10)
{
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-i*2);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y-i*2);
AxisText.Format("%3d",i*10);
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth*3-4,m_ptZeroPoint.y-i*2-5,AxisText);
}
if (m_bPaintCoordinate)
{
pen.DeleteObject();
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-m_coordinateData[0]*2);
for (i = 1; i < GRAPHSAMPLINGNUM; i++)
{
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y-m_coordinateData[i]*2);
}
}
//画鼠标坐标
pen.DeleteObject();
pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
if (m_ptCursor.x > m_ptZeroPoint.x && m_ptCursor.x < m_ptEndPoint.x
&& m_ptCursor.y < m_ptZeroPoint.y && m_ptCursor.y > m_ptEndPoint.y)
{
cursorText.Format("(%2.2f,%d)",(float)m_sampStart*SAMPLINGTIME/1000+(float)(m_ptCursor.x - m_ptZeroPoint.x)*RATIO*SAMPLINGTIME/1000,(m_ptZeroPoint.y - m_ptCursor.y)/2*10);
if (m_ptCursor.x > m_ptGraphStart.x+m_graphWidth - 100)
{
MemDC.TextOut(m_ptCursor.x-70,m_ptCursor.y+10,cursorText);
}
else
{
MemDC.TextOut(m_ptCursor.x+20,m_ptCursor.y+10,cursorText);
}
//画参考线
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptCursor.x-10,m_ptCursor.y);
MemDC.LineTo(m_ptCursor.x+10,m_ptCursor.y);
MemDC.MoveTo(m_ptCursor.x,m_ptCursor.y-10);
MemDC.LineTo(m_ptCursor.x,m_ptCursor.y+10);
}
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
pDc->BitBlt(m_ptGraphStart.x,m_ptGraphStart.y,m_graphWidth,m_graphHeight,&MemDC,0,0,SRCCOPY);
}
#13
先取窗口客户区矩形,然后用两个循环来画线。
画竖线时:
for (int x=0; x < 客户区矩形.right; x += 间隔)
{
pDC->MoveTo(x, 0);
pDC->LineTo(x, 客户区矩形.bottom);
}
画横线时:
for (int y=客户区矩形.bottom-1; y>=0; y -= 间隔)
{
pDC->MoveTo(0, y);
pDC->LineTo(客户区矩形.right, y);
}
另外还要响应WM_SIZE消息,当窗口高度改变时刷新窗口。
画竖线时:
for (int x=0; x < 客户区矩形.right; x += 间隔)
{
pDC->MoveTo(x, 0);
pDC->LineTo(x, 客户区矩形.bottom);
}
画横线时:
for (int y=客户区矩形.bottom-1; y>=0; y -= 间隔)
{
pDC->MoveTo(0, y);
pDC->LineTo(客户区矩形.right, y);
}
另外还要响应WM_SIZE消息,当窗口高度改变时刷新窗口。
#14
网格面积大小保持一定范围
就是横向、纵向象素数保持一定范围
就是横向、纵向象素数保持一定范围
#15
MSDN的DRAWCLI 示例
#16
m_timeaxis.m_dSecondsPrPixel =((double)(m_timeaxis.m_maxtime - m_timeaxis.m_mintime))/ (double)m_plotRect.Width();
void clPlot::DrawXAxisGrid(CDC * dc)
{
long yGrid = m_timeaxis.m_mintime;
long delta = (long)(80.0 + (long)(((m_timeaxis.m_dSecondsPrPixel)))*10);
long d10 = (long)(delta / 10.0);
// todo: delta switch
long diff = ((long)yGrid)%((long)delta);
yGrid = yGrid - diff;
CPen *old, pen(PS_SOLID, 1, m_gridColor);
CPen stick(PS_SOLID,0,RGB(0,0,0));
CPen mline(PS_SOLID,0,RGB(192,192,192));
for( long sx = m_timeaxis.m_mintime - diff; sx < m_timeaxis.m_maxtime; sx+=d10)
{
int off=3;
if((long)sx%(long)delta == 0)
{
off=5;
}
if(sx > m_timeaxis.m_mintime)
{
int x = (int)(m_plotRect.left + ((sx-m_timeaxis.m_mintime)/m_timeaxis.m_dSecondsPrPixel));
// int x = m_plotRect.right - ((sx-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_lSecondsPrPixel);
old = dc->SelectObject(&stick);
dc->MoveTo(CPoint(x,m_plotRect.bottom));
dc->LineTo(CPoint(x+off,m_plotRect.bottom));
dc->SelectObject(old);
old = dc->SelectObject(&mline);
dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
dc->LineTo(CPoint(x,m_plotRect.top+1));
dc->SelectObject(old);
}
}
old = dc->SelectObject(&pen);
while(yGrid <= m_timeaxis.m_maxtime)
{
int x = (int)(m_plotRect.left + ((yGrid-m_timeaxis.m_mintime)/m_timeaxis.m_dSecondsPrPixel));
if(yGrid > m_timeaxis.m_mintime && yGrid<m_timeaxis.m_maxtime)
{
dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
dc->LineTo(CPoint(x,m_plotRect.top+1));
}
// char b[100];
// sprintf(b, "%.0f", yGrid);
// dc->DrawText(b, CRect(m_clientRect.left, y-m_TextHeight/2,m_plotRect.left-5,y+m_TextHeight/2), DT_RIGHT|DT_BOTTOM);
yGrid += delta;
}
dc->SelectObject(old);
}
#17
class timeaxis
{
public:
CString m_szTitle; // time axis title;
int m_mintime; // min time
int m_maxtime; // max time
int m_iTimeMode; // axis grid and legend interval index
double m_dSecondsPrPixel;
timeaxis()
{
m_szTitle = "Time";
m_mintime = 0;
m_maxtime = 800;
m_iTimeMode=0;
m_dSecondsPrPixel=1;
}
};
#18
看看,给力的顶一下
#1
你可以放一个Picture控件,让它不可见
重载OnSize()让Picture控件随着窗口的变化而改变大小,
绘制的坐标轴及其网格根据Picture的lefttop,rightbottom来进行绘制,只要你设置好比例就可以了
重载OnSize()让Picture控件随着窗口的变化而改变大小,
绘制的坐标轴及其网格根据Picture的lefttop,rightbottom来进行绘制,只要你设置好比例就可以了
#2
如果是视图程序的话,就直接根据视图的lefttop,rightbottom来进行绘制,同样设置好比例就可以了
#3
实际上就是根据屏幕的实际的 像素画 多少个像素画一个网格线
怎么获得屏幕的像素
怎么获得屏幕的像素
#4
//获取屏幕的宽度和高度
int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
#5
使用平行投影
将一个单位对应到窗口系统上的一个像素上。
也就是说,对于你的投影体,当窗口大小改变的时候,设置它的宽为窗口框,高为窗口高。
那么不管窗口怎么变,一个像素就一定等于一个单位
将一个单位对应到窗口系统上的一个像素上。
也就是说,对于你的投影体,当窗口大小改变的时候,设置它的宽为窗口框,高为窗口高。
那么不管窗口怎么变,一个像素就一定等于一个单位
#6
不好意思,没看清楚。 我以为是说OpenGL或者D3D
如果你是在CView派生类里面, 那么很简单。
每次RESIZE后,计算出View的中心点,做十字交叉线。
然后向横、纵两个方向扩展经纬线。 设置间隔一定就可以了。 只要循环出窗口范围就停止。
如果你是在CView派生类里面, 那么很简单。
每次RESIZE后,计算出View的中心点,做十字交叉线。
然后向横、纵两个方向扩展经纬线。 设置间隔一定就可以了。 只要循环出窗口范围就停止。
#7
根据你视图显示东西的比例来设置 你网格线的间距 你就在 你视图上 上部和左部 预留20 宽度的区域画线 就用MOVETO LINETO 在这些地方画刻度,如果要长短刻度 那就循环中每过8个线或10个线 画条较长的线 同时在这些较长线上TEXTOUT坐标值,而线与线的间距 就看你视图显示的比例来定 而 这样用了边上的区域 那你要显示的画面的视图区域的起点 可以重20,20 位置开始 这样也可以实现坐标
#8
原点定在哪?
#9
不懂,帮顶
#10
坐标轴的原点在左下角
#11
在onPaint里,
用GetWindowRect/GetClientRect之类获取绘制区大小,定个比例数字,然后用dc->moveto/lineto之类划线就可以了
用GetWindowRect/GetClientRect之类获取绘制区大小,定个比例数字,然后用dc->moveto/lineto之类划线就可以了
#12
给你点源码参考参考,不过要随屏幕变化就要自己修改了
void CMy111View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC *pDc = GetDC();
DrawResponseGraph(pDc);
// Do not call CFormView::OnPaint() for painting messages
}
void CMy111View::DrawResponseGraph(CDC *pDc)
{
int i;
CBitmap curveBitmap;
// float cutTime;
// float cutValue;
int nTextHeight;
int nTextWidth;
CString AxisText;
CString AxisText1;
CString cursorText;
CString cutText;
CString responseText;
TEXTMETRIC Text;
CPen pen;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
curveBitmap.CreateCompatibleBitmap(pDc,m_graphWidth,m_graphHeight);
MemDC.SelectObject(&curveBitmap);
MemDC.FillSolidRect(0,0,m_graphWidth,m_graphHeight,pDc->GetPixel(5,5));
pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptZeroPoint.x,m_ptEndPoint.y);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y);
//画坐标轴和刻度
MemDC.GetTextMetrics(&Text);
nTextWidth = Text.tmAveCharWidth;
nTextHeight = Text.tmHeight;
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(150,150,150));
MemDC.SelectObject(&pen);
AxisText.Format("%dT/(s)",m_sampStart*SAMPLINGTIME/1000);
MemDC.TextOut(m_ptZeroPoint.x+-nTextWidth-15,m_ptZeroPoint.y+2,AxisText);
AxisText1.Format("V/(mm/s)");
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth-50,m_ptGraphStart.y-m_graphHeight-20,AxisText1);
ASSERT(RATIO > 0);
for (i = 50; i <= 800; i+=50)
{
MemDC.MoveTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y);
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptEndPoint.y);
AxisText.Format("%3d",m_sampStart*SAMPLINGTIME/1000+(i*RATIO*SAMPLINGTIME)/1000);
MemDC.TextOut(m_ptZeroPoint.x+i-nTextWidth-5,m_ptZeroPoint.y+2,AxisText);
}
for (i = 10; i <= 50; i+=10)
{
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-i*2);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y-i*2);
AxisText.Format("%3d",i*10);
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth*3-4,m_ptZeroPoint.y-i*2-5,AxisText);
}
if (m_bPaintCoordinate)
{
pen.DeleteObject();
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-m_coordinateData[0]*2);
for (i = 1; i < GRAPHSAMPLINGNUM; i++)
{
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y-m_coordinateData[i]*2);
}
}
//画鼠标坐标
pen.DeleteObject();
pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
if (m_ptCursor.x > m_ptZeroPoint.x && m_ptCursor.x < m_ptEndPoint.x
&& m_ptCursor.y < m_ptZeroPoint.y && m_ptCursor.y > m_ptEndPoint.y)
{
cursorText.Format("(%2.2f,%d)",(float)m_sampStart*SAMPLINGTIME/1000+(float)(m_ptCursor.x - m_ptZeroPoint.x)*RATIO*SAMPLINGTIME/1000,(m_ptZeroPoint.y - m_ptCursor.y)/2*10);
if (m_ptCursor.x > m_ptGraphStart.x+m_graphWidth - 100)
{
MemDC.TextOut(m_ptCursor.x-70,m_ptCursor.y+10,cursorText);
}
else
{
MemDC.TextOut(m_ptCursor.x+20,m_ptCursor.y+10,cursorText);
}
//画参考线
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptCursor.x-10,m_ptCursor.y);
MemDC.LineTo(m_ptCursor.x+10,m_ptCursor.y);
MemDC.MoveTo(m_ptCursor.x,m_ptCursor.y-10);
MemDC.LineTo(m_ptCursor.x,m_ptCursor.y+10);
}
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
pDc->BitBlt(m_ptGraphStart.x,m_ptGraphStart.y,m_graphWidth,m_graphHeight,&MemDC,0,0,SRCCOPY);
}
void CMy111View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC *pDc = GetDC();
DrawResponseGraph(pDc);
// Do not call CFormView::OnPaint() for painting messages
}
void CMy111View::DrawResponseGraph(CDC *pDc)
{
int i;
CBitmap curveBitmap;
// float cutTime;
// float cutValue;
int nTextHeight;
int nTextWidth;
CString AxisText;
CString AxisText1;
CString cursorText;
CString cutText;
CString responseText;
TEXTMETRIC Text;
CPen pen;
CDC MemDC;
MemDC.CreateCompatibleDC(NULL);
curveBitmap.CreateCompatibleBitmap(pDc,m_graphWidth,m_graphHeight);
MemDC.SelectObject(&curveBitmap);
MemDC.FillSolidRect(0,0,m_graphWidth,m_graphHeight,pDc->GetPixel(5,5));
pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptZeroPoint.x,m_ptEndPoint.y);
MemDC.MoveTo(m_ptZeroPoint);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y);
//画坐标轴和刻度
MemDC.GetTextMetrics(&Text);
nTextWidth = Text.tmAveCharWidth;
nTextHeight = Text.tmHeight;
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(150,150,150));
MemDC.SelectObject(&pen);
AxisText.Format("%dT/(s)",m_sampStart*SAMPLINGTIME/1000);
MemDC.TextOut(m_ptZeroPoint.x+-nTextWidth-15,m_ptZeroPoint.y+2,AxisText);
AxisText1.Format("V/(mm/s)");
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth-50,m_ptGraphStart.y-m_graphHeight-20,AxisText1);
ASSERT(RATIO > 0);
for (i = 50; i <= 800; i+=50)
{
MemDC.MoveTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y);
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptEndPoint.y);
AxisText.Format("%3d",m_sampStart*SAMPLINGTIME/1000+(i*RATIO*SAMPLINGTIME)/1000);
MemDC.TextOut(m_ptZeroPoint.x+i-nTextWidth-5,m_ptZeroPoint.y+2,AxisText);
}
for (i = 10; i <= 50; i+=10)
{
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-i*2);
MemDC.LineTo(m_ptEndPoint.x,m_ptZeroPoint.y-i*2);
AxisText.Format("%3d",i*10);
MemDC.TextOut(m_ptZeroPoint.x-nTextWidth*3-4,m_ptZeroPoint.y-i*2-5,AxisText);
}
if (m_bPaintCoordinate)
{
pen.DeleteObject();
pen.CreatePen(PS_SOLID,2,RGB(255,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptZeroPoint.x,m_ptZeroPoint.y-m_coordinateData[0]*2);
for (i = 1; i < GRAPHSAMPLINGNUM; i++)
{
MemDC.LineTo(m_ptZeroPoint.x+i,m_ptZeroPoint.y-m_coordinateData[i]*2);
}
}
//画鼠标坐标
pen.DeleteObject();
pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
if (m_ptCursor.x > m_ptZeroPoint.x && m_ptCursor.x < m_ptEndPoint.x
&& m_ptCursor.y < m_ptZeroPoint.y && m_ptCursor.y > m_ptEndPoint.y)
{
cursorText.Format("(%2.2f,%d)",(float)m_sampStart*SAMPLINGTIME/1000+(float)(m_ptCursor.x - m_ptZeroPoint.x)*RATIO*SAMPLINGTIME/1000,(m_ptZeroPoint.y - m_ptCursor.y)/2*10);
if (m_ptCursor.x > m_ptGraphStart.x+m_graphWidth - 100)
{
MemDC.TextOut(m_ptCursor.x-70,m_ptCursor.y+10,cursorText);
}
else
{
MemDC.TextOut(m_ptCursor.x+20,m_ptCursor.y+10,cursorText);
}
//画参考线
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
MemDC.MoveTo(m_ptCursor.x-10,m_ptCursor.y);
MemDC.LineTo(m_ptCursor.x+10,m_ptCursor.y);
MemDC.MoveTo(m_ptCursor.x,m_ptCursor.y-10);
MemDC.LineTo(m_ptCursor.x,m_ptCursor.y+10);
}
pen.DeleteObject();
pen.CreatePen(PS_DOT,1,RGB(0,0,0));
MemDC.SelectObject(&pen);
pDc->BitBlt(m_ptGraphStart.x,m_ptGraphStart.y,m_graphWidth,m_graphHeight,&MemDC,0,0,SRCCOPY);
}
#13
先取窗口客户区矩形,然后用两个循环来画线。
画竖线时:
for (int x=0; x < 客户区矩形.right; x += 间隔)
{
pDC->MoveTo(x, 0);
pDC->LineTo(x, 客户区矩形.bottom);
}
画横线时:
for (int y=客户区矩形.bottom-1; y>=0; y -= 间隔)
{
pDC->MoveTo(0, y);
pDC->LineTo(客户区矩形.right, y);
}
另外还要响应WM_SIZE消息,当窗口高度改变时刷新窗口。
画竖线时:
for (int x=0; x < 客户区矩形.right; x += 间隔)
{
pDC->MoveTo(x, 0);
pDC->LineTo(x, 客户区矩形.bottom);
}
画横线时:
for (int y=客户区矩形.bottom-1; y>=0; y -= 间隔)
{
pDC->MoveTo(0, y);
pDC->LineTo(客户区矩形.right, y);
}
另外还要响应WM_SIZE消息,当窗口高度改变时刷新窗口。
#14
网格面积大小保持一定范围
就是横向、纵向象素数保持一定范围
就是横向、纵向象素数保持一定范围
#15
MSDN的DRAWCLI 示例
#16
m_timeaxis.m_dSecondsPrPixel =((double)(m_timeaxis.m_maxtime - m_timeaxis.m_mintime))/ (double)m_plotRect.Width();
void clPlot::DrawXAxisGrid(CDC * dc)
{
long yGrid = m_timeaxis.m_mintime;
long delta = (long)(80.0 + (long)(((m_timeaxis.m_dSecondsPrPixel)))*10);
long d10 = (long)(delta / 10.0);
// todo: delta switch
long diff = ((long)yGrid)%((long)delta);
yGrid = yGrid - diff;
CPen *old, pen(PS_SOLID, 1, m_gridColor);
CPen stick(PS_SOLID,0,RGB(0,0,0));
CPen mline(PS_SOLID,0,RGB(192,192,192));
for( long sx = m_timeaxis.m_mintime - diff; sx < m_timeaxis.m_maxtime; sx+=d10)
{
int off=3;
if((long)sx%(long)delta == 0)
{
off=5;
}
if(sx > m_timeaxis.m_mintime)
{
int x = (int)(m_plotRect.left + ((sx-m_timeaxis.m_mintime)/m_timeaxis.m_dSecondsPrPixel));
// int x = m_plotRect.right - ((sx-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_lSecondsPrPixel);
old = dc->SelectObject(&stick);
dc->MoveTo(CPoint(x,m_plotRect.bottom));
dc->LineTo(CPoint(x+off,m_plotRect.bottom));
dc->SelectObject(old);
old = dc->SelectObject(&mline);
dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
dc->LineTo(CPoint(x,m_plotRect.top+1));
dc->SelectObject(old);
}
}
old = dc->SelectObject(&pen);
while(yGrid <= m_timeaxis.m_maxtime)
{
int x = (int)(m_plotRect.left + ((yGrid-m_timeaxis.m_mintime)/m_timeaxis.m_dSecondsPrPixel));
if(yGrid > m_timeaxis.m_mintime && yGrid<m_timeaxis.m_maxtime)
{
dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
dc->LineTo(CPoint(x,m_plotRect.top+1));
}
// char b[100];
// sprintf(b, "%.0f", yGrid);
// dc->DrawText(b, CRect(m_clientRect.left, y-m_TextHeight/2,m_plotRect.left-5,y+m_TextHeight/2), DT_RIGHT|DT_BOTTOM);
yGrid += delta;
}
dc->SelectObject(old);
}
#17
class timeaxis
{
public:
CString m_szTitle; // time axis title;
int m_mintime; // min time
int m_maxtime; // max time
int m_iTimeMode; // axis grid and legend interval index
double m_dSecondsPrPixel;
timeaxis()
{
m_szTitle = "Time";
m_mintime = 0;
m_maxtime = 800;
m_iTimeMode=0;
m_dSecondsPrPixel=1;
}
};
#18
看看,给力的顶一下