Je veux être en mesure d'écrire du code comme ceci:Comment obtenir la position d'un contrôle par rapport au rectangle client de la fenêtre?
HWND hwnd = <the hwnd of a button in a window>;
int positionX;
int positionY;
GetWindowPos(hwnd, &positionX, &positionY);
SetWindowPos(hwnd, 0, positionX, positionY, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
Et avoir rien faire. Cependant, je ne peux pas travailler sur la façon d'écrire une fonction GetWindowPos()
qui me donne des réponses dans les bonnes unités:
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;
}
Si j'utilise cette fonction, je reçois des coordonnées qui sont par rapport à la partie supérieure gauche du parent fenêtre, mais SetWindowPos()
veut des coordonnées par rapport à la zone en dessous de la barre de titre (je présume qu'il s'agit de la "zone client", mais la terminologie win32 est tout un peu nouveau pour moi).
Solution C'est la fonction GetWindowPos()
de travail (merci 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;
}
Est-ce une application de fenêtre –
Comment ça marche, Quelle est l'utilisation de DirectX. Débutant en directx. J'ai fait ma propre fonction pour le faire –
Oui c'est une application windows, d'où l'utilisation de win32 api. – Andy