在vc++中利用MFC建立了一个对话框式的软件界面,如何在坐标轴显示刻度值的大小?
坐标轴已经建立好了,就是刻度没有不会标上去,需要能根据坐标轴中的图像结合坐标轴刻度能读出图形的相关数据(如电流)...
坐标轴已经建立好了,就是刻度没有不会标上去,需要能根据坐标轴中的图像结合坐标轴刻度能读出图形的相关数据(如电流)
展开
2个回答
展开全部
修改一下这个函数就可以了
//用以绘制坐标刻度
void CCoordinates::DrawCoordinate(HDC hDC)
{
int nIndex =0;
int nCount=0;
CPen pen(PS_SOLID,1,RGB(0,0,255));
HGDIOBJ hOldPen = SelectObject(hDC,pen.m_hObject);
char szDigi[64];
//画水平坐标系
{
//画水平线
MoveToEx(hDC,m_nXOffset,m_nYOffset,NULL);
LineTo(hDC,m_nHLength+m_nXOffset,m_nYOffset);
nCount = m_nHLength/10;
//画刻度,间隔10像素一个小刻度,每50一个中刻度,100一个大刻度
for(nIndex=0;nIndex<nCount;nIndex++)
{
if(nIndex%10==0) //大刻度高15像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-15);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset+nIndex*10-10,m_nYOffset+5,szDigi,strlen(szDigi));
}
else if(nIndex%5==0)//中刻度高10像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-10);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset+nIndex*10-10,m_nYOffset+5,szDigi,strlen(szDigi));
}
else //小刻度高5像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-5);
}
}
}
//画垂直坐标系
{
//画垂直线
MoveToEx(hDC,m_nXOffset,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset,0);//m_nVLength+m_nYOffset);
nCount=m_nVLength/10;
//画刻度,间隔10像素一个小刻度,每50一个中刻度,100一个大刻度
for(nIndex=0;nIndex<nCount;nIndex++)
{
if(nIndex%10==0) //大刻度高15像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+15,m_nYOffset-nIndex*10);
if(0!=nIndex)
{
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset-25,m_nYOffset-nIndex*10-5,szDigi,strlen(szDigi));
}
}
else if(nIndex%5==0)//中刻度高10像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+10,m_nYOffset-nIndex*10);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset-25,m_nYOffset-nIndex*10-5,szDigi,strlen(szDigi));
}
else //小刻度高5像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+5,m_nYOffset-nIndex*10);
}
}
}
SelectObject(hDC,hOldPen);
}
//用以绘制坐标刻度
void CCoordinates::DrawCoordinate(HDC hDC)
{
int nIndex =0;
int nCount=0;
CPen pen(PS_SOLID,1,RGB(0,0,255));
HGDIOBJ hOldPen = SelectObject(hDC,pen.m_hObject);
char szDigi[64];
//画水平坐标系
{
//画水平线
MoveToEx(hDC,m_nXOffset,m_nYOffset,NULL);
LineTo(hDC,m_nHLength+m_nXOffset,m_nYOffset);
nCount = m_nHLength/10;
//画刻度,间隔10像素一个小刻度,每50一个中刻度,100一个大刻度
for(nIndex=0;nIndex<nCount;nIndex++)
{
if(nIndex%10==0) //大刻度高15像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-15);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset+nIndex*10-10,m_nYOffset+5,szDigi,strlen(szDigi));
}
else if(nIndex%5==0)//中刻度高10像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-10);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset+nIndex*10-10,m_nYOffset+5,szDigi,strlen(szDigi));
}
else //小刻度高5像素
{
MoveToEx(hDC,m_nXOffset+nIndex*10,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset+nIndex*10,m_nYOffset-5);
}
}
}
//画垂直坐标系
{
//画垂直线
MoveToEx(hDC,m_nXOffset,m_nYOffset,NULL);
LineTo(hDC,m_nXOffset,0);//m_nVLength+m_nYOffset);
nCount=m_nVLength/10;
//画刻度,间隔10像素一个小刻度,每50一个中刻度,100一个大刻度
for(nIndex=0;nIndex<nCount;nIndex++)
{
if(nIndex%10==0) //大刻度高15像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+15,m_nYOffset-nIndex*10);
if(0!=nIndex)
{
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset-25,m_nYOffset-nIndex*10-5,szDigi,strlen(szDigi));
}
}
else if(nIndex%5==0)//中刻度高10像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+10,m_nYOffset-nIndex*10);
sprintf(szDigi,"%d",nIndex*10);
TextOut(hDC,m_nXOffset-25,m_nYOffset-nIndex*10-5,szDigi,strlen(szDigi));
}
else //小刻度高5像素
{
MoveToEx(hDC,m_nXOffset,m_nYOffset-nIndex*10,NULL);
LineTo(hDC,m_nXOffset+5,m_nYOffset-nIndex*10);
}
}
}
SelectObject(hDC,hOldPen);
}
展开全部
以下是一段参考代码,具体数值你自己改。
void CDrawDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC *pDC;
pDC=GetDC();
CPen pen,pen1;
pen.CreatePen(PS_SOLID,3,RGB(200,0,0));
pen1.CreatePen(PS_SOLID,1,RGB(0,0,0));
pDC->SelectObject(&pen);
pDC->SetViewportOrg(margin,margin);
pDC->SetMapMode(MM_TEXT);
pDC->Rectangle(0,0,500,500);
pDC->SelectObject(&pen1);
//draw coordinate scale line
int Xlenth=0;
for (int X=0;X<10,Xlenth<=500;X++)
{
pDC->MoveTo(Xlenth,500);
pDC->LineTo(Xlenth,510);
Xlenth+=50;
}
int Xilenth=0;
for (int Xi=0;Xi<100,Xilenth<=500;Xi++)
{
pDC->MoveTo(Xilenth,500);
pDC->LineTo(Xilenth,505);
Xilenth+=5;
}
int Ylength=0;
for (int Y=0;Y<10,Ylength<=500;Y++)
{
pDC->MoveTo(-10,Ylength);
pDC->LineTo(0,Ylength);
Ylength+=50;
}
int Yilength=0;
for (int Yi=0;Yi<100,Yilength<=500;Yi++)
{
pDC->MoveTo(-5,Yilength);
pDC->LineTo(0,Yilength);
Yilength+=5;
}
pen.DeleteObject();
pen1.DeleteObject();
// Do not call CDialog::OnPaint() for painting messages
DrawCoordinate();//此函数绘制刻度的值
}
以下是DrawCoordinate()函数部分代码
{
. ......
//刻度Y轴
int Ylength=0;
int posY=Ymin;
for (int Y=0;Y<10,Ylength>=-canvas,posY<=Ymax;Y++)
{
pDC->MoveTo(-10,Ylength);
pDC->LineTo(0,Ylength);
char str[20];
sprintf(str,"%d", posY);
int nBkMode = pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(-60,Ylength,str); //这里输出在Y轴方向具体的刻度值
pDC->SetBkMode(nBkMode);
posY=(int)((Ymax-Ymin)/10+posY);
Ylength-=50;
}
void CDrawDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC *pDC;
pDC=GetDC();
CPen pen,pen1;
pen.CreatePen(PS_SOLID,3,RGB(200,0,0));
pen1.CreatePen(PS_SOLID,1,RGB(0,0,0));
pDC->SelectObject(&pen);
pDC->SetViewportOrg(margin,margin);
pDC->SetMapMode(MM_TEXT);
pDC->Rectangle(0,0,500,500);
pDC->SelectObject(&pen1);
//draw coordinate scale line
int Xlenth=0;
for (int X=0;X<10,Xlenth<=500;X++)
{
pDC->MoveTo(Xlenth,500);
pDC->LineTo(Xlenth,510);
Xlenth+=50;
}
int Xilenth=0;
for (int Xi=0;Xi<100,Xilenth<=500;Xi++)
{
pDC->MoveTo(Xilenth,500);
pDC->LineTo(Xilenth,505);
Xilenth+=5;
}
int Ylength=0;
for (int Y=0;Y<10,Ylength<=500;Y++)
{
pDC->MoveTo(-10,Ylength);
pDC->LineTo(0,Ylength);
Ylength+=50;
}
int Yilength=0;
for (int Yi=0;Yi<100,Yilength<=500;Yi++)
{
pDC->MoveTo(-5,Yilength);
pDC->LineTo(0,Yilength);
Yilength+=5;
}
pen.DeleteObject();
pen1.DeleteObject();
// Do not call CDialog::OnPaint() for painting messages
DrawCoordinate();//此函数绘制刻度的值
}
以下是DrawCoordinate()函数部分代码
{
. ......
//刻度Y轴
int Ylength=0;
int posY=Ymin;
for (int Y=0;Y<10,Ylength>=-canvas,posY<=Ymax;Y++)
{
pDC->MoveTo(-10,Ylength);
pDC->LineTo(0,Ylength);
char str[20];
sprintf(str,"%d", posY);
int nBkMode = pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(-60,Ylength,str); //这里输出在Y轴方向具体的刻度值
pDC->SetBkMode(nBkMode);
posY=(int)((Ymax-Ymin)/10+posY);
Ylength-=50;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询