关于交互绘图时,坐标映射问题。
问题:为什么静态绘制的时候可以正常显示,但光标移动或者单击时,光标或者单击时点出现的位置刚好是在Y轴的方向上反了?代码如下:
/***********************************************************/
OnDraw里面:
//坐标映射
CRect rcClient;
GetClientRect(&rcClient);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportOrg(rcClient.left,rcClient.bottom);
pDC->SetWindowExt(rcClient.Width(),rcClient.Height());
pDC->SetViewportExt(rcClient.right,-rcClient.bottom);
pDC->LPtoDP(&rcClient);
//绘制坐标原点
CPoint pt(rcClient.left+20,rcClient.bottom+20);
DrawCoordOrigin(pDC,pt);
//图形绘制
.......
/***********************************************************/
OnPaint里面:
//坐标映射
CRect rcClient;
GetClientRect(&rcClient);
dc.SetMapMode(MM_ANISOTROPIC);
dc.SetViewportOrg(rcClient.left,rcClient.bottom);
dc.SetWindowExt(rcClient.Width(),rcClient.Height());
dc.SetViewportExt(rcClient.right,-rcClient.bottom);
dc.LPtoDP(&rcClient);
//绘制图纸背景
DrawPaperBk(&dc);
/***********************************************************/
OnEraseBkgnd里面:
//坐标映射
CRect rcClient;
GetClientRect(&rcClient);
dc.SetMapMode(MM_ANISOTROPIC);
dc.SetViewportOrg(rcClient.left,rcClient.bottom);
dc.SetWindowExt(rcClient.Width(),rcClient.Height());
dc.SetViewportExt(rcClient.right,-rcClient.bottom);
dc.LPtoDP(&rcClient);
/***********************************************************/
OnLButtonDown里面(采用双缓冲方式):
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->GetMainWnd();
CChildFrame* pChild=(CChildFrame*)pMain->GetActiveFrame();
CMCNCView* pView=(CMCNCView*)pChild->GetActiveView();
if (pView==this)
{
if (m_Activat)
{
CMCNCDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->SetModifiedFlag(TRUE);//设置修改标志
CClientDC dc(this);
CBitmap m_bitmap;
CBitmap* pOldBitmap;
CDC* pDC=new CDC;
CRect rect;
//OnPrepareDC(&dc);
GetClientRect(&rect);
pDC->CreateCompatibleDC(&dc);
m_bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
pOldBitmap=pDC->SelectObject(&m_bitmap);
CBrush BkBrush(RGB(0,0,0));
CBrush* pOldBrush=pDC->SelectObject(&BkBrush);
pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY);
OnPrepareDC(pDC);
//坐标映射
CRect rcClient;
GetClientRect(&rcClient);
dc.SetMapMode(MM_ANISOTROPIC);
dc.SetViewportOrg(rcClient.left,rcClient.bottom);
dc.SetWindowExt(rcClient.Width(),rcClient.Height());
dc.SetViewportExt(rcClient.right,-rcClient.bottom);
dc.LPtoDP(&rcClient);
dc.LPtoDP(&point);
//绘制背景
COLORREF OldColor=pDC->SetBkColor(RGB(0,0,0));
//磁靠选中点
int nCount=pDoc->m_LineArray.GetSize();
for (int i=0;i<nCount;i++)
{
CLine* pLine=(CLine*)pDoc->m_LineArray.GetAt(i);
pLine->MoveToPoint(point);
}
//绘制光标
m_NowPt=point;
DrawCursor(pDC,rect,m_NowPt,m_PrePt);
m_PrePt=m_NowPt;
........
/***********************************************************/
OnMouseMove里面(采用双缓冲方式)
CMainFrame* pMain=(CMainFrame*)AfxGetApp()->GetMainWnd();
CChildFrame* pChild=(CChildFrame*)pMain->GetActiveFrame();
CMCNCView* pView=(CMCNCView*)pChild->GetActiveView();
if (pView==this)
{
m_Activat=TRUE;
CMCNCDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
OnPrepareDC(&dc);
CBitmap m_bitmap;
CBitmap* pOldBitmap;
CDC* pDC=new CDC;
CRect rect;
OnPrepareDC(&dc);
GetClientRect(&rect);
pDC->CreateCompatibleDC(&dc);
m_bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
pOldBitmap=pDC->SelectObject(&m_bitmap);
CBrush BkBrush(RGB(0,0,0));
CBrush* pOldBrush=pDC->SelectObject(&BkBrush);
pDC->PatBlt(rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY);
OnPrepareDC(pDC);
//坐标映射
CRect rcClient;
GetClientRect(&rcClient);
dc.SetMapMode(MM_ANISOTROPIC);
dc.SetWindowExt(rcClient.Width(),rcClient.Height());
dc.SetViewportExt(rcClient.right,-rcClient.bottom);
dc.SetViewportOrg(rcClient.left,rcClient.bottom);
dc.LPtoDP(&rcClient);
dc.DPtoLP(&point);//这里转换为逻辑坐标
.........
/***********************************************************/
每个函数中标有//坐标映射的地方就是映射的代码.
目前效果图如下:
原点坐标已经转换了,但我移动鼠标的时候光标的Y轴方向跟鼠标移动的方向反了!!!!记住是Y轴的方向。
分数不多,有了再给。
万分感激!