Windows GDI中的座標系一文所涉及的代碼

來源:互聯網
上載者:User

源碼1

/*
 Function:

 把邏輯位置轉換為最終的物理座標空間中的位置

 Parameter:

 hDC---待轉換邏輯座標所處的空間

 lpPoint---待轉換的邏輯點轉換前為邏輯點,轉換後為取整後的毫米

 nCount----待轉換點的個數

 RetValue:

 TRUE or FALSE

 History:

 2003-10-25 11:13
*/
void GetPhysicalPosition(HDC hDC,LPPOINT lpPoint ,int nCount)
{
 POINT originPoint;

 int widthmm=GetDeviceCaps(hDC,HORZSIZE);
 int heightmm=GetDeviceCaps(hDC,VERTSIZE);

 int widthres=GetDeviceCaps(hDC,HORZRES);
 int heightres=GetDeviceCaps(hDC,VERTRES);

 LPtoDP(hDC,lpPoint,nCount);

 GetDCOrgEx(hDC,&originPoint);

 for(int i=0; i<nCount; ++i)
 {
  lpPoint[i].x +=originPoint.x;
  lpPoint[i].y +=originPoint.y;

  lpPoint[i].x=lpPoint[i].x*widthmm/widthres;
  lpPoint[i].y=lpPoint[i].y*heightmm/heightres;
 }

}

源碼2
/*
 Function:

  我們自己的把邏輯座標轉換為裝置座標的函數
*/
BOOL MyLPtoDP(
  HDC hdc,           // handle to device context
  LPPOINT lpPoints,  // array of points
  int nCount         // count of points in array
)
{
 int graphicsMode=GetGraphicsMode(hdc);

 if(graphicsMode ==GM_ADVANCED) //處理啟用了全局座標系的情況
 {
  XFORM curForm;

  GetWorldTransform(hdc,&curForm);

  for(int i=0; i<nCount; ++i)//應用公式一完成全局座標空間向頁面座標空間的轉換
  {
   float xpage=lpPoints[i].x*curForm.eM11+lpPoints[i].y*curForm.eM21+curForm.eDx;
   float ypage=lpPoints[i].x*curForm.eM12+lpPoints[i].y*curForm.eM22+curForm.eDy;

   lpPoints[i].x=(int)xpage;
   lpPoints[i].y=(int)ypage;
  }
 }

 POINT pointOrgView,pointOrgWin;
 SIZE winSize,viewSize;

 //得到視窗、視口的原點和範圍
 GetViewportOrgEx(hdc,&pointOrgView);
 GetViewportExtEx(hdc,&viewSize);
 GetWindowOrgEx(hdc,&pointOrgWin);
 GetWindowExtEx(hdc,&winSize);

 //根據公式二進行頁面座標空間到裝置座標空間的轉換
 for(int i=0; i<nCount; ++i)
 {
  float xdevice=(lpPoints[i].x-pointOrgWin.x)*viewSize.cx/(float)winSize.cx+pointOrgView.x;
  float ydevice=(lpPoints[i].y-pointOrgWin.y)*viewSize.cy/(float)winSize.cy+pointOrgView.y;

  lpPoints[i].x=(int)xdevice;
  lpPoints[i].y=(int)ydevice;
 
 }

 return TRUE;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.