Windows GDI中的坐标系一文所涉及的代码

博客给出两段源码。一段是把逻辑位置转换为最终物理坐标空间中位置的函数,通过获取设备参数进行转换;另一段是自定义的把逻辑坐标转换为设备坐标的函数,处理了启用世界坐标系的情况,并根据窗口、视口信息完成转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值