html关于窗体控件相对屏幕的坐标,c++ 如何获取控件相对于窗口客户端的位置?...

我想要写这样的代码:

HWND hwnd = ;

int positionX;

int positionY;

GetWindowPos(hwnd, &positionX, &positionY);

SetWindowPos(hwnd, 0, positionX, positionY, 0, 0, SWP_NOZORDER | SWP_NOSIZE);

并且它什么都不做。但是,我无法找出如何编写GetWindowPos()函数,以正确的单位给出答案:

void GetWindowPos(HWND hWnd, int *x, int *y)

{

HWND hWndParent = GetParent(hWnd);

RECT parentScreenRect;

RECT itemScreenRect;

GetWindowRect(hWndParent, &parentScreenRect);

GetWindowRect(hWnd, &itemScreenRect);

(*x) = itemScreenRect.left - parentScreenRect.left;

(*y) = itemScreenRect.top - parentScreenRect.top;

}

如果我使用这个函数,我得到相对于父窗口左上角的坐标,但SetWindowPos()需要相对于标题栏下面的区域的坐标(我假定这是“客户区”,但是win32术语对我来说有点新鲜)

这是工作的GetWindowPos()函数(谢谢Sergius):

void GetWindowPos(HWND hWnd, int *x, int *y)

{

HWND hWndParent = GetParent(hWnd);

POINT p = {0};

MapWindowPoints(hWnd, hWndParent, &p, 1);

(*x) = p.x;

(*y) = p.y;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值