dc.SetWindowOrg(50,50); 
dc.SetViewportOrg(100,100);CPoint pt;
pt = CPoint(100, 200); dc.LPtoDP(&pt); 
TRACE(_T("LPtoDP pt = (%d, %d) \n"), pt.x,pt.y); pt = CPoint(100, 200); 
dc.DPtoLP(&pt); 
TRACE(_T("DPtoLP pt = (%d, %d) \n"), pt.x,pt.y); 输出为:
LPtoDP pt = (150, 250) 
DPtoLP pt = (50, 150) 上面的结果是如何计算出来的?

解决方案 »

  1.   

    void CDC::LPtoDP(LPSIZE lpSize) const
    {
    ASSERT(AfxIsValidAddress(lpSize, sizeof(SIZE))); CSize sizeWinExt = GetWindowExt();
    CSize sizeVpExt = GetViewportExt();
    lpSize->cx = MulDiv(lpSize->cx, abs(sizeVpExt.cx), abs(sizeWinExt.cx));
    lpSize->cy = MulDiv(lpSize->cy, abs(sizeVpExt.cy), abs(sizeWinExt.cy));
    }void CDC::DPtoLP(LPSIZE lpSize) const
    {
    ASSERT(AfxIsValidAddress(lpSize, sizeof(SIZE))); CSize sizeWinExt = GetWindowExt();
    CSize sizeVpExt = GetViewportExt();
    lpSize->cx = MulDiv(lpSize->cx, abs(sizeWinExt.cx), abs(sizeVpExt.cx));
    lpSize->cy = MulDiv(lpSize->cy, abs(sizeWinExt.cy), abs(sizeVpExt.cy));
    }在VC里转到LPtoDP就找到了在wingdi.cpp中的上述代码。
      

  2.   

    因为SetWindowOrg已经改变的圆点坐标由0,0改变为50,50了
    那个相对它到圆点的距离肯定也变为150,250了
    至于DPtoLP函数功能:该函数将设备坐标转变为逻辑坐标,转变依赖于设备的图形模式,窗口和坐标的起点及范围的设置,和转换的内容,
    其实你应该了解下这几个函数的功能,然后你自然就明白为什么了!
      

  3.   

    SetWindowOrg和SetViewportOrg不要同时用,它们之间是有关联的。
    MSDN里说了:This is related to the SetViewportOrgEx function. Generally, you will use one function or the other, but not both. 
      

  4.   


    你找的只是LPtoDP(LPSIZE lpSize)函数原型,不是楼主要的LPtoDP(LPPOINT lpPoints)函数原型agree
      

  5.   

    The SetWindowOrg function specifies which window point maps to the viewport origin (0,0). The SetViewportOrg function specifies which device point maps to the window origin (0,0).The DPtoLP function converts device coordinates into logical coordinates. The conversion depends on the mapping mode of the device context, the settings of the origins and extents for the window and viewport, and the world transformation. 
      

  6.   

    对于所有的映像方式,Windows都用下面两个公式来将窗口(逻辑)坐标转化为视口(设备)坐标:
    将窗口坐标转换成视口坐标DPtoLPxviewport=(xwindow-xwinorg)*(xviewext/xwinext)+xvieworg yviewport=(ywindow-ywinorg)*(yviewext/ywinext)+yvieworg
    视口坐标转换为窗口坐标LPtoDPxwindow=(xviewport-xvieworg)*(xwinext/xviewext)+xwinorg ywindow=(yviewport-yvieworg)*(ywinext/yviewext)+ywinorg
      

  7.   

    http://www.vckbase.com/document/viewdoc/?id=927看下这个
      

  8.   

    参看下面文章:
    http://www.joyvc.cn/GraphicAndMedia/GraphicAndMedia00001.html
      

  9.   

    为什么在 MM_TEXT 模式下,CSize sizeWinExt=GetWindowExt(); 
        CSize sizeVpExt=GetViewportExt(); 都是 1, 1???
      

  10.   

    因为在MM_TEXT映射模式下,DP和LP的比例是1:1
    函数GetWindowExt(); 和GetViewportExt(); 取出的值单独看,看不出个所以然,两个值同时参考才有意义,即窗口和视口的比例关系。
      

  11.   

    我也认为SetWindowOrg和SetViewportOrg不要同时用
    SetWindowOrg是设置逻辑坐标原点
      

  12.   

    同意8楼(xviewport - xvieworg)/(xwindow-xwinorg)=xviewext/xwinext